-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay.java
84 lines (73 loc) · 2.28 KB
/
Day.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package uk.ac.aston.oop.calendar;
/**
* Maintain the appointments for one day in a diary.
*/
public class Day {
// The first and final bookable hours in a day.
public static final int START_OF_DAY = 9;
public static final int FINAL_APPOINTMENT_TIME = 17;
// The number of bookable hours in a day.
public static final int MAX_APPOINTMENTS_PER_DAY = FINAL_APPOINTMENT_TIME - START_OF_DAY + 1;
// A day number within a particular year. (1-366)
private int dayNumber;
// The current list of appointments for this day.
private Appointment[] appointments;
/**
* Constructor for objects of class Day.
*
* @param dayNumber The number of this day in the year (1-366).
*/
public Day(int dayNumber) {
this.dayNumber = dayNumber;
appointments = new Appointment[MAX_APPOINTMENTS_PER_DAY];
}
/**
* Make an appointment.
*
* @param time The hour at which the appointment starts.
* @param appointment The appointment to be made.
* @return true if the appointment was successful, false otherwise.
*/
public boolean makeAppointment(int time, Appointment appointment) {
// TODO complete this function!
for(int i = 0; i <appointment.getDuration(); i++) {
if(!(validTime(time + i))) {
return false;
}
//if (validTime(time) && appointments [time - START_OF_DAY] == null) {
if(appointments [time - START_OF_DAY + i] != null) {
return false;
}
}
for(int i = 0; i <appointment.getDuration(); i++) {
appointments [time - START_OF_DAY + i] = appointment;
}
return true;
}
/**
* @param time Which time of day. This must be between the START_OF_DAY time and
* the FINAL_APPOINTMENT_TIME.
* @return The Appointment at the given time. null is returned if either the
* time is invalid or there is no Appointment at the given time.
*/
public Appointment getAppointment(int time) {
if (validTime(time)) {
return appointments[time - START_OF_DAY];
} else {
return null;
}
}
/**
* @return The number of this day within the year (1 - 366).
*/
public int getDayNumber() {
return dayNumber;
}
/**
* @return true if the time is between FINAL_APPOINTMENT_TIME and END_OF_DAY,
* false otherwise.
*/
public boolean validTime(int time) {
return time >= START_OF_DAY && time <= FINAL_APPOINTMENT_TIME;
}
}