How to present time intervals in a schedule

I am writing a web application that allows users to enroll in university courses. Courses can be provided at certain time intervals - time interval - day (after a week, i.e. Sunday, Monday, etc.) And an hour. There are fixed time intervals at which courses can be provided. My question is: what is the best way to implement these time intervals?

One thought that I have is simply using an enumeration, but then there are 70 time slots. Therefore, I thought that I had two correspondence - one in a week (although I'm sure it already exists somewhere - do you know where I can find an existing listing of this kind?) And one for the allowed hours (for example - 8: 00, 9:00, 10:00, etc.) And they have a Timeslot class, both of which.

However, I feel that there should be a more elegant solution that I did not think about - what do you think?

+6
source share
5 answers

Personally, I believe that the only listing is the perfect solution, although I agree that in this case it is a little ugly.

I would most 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 the number of days (0 to 6) to strings and vice versa. Create it during the constructor.

0
source

The Calendar class ( Java API reference ) has constants for days of the week.

The best way to imagine time intervals depends on how you want this data to interact with the rest of your application. Personally, I would create an object that represents the time interval of the course, and takes an array of int days available and int for the start hour, int for the start minute (if necessary), int for the end hour and int for the end minute (if necessary) in the constructor. The object will contain a list of Calendar objects that reflect the start and end time of the course interval for each day when the course will correspond. Sort of:

 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; } } } 
0
source

First of all, the Java Calendar class is what you want to use in terms of the day of the week. I do not see this changing in the near future. Now for the second part, where TimeSlot also has an hour field, we would create something in this direction.

 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 will have a class representing the classes.

 public class Class { String title; Instructor instructor; int sizeOfClass; //rest of implementation } 

Then a class to represent the planned 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 the extension of the class object, since it would seem that ScheduledClass can be made more general and, therefore, rely on composition.

0
source

java.time

You are using nasty old legacy classes that are now superseded by java.time classes.

DayOfWeek

The java.time.DayOfWeek class is an enumeration that already defines seven objects, one for each day of the week. They are numbered in the standard order of ISO 8601, 1-7 for Monday through Sunday.

Make WeekTimeSlot class

It seems that you want to introduce the general idea of ​​the day of the week and time of day. You do not nail certain points on the timeline. Therefore, to start and stop, only the LocalTime class is LocalTime . This class has no date and no time zone.

I suggest defining a class with at least three members:

You can also track the length of time for each slot, rather than re-calculate at run time.

Enumerations are intended for a limited number of objects whose values ​​are known at compile time. I expect your values ​​to change at runtime for different semesters / quarters. Therefore, it is not suitable for transfers.

EnumMap

If you want to group slots by day of the week, use EnumMap . This Map implementation is optimized for use when key values ​​are enumerated objects. You would map a DayOfWeek object to a List<WeekTimeSlot> .

This Map implementation takes up very little memory and is very fast.

Timeline Moment

If you need to apply these slots to a specific point on a specific day on the actual timeline, use LocalTime along with LocalDate and ZoneId to get ZonedDateTime .

You can find the Interval class from ThreeTen-Extra , an extension of the java.time classes. This class represents the time interval between a pair of moments on the timeline.

0
source

I suggest you create 3 transfers: days (Sunday, Monday, etc.); hours (0, 1, 2, ..., 23), minutes (0, 1, 60);

Then create a TimeEdge class that contains the day, hour, minutes. Then create a TimeSlot class that refers to members of the TimeEdge type: start and end.

-1
source

Source: https://habr.com/ru/post/892399/


All Articles