Java – can I nest classes in enumerations and create ArrayLists in enumerations?

I'm trying to do that I have a week's schedule I use enumeration because weekdays are constant

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

I have a course called session Conversation is what happens at a specific time, such as math class @ H_ 404_ 5@

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 category called venue The venue holds meetings. For example, mathematics classes can be held from 9 a.m. to 10 a.m. in a venue called "mathematics class" (an example) @ h_ 404_ 5@

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 – create a list of sessions in the enumeration, that is, there are sessions every day, and then I need the structure in the site (ArrayList or enumset?) The enumeration is saved in, that is, the venue has sessions from Monday to Friday (ideally the school class) So it would be like this: @ h_ 404_ 5@

public enum WeekDay {
  MONDAY,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 in the field, we can: @ h_ 404_ 5@

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();
  ...
}

Here are my questions: @ h_ 404_ 5@

>Can I embed the session class in the enumeration? > Can enum accept ArrayLists? > What is the best structure for holding meetings in the venue? > A better idea@ H_ 404_ 5@

I was told that I would pass all venues on the same day. For example, Monday is the Monday of all venues, and each venue will update its list So even if no one commented, I think this is the end of the discussion@ H_ 404_ 5@

Solution

If you add too many workdays to the workday enumeration, you should consider a superclass or interface workday and implement each workday in his own class

If you really want to do this in your enumeration, you can do it this way: @ h_ 404_ 5@

1) Implement methods and switch in each method (and constructor), such as @ H_ 404_ 5@

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

default:
    break;
}

2) Abstract the method and add an anonymous implementation for each working day: @ h_ 404_ 5@

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

3) Add parameters to constructor: @ h_ 404_ 5@

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 enumerations more enumerative@ H_ 404_ 5@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>