Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Enum Values Each Having Their Own Enum Values

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Enum Values Each Having Their Own Enum Values

    (The title sucks because I don't understand my problem well; I'm lost as to how I approach this. Hopefully the question is answerable!)

    As an exercise to test and broaden my knowledge of OOP, I decided to make a bunch of objects pertaining to Dungeons and Dragons (knowledge of D&D won't be necessary for answering this question). The first thing I decided to start on was a spell. To simplify the spell for now, let's say it has three things: a name, school and subschool. The name is self-explanatory, so I'll skip that, however the school isn't. All you'll really need to know is that the "school" is similar to a "School of Thought", not institute for education. It can be thought of as a classification. Some (not all) of the schools have subschools, which are basically sub-classifications.

    There's 8 schools, so I decided that enumeration would work great. The part that I am not sure how to approach is the subschools. Let's take a look at one of the schools: conjuration. Conjuration has it's own 5 subschools, which are unique to it.

    I wish to create a class that holds the information for the spell. To start off, I have a String for the name, and an enum for the school. Here's what I have so far:

    public class Spell {
        private String name;
        private School school;
        // TODO: Subschool
    }
     
    public enum School {
        ABJURATION("Abjuration"), CONJURATION("Conjuration"), DIVINATION("Divination"), ENCHANTMENT("Enchantment"),
        EVOCATION("Evocation"), ILLUSION("Illusion"), NECROMANCY("Necromancy"), TRANSMUTATION("Transmutation");
    }

    In the enumeration School, I have the 8 schools, and their names to make it easy to display in a program. The problem I am having is that I don't know what type I should use for my subschool.

    An example of a use for this spell would be a program that creates a new spell. It has a textbox for the name, and a drop-down list for the school and subschool. By selecting one of the schools (such as conjuration), the second drop-down list should automatically update with the 5 different subschools of conjuration. I can then press a button and it will create a class with the name, school and subschool stored inside of it.

    I don't actually need someone to write any code for me; all I am looking for is a high-level explanation of the relations for the fields, and if given the proper terminology, I should be able to research anything I don't know and write the code myself, although a coded example works too. Whichever is easier for you, works for me.

    Thanks!


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Inspired
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: Enum Values Each Having Their Own Enum Values

    There are two main considerations:

    1. the data structure that stores the schools and subschools
    2. the way to interact with the user.

    1. The data structure.
    Using enums is among a number of solutions including the use of Maps and Arrays. If you want to use enums you could nest the sub school as an enum with in the definition of the school.

    For example:

    public enum School {
        ABJURATION("Abjuration", SubSchools.SUBSCHOOL1), 
        CONJURATION("Conjuration"); 
     
        private final String name;
        private final SubSchools SubSchoolEnum;
     
      School(String name){
        	this.name = name;
        	this.SubSchoolEnum = null;
      }
     
     School(String name, SubSchools SubSchoolEnum){
         this.name = name;
         this.SubSchoolEnum = SubSchoolEnum;
     }

    SubSchool is the nested enum:

    public enum SubSchools {
    	SUBSCHOOL1(new String[]{"qqqq","wwww","eee"});
     
    	private final String[] SchoolList;
     
     
    	SubSchools(String[] SchoolList){		
    		this.SchoolList = SchoolList;
    	}
     
    	public String[] getSchools(){
    		return this.SchoolList;
    	}
    }


    With this data structure you can retrieve a list of enum in School using the values() method: SubSchools subSchool = school.getSubSchool(); and loop through the enum in school.

    If you add the following methods to School enum you can retrieve data about each school:

    public SubSchools getSubSchool(){
    	return this.SubSchoolEnum;
    }
     
    public String getName(){
    	return this.name;
    }

    This data structure can be used to firstly retrieve the list of school that populate the first drop down and then when the user selects an option the subschools can be retrieved.

    2. Interact with the user.

    This can be done with a combinations of a servlet on the server side and HTML/JAVASCRIPT/AJAX on the client side.

    The servlet is in charge of retrieving the data and sending it to the user, while the HTML displays the dropdown and the Ajax sends the selected option to the servlet which obtains the subschools and sends them in a response to the page which displays the dropdown with the subschool options.

    Theres a lot of work to do here but all the info you need is on the net and there are lots of videos in youtube. Search for using servlets and ajax. Heres a link to get you started.

Similar Threads

  1. Enum with strings
    By popnfresh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2013, 08:12 PM
  2. enum
    By anis.laghaei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 23rd, 2012, 03:19 PM
  3. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  4. Character Values Inside of Number Values
    By bgroenks96 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 08:27 PM
  5. [SOLVED] Enum problem
    By pajfilms in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2011, 07:26 AM

Tags for this Thread