5

I'm trying to do this. I have an enum of week days. I have used enum since weekdays are constant

public enum WeekDay {
  MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY;
}

and I have a class, called Session. A session is simply what is going on at a particular time e.g. a maths class

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

  /** several methods follow to get and set, and check e.t.c **/
}

There is a third class, called Venue. Venues host the sessions, e.g. the maths class can be from 9am to 10am, at a venue called "maths-class" (an example)

public class Venue { // simply a place that can hold several sessions in a day
  private String name;
  private int capacity;

  /** several methods**/
}

What I need to do is this - create a list of sessions in the enum i.e. each day has its sessions, and then I need to hold the enums in a structure (an ArrayList or enumset?) within a 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;

  /** the list of sessions for a given day**/
  private ArrayList <Session> list;
  private int numOfSessions; // number of sessions

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

So that in the venue, we can have:

public class Venue {
  private String name;
  private int capacity;
  private ? <WeekDay> list; //structure to hold days, i don't know which one to use yet

  /** several methods like **/
  numOfSessionsOn();
  getSessionsOn();
  addSessionOn();
  removeSessionOn();
  ...
}

And here are my questions:

  1. Can I nest the Session class within an enum?
  2. Can an enum accept arraylists?
  3. What is the best structure to hold the enums with their sessions inside the venue?
  4. Any better ideas for this?

Someone just told me that I'll be passing the same day to all venues regardless e.g. MONDAY is MONDAY for all venues, its list will be updated by every venue. So I think that's the end of discussion even though nobody commented.

2
  • 1
    Yes. To all. Except 4 because session is varying entity. Monday can have two session of Physics and next monday it will have two completely different sessions of another subject so it will not be wise to use this structure at all. Commented Oct 31, 2012 at 17:53
  • did you solve your problem? Commented Dec 7, 2012 at 17:10

2 Answers 2

2

1) Yes, you can.

2) Enum can have all that a normal Class have. The only diference is that it has fixed number of Objects (for example, your WeekDay have MONDAY, TUESDAY, etc.). So, you can have something like

public enum WeekDay {
    MONDAY, ..., FRIDAY;
    private final List<Session> sessions;
    private WeekDay {
        this.sessions = new ArrayList<Session>();
    }
}

3) It always depends on the case. You can use EnumSet to save your WeekDay

4) You are trying to use your Enum as a Class. Enum should always be Atomic (don't have state). I would let WeekDay be a Simple Enum and would structure something like the structure I posted bellow:

public enum WeekDay {
    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY;
}
// Session have a topic and a duration
public class Session {
    private final String topic;
    private final Duration duration;
    public Session(String title, Duration duration) {
        ...
    }
    // getter/setters
}
/**
/* Venue is nothing more than an specialization of Session. So for example
/* A venue is a session of math hat starts at 3pm.
/*/
public class Venue {
    private Session session;
    private Time startTime;
    ...
}
/**
 * Class that manages the venues that happens in a WeekDay. It can control
 * that will not add two Venus that happens at same period of time.
 */
public class WeekDaySchedule {
    private WeekDay weekDay;
    private Set<Venue> venues;
    ...
}
/**
* Controls the whole week.
*/
public class WeekSchedule {
    private Collection<WeekDaySchedule> weekDaySchedules;
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you add too much to your weekday enum, you should think about a superclass or interface Weekday and implementing every weekday in his own class.

If you really want to do it in your enum, you could do it in this ways:

1) Implement the method, do a switch in every method (and constructor) like:

switch (this) {
case MONDAY:
    return ...
    break;

default:
    break;
}

2) Make the method abstract, add an anonymous implementation for every weekday:

public enum weekday {
    MONDAY {
        @Override
        public Object getSomething() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    public abstract Object getSomething();
}

3) Add the arguments for the constructor:

public enum weekday {
    MONDAY(new Object());
    final private Object object;
    private weekday(Object object) {
        this.object = object;
    }
}

But as i said, try to avoid making your enum more than an enumeration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.