views:

19

answers:

1

I need to accomplish the following (this is a simplified version):

enum Animals{
 enum Cats{tabby("some value"), siamese("some value")},
 enum Dogs{poodle("some value"), dachsund("some value")},
 enum Birds{canary("some value"), parrot("some value")}

 private String someValue = "";

 private ShopByCategory(String someValue)
 {
     this.someValue = someValue;
 }

 public String getSomeValue()
 {
     return this.someValue;
 }
}

So that I can access these items as follows:

string cat1 = Animals.Cats.tabby.getSomeValue;
string dog1 = Animals.Dogs.dachsund.getSomeValue;
string bird1 = Animals.Birds.canary.getSomeValue;

The reason why I am attempting to do this with enums is the fact that I need to be able to access each tier without having to a) instantiate a class, b) hide the names of the tiers behind a method name, or c) use an iterator to go through an EnumSet.

Is this at all possible? What would you suggest instead of enums?

+2  A: 
//Animals.java
public class Animals {
    public static class Cats {
        public static final String tabby = "some value";
        public static final String siamese = "some value";
    }
    public static class Dogs {
        public static final String poodle = "some value";
        public static final String dachsund = "some value";
    }
    public static class Birds {
        public static final String canary = "some value";
        public static final String parrot = "some value";
    }
}

//ShopByCategory.java
public class ShopByCategory {
    private String someValue;
    public ShopByCategory(String value){
         this.someValue = value;
    }
    public String getSomeValue(){
        return this.someValue;
    }
}
//Main.java - an example of what you can do
public class Main {
    public static void main(String[] args){
         ShopByCategory sbc = new ShopByCategory(Animals.Birds.canary);
         System.out.println(sbc.getSomeValue());
         System.out.println(Animals.Dogs.poodle);
    }
}
Leo Izen