-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppointmentBook.java
More file actions
86 lines (78 loc) · 2.96 KB
/
AppointmentBook.java
File metadata and controls
86 lines (78 loc) · 2.96 KB
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
85
86
public class AppointmentBook {
private boolean[][] schedule;
public AppointmentBook (boolean[][] schedule)
{
this.schedule = schedule;
}
/**
* Returns true if minute in period is available for an appointment and returns
* false otherwise
* Preconditions: 1 <= period <= 8; 0 <= minute <= 59
*/
private boolean isMinuteFree(int period, int minute) {
return schedule[period-1][minute];
/* implementation not shown */ }
public void printPeriod(int period){
for (int i = 0; i < schedule[period-1].length; i++){
System.out.println(i + " " + schedule[period-1][i]);
}
}
/**
* Marks the block of minutes that starts at startMinute in period and
* is duration minutes long as reserved for an appointment
* Preconditions: 1 <= period <= 8; 0 <= startMinute <= 59;
* 1 <= duration <= 60
*/
private void reserveBlock(int period, int startMinute, int duration) {
for (int i = startMinute; i < startMinute + duration; i++){
schedule[period-1][i] = false;
}
/* implementation not shown */ }
public void printBlock(int period, int startMinute, int endMinute){
for (int i = startMinute; i <= endMinute; i++){
System.out.print(schedule[period-1][i] + " ");
}
System.out.println();
}
/**
* Searches for the first block of duration free minutes during period, as
* described in
* part (a). Returns the first minute in the block if such a block is found or
* returns -1 if no
* such block is found.
* Preconditions: 1 <= period <= 8; 1 <= duration <= 60
*/
public int findFreeBlock(int period, int duration) {
int blockLength = 0;
for (int i = 0; i<60; i++){
if (isMinuteFree(period, i)){
blockLength ++;
if(blockLength == duration){
return i - duration + 1;
}
}
else {blockLength = 0;}
}
return -1;
/* to be implemented in part (a) */ }
/**
* Searches periods from startPeriod to endPeriod, inclusive, for a block
* of duration free minutes, as described in part (b). If such a block is found,
* calls reserveBlock to reserve the block of minutes and returns true;
* otherwise
* returns false.
* Preconditions: 1 <= startPeriod <= endPeriod <= 8; 1 <= duration <= 60
*/
public boolean makeAppointment(int startPeriod, int endPeriod, int duration){
int startDuration;
for (int r = startPeriod; r <= endPeriod; r++){
startDuration = findFreeBlock(r, duration);
if (startDuration != -1){
reserveBlock(r, startDuration, duration);
return true;
}
}
return false;
/* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}