-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.java
More file actions
144 lines (110 loc) · 3.97 KB
/
Copy pathEvent.java
File metadata and controls
144 lines (110 loc) · 3.97 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
public class Event
{
private int first; // beginning time of event
private int last; // ending time of event
private String data; // information about the event
// NOTE: time should be inputed in military time, with midnight = 2400
// as opposed to 0
public Event(int start, int end, String info)
{
first = start;
last = end;
data = info;
}
public int getStart()
{
return first;
}
public int getEnd()
{
return last;
}
public String getInfo()
{
return data;
}
// do two events conflict with each other?
public boolean conflict(Event that)
{
// if one starts where the other ends, the events don't conflict
if (that.first == last || that.last == first)
return false;
// check if the start time of other event falls between start
// and finish of this event
if (that.first >= first && that.first <= last)
return true;
// check if end time of other event falls between start and
// finish of this event
if (that.last >= first && that.last <= last)
return true;
// check if other event wraps around military clock
if (that.last < that.first) {
// if both wrap, eevents conflict
if (last < first) return true;
// if this event starts after that ends, then events conflict
else if (last > that.first) return true;
}
// check if this event wraps around military clock and
// other event ends after this starts
if (last < first && that.last > first) return true;
return false;
}
// combine two overlapping events into just one
public Event concatenate(Event that) {
int start;
int end;
String info;
// figure out which event starts first
if (first < that.first) {
start = first;
info = data + ", " + that.data;
}
else {
start = that.first;
info = that.data + ", " + data;
}
// figure out which event ends last
if (that.last < that.first && last >= first) {
end = that.last;
}
else if (last < first && that.last >= that.first) {
end = last;
}
else {
if (last > that.last) end = last;
else end = that.last;
}
return new Event(start, end, info);
}
public String toString()
{
String output = first + "-" + last + ": " + data;
return output;
}
// test method
public static void main(String[] args) {
// set up some events
Event ev0 = new Event(1300, 1500, "Rehearsal1");
Event ev1 = new Event(2200, 200, "conflict1");
Event ev2 = new Event(2100, 2300, "conflict2");
Event ev3 = new Event(1000, 1300, "conflict3");
//System.out.println(ev0);
//System.out.println(ev1);
System.out.println("Does " + ev0.getInfo() + " conflict with "
+ ev1.getInfo() + "?");
if (ev0.conflict(ev1)) System.out.println("Yes.");
else System.out.println("No.");
System.out.println("Does " + ev0.getInfo() + " conflict with "
+ ev2.getInfo() + "?");
if (ev0.conflict(ev2)) System.out.println("Yes.");
else System.out.println("No.");
System.out.println("Does " + ev0.getInfo() + " conflict with "
+ ev3.getInfo() + "?");
if (ev0.conflict(ev3)) System.out.println("Yes.");
else System.out.println("No.");
System.out.println();
if (ev1.getStart() == ev2.getEnd() || ev1.getEnd() == ev2.getStart() || ev1.conflict(ev2))
System.out.println(ev2.concatenate(ev1));
else System.out.println("not concatenated");
}
}