0

I have two enum classes College.java and Student.java

here is the code snippet for both the classes.

public enum College {

   ID("Id of College"), NAME("Name of College"), ADDRESS("Address of College");

   private final String description;

   private College(String details) {
       this.description = description;
   }

}

public enum Student {
    ROLL(College.ID, "Roll No of Student"), CLASS(College.NAME, "Class of Student"),
    PLACE(College.ADDRESS, "Place of Student"), STUDENTID(setMap());

    private final String description;
    private final College college;
    private Map<college, String> attributes;

    private Student(College college, String details) {
        this.college = college;
        this.details = details;
    }
    private Student(Map<College, String> attributes) {
        this.attributes = attributes;
        for(college key: attributes.keySet()) {
            this.college=key;
            this.description=attributes.get(key);
        }
        
    }
    
    private Map<College, String> setMap() {
        Map<college, String> map = new HashMap<college, String>();
        map.put(College.ID ,"ID of college");
        map.put(College.NAME,"Name of Student"); 
        map.put(College.ADDRESS,"Address of the College"); 
        return map;
    }
}

My question is that i am unable to set multiple College enum's to one Student enum(STUDENTID). When i tried using a helper method and setting values in Map, It is only setting one value when STUDENTID enum is initialized. I want all the Map values to be initialized for the STUDENTID enum. Is there any other way to do that? Please help me here, I am really stuck here

1 Answers1

0

As per our discussion I believe the code shared would change to the below.

public enum Student {
    ROLL(College.ID, "Roll No of Student"), CLASS(College.NAME, "Class of Student"),
    PLACE(College.ADDRESS, "Place of Student"),
    STUDENTID(
        new MyEntry<College, String>(College.ID, "ID of college"),
        new MyEntry<College, String>(College.NAME, "Name of Student"),
        new MyEntry<College, String>(College.ADDRESS, "Address of the College")
    );

    private Map<College, String> attributes = new HashMap<,>();

    private Student(College college, String details) {
        attributes.put(college, details);
    }

    private Student(MyEntry<College, String>... attributes) {
        for(MyEntry<College, String> entry : attributes) {
            this.attributes.put(entry.getKey(), entry.getValue());
        }
    }
}

The following fields were removed from Student because they were redundant with the map.

private final String description;
private final College college;

Since you mentioned in discussion you are using Java 7 you don't have access to Java 9 Map.ofEntriesMap or Map.entry(described here) so you will have to mimic this using this stackoverflow post. This is where MyEntry class comes from.

This was not tested since I don't have a Java IDE setup right now. However I did do research on many of the functions and most of it should be correct.

As mentioned in the comments and chat this usage of enum is an anti-pattern and should be avoided.

Omar Abdel Bari
  • 934
  • 9
  • 17