-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay.java
More file actions
126 lines (99 loc) · 3.28 KB
/
Copy pathDay.java
File metadata and controls
126 lines (99 loc) · 3.28 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.*;
import java.io.*;
public class Day
{
private Queue<Event> conflicts; // collection of events during the day
private String name; // name of the day of week
// NOTE: name should equal a day of the week, and nothing else
public Day(String name)
{
conflicts = new LinkedList<Event>();
this.name = name;
}
// adds an event to the collection, and if it conflicts with another event,
// both events are concatenated together
public void addEvent(Event ev)
{
Event event = ev;
Object[] events = conflicts.toArray();
int length = events.length;
// go through the existing list of conflicts
for (int i = 0; i < length; i++) {
Event e = (Event) events[i];
// if the inputed event conflicts with an already existent conflict,
// concatenate the two events and remove the already existent conflict
// from the day's list of conflicts
if (event.getStart() == e.getEnd() || event.getEnd() == e.getStart() || event.conflict(e)) {
event = event.concatenate(e);
conflicts.remove(e);
}
}
// add the event to the day's list of events
conflicts.add(event);
}
// function used if making a rehearsal schedule
// just adds rehearsal time to list, since rehearsals shouldn't
// be concatenated
public void addRehearsal(Event ev) {
conflicts.add(ev);
}
// this function is to make the casting process easier
public boolean addEvents(Collection<Event> events) {
return conflicts.addAll(events);
}
public boolean equals(Day that) {
return name.equals(that.name);
}
public String name() {
return name;
}
public boolean contains(Event ev) {
return conflicts.contains(ev);
}
public boolean remove(Event ev) {
return conflicts.remove(ev);
}
public boolean isEmpty() {
return conflicts.isEmpty();
}
public Queue<Event> allEvents() {
return conflicts;
}
// output events of day; one event per line
public void show()
{
System.out.println(name + ": ");
System.out.println();
for (Event ev : conflicts) {
System.out.println(ev.toString());
}
System.out.println();
}
public String toString() {
String output = name + ":\n\n";
for (Event ev : conflicts) {
output += ev.toString();
output += "\n";
}
output += "\n";
return output;
}
// test method
public static void main(String[] args) {
// set up some events
Event ev1 = new Event(1700, 2000, "conflict1");
Event ev2 = new Event(2200, 200, "conflict2");
Event ev3 = new Event(1300, 1500, "conflict3");
Event ev = new Event(2100, 100, "conflict");
// Add to the same day
Day tues = new Day("Tuesday");
tues.addEvent(ev1);
tues.addEvent(ev2);
tues.addEvent(ev3);
tues.addEvent(ev);
// Output the events for the Day
tues.show();
Day tues2 = new Day("Wednesday");
System.out.println(tues.equals(tues2));
}
}