-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.java
More file actions
217 lines (195 loc) · 5.42 KB
/
Event.java
File metadata and controls
217 lines (195 loc) · 5.42 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* CS151 - HW4
* @author Thy Nguyen
*/
import java.util.Calendar;
import java.text.ParseException;
import java.util.GregorianCalendar;
enum MONTHS {
Jan, Feb, Mar, Apr, May, June, July, Aug, Sep, Oct, Nov, Dec;
}
enum DAYS {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday;
}
public class Event {
public static MONTHS[] monthArr = MONTHS.values();
public static DAYS[] dayArr = DAYS.values();
private String title;
private GregorianCalendar date;
private String startTime;
private String endTime;
/**
* To construct an Event object with String
*
* @param t
* title of the event
*/
public Event(String t) {
this.title = t;
}
/**
* To construct an Event object with String
*
* @param t
* the title of the event
* @param day
* the day of month when the event occurs
* @param mon
* the month when the event occurs
* @param year
* the year when the event occurs
*/
public Event(String t, String day, String mon, String year) {
this.title = t;
date = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(mon), Integer.parseInt(day));
}
/**
* @return the title of the event
*/
public String getTitle() {
return title;
}
/**
* To get the date when the event occurs
*
* @return the date when the event occurs
*/
public GregorianCalendar getDate() {
return date;
}
/**
* @return the start time of the event
*/
public String getStartTime() {
return startTime;
}
/**
* @return the end time of the event
*/
public String getEndTime() {
return endTime;
}
/**
* To set the start time of the event
*
* @param s
* start time string in format hh:mm
*/
public void setStartTime(String s) {
String[] time = s.split(":");
date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
date.set(Calendar.MINUTE, Integer.parseInt(time[1]));
startTime = s;
}
/**
* To set the start time of the event
*
* @param s
* end time string in format hh:mm
*/
public void setEndTime(String s) {
if(s!=null) {
try {
String[] time = s.split(":");
date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
date.set(Calendar.MINUTE, Integer.parseInt(time[1]));
}catch (NullPointerException npx) {
}
}
}
/**
* To get the string date in desired format similar to Saturday, Mar 24,
* 2018
*
* @return string of date in desired format
*/
// public String eventDate() {
// return dayArr[date.get(Calendar.DAY_OF_WEEK) - 1].toString() + ", "
// + monthArr[date.get(Calendar.MONTH)].toString() + " " + date.get(Calendar.DAY_OF_MONTH) + ", "
// + date.get(Calendar.YEAR);
// }
/**
* To get the string of title, start time and end time of the event
*/
public String toString() {
if (endTime != null)
return startTime + " - " + endTime + ": " + title;
else
return startTime + ": " + title ;
}
/**
* @return The string in desired format used for printing list of event
*/
// public String toStringForList() {
// String printEnd;
// if (endTime == null)
// printEnd = " ";
// else
// printEnd = " - " + endTime;
// return dayArr[date.get(Calendar.DAY_OF_WEEK) - 1].toString() + " "
// + monthArr[date.get(Calendar.MONTH)].toString() + " " + date.get(Calendar.DAY_OF_MONTH) + " "
// + startTime + printEnd + " " + title;
// }
/**
* To check if two events are in the same date
*
* @param e
* event that needs to be checked
* @return true if two events occur in the same date, false otherwise
*/
public boolean sameDate(Event e) {
return this.date.get(Calendar.YEAR) == e.getDate().get(Calendar.YEAR)
&& this.date.get(Calendar.MONTH) == e.getDate().get(Calendar.MONTH)
&& this.date.get(Calendar.DAY_OF_MONTH) == e.getDate().get(Calendar.DAY_OF_MONTH);
}
/**
* To check if the event conflicts time with another
*
* @param e
* event that needs to be checked
* @return true if they are conflict, false otherwise
*/
// public boolean isConflict(Event e) {
// boolean isConflict = false;
// if (this.sameDate(e)) {
// if (endTime != null) {
// if (e.getStartTime().compareTo(this.getStartTime()) >= 0
// && e.getStartTime().compareTo(this.getEndTime()) < 0)
// isConflict = true;
// else
// isConflict = e.getStartTime().compareTo(this.getStartTime()) == 0;
// }
//
// }
// System.out.println(isConflict);
// return isConflict;
// }
public boolean isConflict(Event e) {
if (!this.sameDate(e))
return false;
if (endTime != null) {
if (e.getStartTime().compareTo(this.getStartTime()) > 0
&& e.getStartTime().compareTo(this.getEndTime()) < 0)
return true;
} else
return e.getStartTime().compareTo(this.getStartTime()) == 0;
if (e.getStartTime().compareTo(this.getEndTime())==0)
return true;
else
return false;
}
/**
* To compare two events in term of starting time
*
* @param e
* event that needs to be checked
* @return -1, 0, 1 for this event start time is smaller, equal or greater
* than those of e respectively
*/
public int compareTo(Event e) {
if (this.sameDate(e))
return this.getStartTime().compareTo(e.getStartTime());
// not the same date
return this.getDate().compareTo(e.getDate());
}
}// end class