Results 1 to 6 of 6
  1. #1
    obby is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Arraylist and classes within Enums...is it possible?

    Hello, i'm trying to do this. I have an enum of week days


    public enum WeekDay {

    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY;

    }


    and i have a class, called session...

    public class Session {
    public String title;
    public int duration,start,end;

    /** several methods follow**/

    }

    a session is simply what is going on at a particular time

    and i have another class, called Venue...

    public class Venue {

    private String name;
    private int capacity;

    /** several methods**/
    }

    what i need to do is this, to create an arraylist of sessions in the enums i.e. each day has its sessions and then i need to hold the enums in a structure (an arraylist or enumset?) within the an instance of venue i.e a venue has sessions from monday to friday (ideally school classes)...so it will be something like this

    public enum WeekDay {

    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY;
    private ArrayList <Session> list;
    private int numOfSessions;

    /** with some methods like **/
    addSession();
    removeSession();
    getSession();
    checkTimeOfSession();
    .....
    }

    public class Venue {

    private String name;
    private int capacity;
    private ? <WeekDay> list;
    /** several methods like **/
    numOfSessionsOn();
    getSessionsOn();
    addSessionOn();
    removeSessionOn();
    ......


    }
    That's what i had in mind, i dind't use a class for WeekDay since they will never change.

    and here are my questions
    1. can i nest the session class within the enum?
    2. can an enum accept arraylists?
    3. which is the best structure to hold the enums with their sessions inside the venue class?
    4. any better idea for this idea?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Arraylist and classes within Enums...is it possible?

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Arraylist and classes within Enums...is it possible?

    Presumably each Venue does not have the same number of Sessions, or indeed the identical Sessions, for each day of the week as all other Venues.
    Because that is what the above model implies.

    There is only one WeekDay object for a MONDAY, which you are supplying to every Venue. It has one List of Sessions.
    Any change made to that MONDAY object in Venue 'A' will be seen by Venues 'B', 'C' and 'D'.

    Keep the enum, but have a Map<WeekDay, List<Session>> in the Venue.
    Or possibly a whole class to represent the Session management.

    Not sure why you need a 'numOfSessions' variable.

    Oh, and please use [code] tags [/code] when posting code.
    A lot of us won't read unformatted code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    obby is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Arraylist and classes within Enums...is it possible?

    Thanks for the that, i never knew i was passing the same day to every venue! thank you

  5. #5
    obby is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Arraylist and classes within Enums...is it possible?

    Quote Originally Posted by Tolls View Post
    Presumably each Venue does not have the same number of Sessions, or indeed the identical Sessions, for each day of the week as all other Venues.
    Because that is what the above model implies.

    There is only one WeekDay object for a MONDAY, which you are supplying to every Venue. It has one List of Sessions.
    Any change made to that MONDAY object in Venue 'A' will be seen by Venues 'B', 'C' and 'D'.

    Keep the enum, but have a Map<WeekDay, List<Session>> in the Venue.
    Or possibly a whole class to represent the Session management.

    Not sure why you need a 'numOfSessions' variable.

    Oh, and please use [code] tags [/code] when posting code.
    A lot of us won't read unformatted code.
    thanks so much...And about the map, how could i add a session to a venue for a particular day? just a little code snippet to see how a map goes...

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Arraylist and classes within Enums...is it possible?

    Map.get().
    If the returned value is null then create a new List<Session> and add it to the map (Map.put()).
    Use this List (either the one returned by the get() or the new one) and add you new Session to it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. General question, why don't Java classes use enums?
    By Anza Power in forum Advanced Java
    Replies: 9
    Last Post: 12-22-2011, 05:50 AM
  2. Enums taking in enums?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 06-11-2011, 01:40 PM
  3. trying to learn enums and arrays
    By Gerrburge in forum New To Java
    Replies: 9
    Last Post: 02-02-2011, 02:54 PM
  4. Returning flags from enums
    By willemien in forum New To Java
    Replies: 5
    Last Post: 05-26-2010, 07:37 AM
  5. why we are using enums in Java?
    By manish.anchan in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 04:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •