Results 1 to 6 of 6
- 10-31-2012, 03:41 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
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?
- 10-31-2012, 04:05 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-31-2012, 04:06 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 10-31-2012, 04:19 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
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
- 10-31-2012, 04:25 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
- 10-31-2012, 05:34 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
Similar Threads
-
General question, why don't Java classes use enums?
By Anza Power in forum Advanced JavaReplies: 9Last Post: 12-22-2011, 05:50 AM -
Enums taking in enums?
By rizowski in forum New To JavaReplies: 7Last Post: 06-11-2011, 01:40 PM -
trying to learn enums and arrays
By Gerrburge in forum New To JavaReplies: 9Last Post: 02-02-2011, 02:54 PM -
Returning flags from enums
By willemien in forum New To JavaReplies: 5Last Post: 05-26-2010, 07:37 AM -
why we are using enums in Java?
By manish.anchan in forum New To JavaReplies: 7Last Post: 01-08-2010, 04:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks