Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I store this data in a Java enum?

Tags:

java

enums

What's the best way to store this data in a Java enum?

<select>
    <option></option>
    <option>Recommend eDelivery</option>
    <option>Require eDelivery</option>
    <option>Require eDelivery unless justification provided</option>
</select>

I'm new to java and have tried things like

public enum Paperless { 
      "None" = null,
      "Recommend eDelivery" = "Recommend eDelivery",
      "Require eDelivery" = "Require eDelivery",
      "Require eDelivery unless justification provided" = "Require eDelivery w/out justification"
}

But this doesn't work. I'm considering the possibility of storing a text value that summarizes the option that the user sees on this web page.

like image 908
Webnet Avatar asked Mar 28 '12 20:03

Webnet


2 Answers

Take a look at the enum tutorial, more specifically the Planet example. You can do the same, e.g.

public enum Paperless{
  NONE( null ),
  RECOMMENDED_DELIVERY( "Recommended delivery" ),
  ...//put here the other values
  REQUIRED_DELIVERY( "Required delivery" );
  private String name;
  Paperless( String name ){
    this.name = name;
  }
  public String getName(){
    return this.name;
  }
}
like image 165
Robin Avatar answered Oct 03 '22 06:10

Robin


Something like this can work for your case:

public enum PaperLess {
    NONE("none"),
    RECOMMEND("Recommend eDelivery"),
    REQUIRE("Require eDelivery"),
    REQUIRE_JUSTIFIED("Require eDelivery unless justification provided");

    private String value;

    private PaperLess(String value) {
       this.value = value;
    }

    public String getValue() {
       return value;
    }
}
like image 37
anubhava Avatar answered Oct 03 '22 08:10

anubhava