How to represent time slots in a schedule
I'm writing a web application that allows users to enlist to university course开发者_如何学编程s. Courses may be given in certain time slots - a time slot is a day (in a week, i.e., Sunday, Monday, etc.) and an hour. There are fixed time slots in which courses can be given. My question is - what's the best way to implement these time slots?
One thought I had is just using an enum, but then there are 70 time slots. So I thought of having two enums - one for the week day (although I bet this already exists somewhere - do you know where I can find an existing enum of this sort?) and one for the allowed hours (for example - 8:00, 9:00, 10:00, etc.) and have a Timeslot
class hold both of these.
However, I feel that there must be a more elegant solution I haven't thought of - what do you think?
java.time
You are using troublesome old legacy classes that have now been supplanted by the java.time classes.
DayOfWeek
The java.time.DayOfWeek
class is an enum that already defines seven objects, one for each day of the week. They are numbered in standard ISO 8601 order, 1-7 for Monday-Sunday.
Make WeekTimeSlot
class
Seems that you want to represent the generic idea of a day-of-week and a time-of-day. You are not nailing down specific moments on the timeline. So you need only a LocalTime
class for start & stop times. This class lacks a date and lacks a time zone.
I suggest defining a class with at least three members:
DayOfWeek
– dowLocalTime
– startLocalTime
– stop
You may also want to keep track of the length of time for each slot rather than calculate repeatedly at runtime.
Duration
– duration
Enums are meant for a limited number of objects whose values are known at compile time. I expect your values will vary at runtime for different semesters/quarters. So not appropriate for enums.
EnumMap
If you want to group the slots by day-of-week, use an EnumMap
. This implementation of Map
is optimized for use when the key values are enum objects. You would map DayOfWeek
object to a List<WeekTimeSlot>
.
This Map
implementation takes very little memory and runs very fast.
Moment on timeline
If you need to apply these slots to a particular moment on a particular day on the actual timeline, use the LocalTime
along with a LocalDate
and a ZoneId
to get a ZonedDateTime
.
You may find helpful the Interval
class from the ThreeTen-Extra project, an extension of the java.time classes. This class represents a span of time between a pair of moments on the timeline.
Personally I think a a single enum is a perfect solution, although I agree that it's a bit ugly in this case.
I would likely implement a class with this interface:
class ScheduledTime {
int time;
HashMap days<int, string>;
Time(String day, int hour) //Inits the "time" variable with this formula: day*10 + hour - someNum.
(someNum so that hour - someNum is 0)
boolean compareTo()/equals(); (just compare time)
String getDay()// Divide by 10 to get a number from 0 to 6, then just convert to sunday-monday.
int getTime() //Mod by 10 and then add someNum.
}
Use the HashMap to convert from day nums (0 to 6) to Strings and vice versa. Instantiate it during the constructor.
I'd suggest you to create 3 enums: days (sunday, monday, etc); hours (0, 1, 2, ..., 23), minutes (0, 1, 60);
Then create class TimeEdge that holds day, hour, minutes. Then create class TimeSlot that contians to members of type TimeEdge: start and end.
The Calendar class (Java API reference) has constants for days of the week.
How best to represent the time slots depends on how you want this data to interact with the rest of your application. Personally, I would create an object that represents a course time slot, and takes an int array of available days and an int for the starting hour, an int for the starting minute (if needed), an int for the ending hour, and an int for the ending minute (if needed) in the constructor. The object would hold a list of Calendar objects that reflect the starting and ending times of the course time slot for each day the course would meet. Something like:
public Class TimeSlot {
private Calendar[] coursesStart;
private Calenddar[] coursesEnd;
public TimeSlot(int[] daysMeeting, int startHour, int startMinute, int endHour, int endMinute) {
coursesStart = new Calendar[daysMeeting.length];
coursesEnd = new Calendar[daysMeeting.length];
for (int ctr = 0; ctr < daysMeeting.length; ctr++) {
int courseDay = daysMeeting[ctr];
Calendar start = new Calendar();
start.set(Calendar.DAY_OF_WEEK, courseDay);
start.set(Calendar.HOUR, startHour);
start.set(Calendar.MINUTE, startMinute);
coursesStart[ctr] = start;
Calendar end = new Calendar();
end.set(Calendar.DAY_OF_WEEK, courseDay);
end.set(Calendar.HOUR, endHour);
end.set(Calendar.MINUTE, endMinute);
coursesEnd[ctr] = end;
}
}
}
To the first part the Java Calendar class is what you will want to use, in terms of the day of the week. I don't see this changing anytime soon. Now for the second part where the TimeSlot
also has an hour field we would create something along these lines.
class TimeSlot
{
Calendar classStart; //Holds the day and the hour for the start
Calendar classEnd; //Holds the day and the hour for the end
public TimeSlot(Calendar classStart, Calendar classEnd)
{
this.classStart = classStart;
this.classEnd = classEnd;
}
public TimeSlot(Calendar classStart, int lengthOfClassInMinutes)
{
this.classStart = classStart;
this.classEnd = classStart;
classEnd.add(Calendar.MINUTE, lengthOfClassInMinutes);
}
Now to show how this fits into the grand scheme of things we would have a class that represents classes.
public class Class
{
String title;
Instructor instructor;
int sizeOfClass;
//rest of implementation
}
Then a class to represent a scheduled class in the system.
public class ScheduledClass
{
Class classToSchedule;
TimeSlot classTimeSlot;
public ScheduledClass(Class classToSchedule, TimeSlot classTimeSlot)
{
this.classToSchedule=classToSchedule;
this.classTimeSlot=classTimeSlot;
}
}
In the previous I went against extending the Class object as it would seem that ScheduledClass could be made more generic and thus should rely on composition.
精彩评论