Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent time slots in a schedule

Tags:

java

oop

I'm writing a web application that allows users to enlist to university courses. 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?

like image 534
Amir Rachum Avatar asked Jul 09 '11 15:07

Amir Rachum


People also ask

What is a slot schedule?

Slot-based scheduling refers to the process of establishing time periods for completing specific activities. These time periods or time slots establish a period during which businesses and organizations conduct meetings, hold appointments or perform certain processes like product manufacturing.

What does time slot interval mean?

The time slot intervals indicate how each hour is divided up in the calendar; therefore a smaller interval will mean lots of little slices, while a larger interval will mean a smaller amount of bigger chunks.


1 Answers

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 – dow
  • LocalTime – start
  • LocalTime – 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.

like image 191
Basil Bourque Avatar answered Oct 06 '22 00:10

Basil Bourque