-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalendarClass.java
More file actions
382 lines (330 loc) · 9.74 KB
/
CalendarClass.java
File metadata and controls
382 lines (330 loc) · 9.74 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
/**
* The intent of this class is to hold all the methods used to manipulate and edit the Calendar and its Events.
* @author Ashraf Saber
* Version: 1
*/
public class CalendarClass extends GregorianCalendar{
//instance variables
static TreeMap<GregorianCalendar, TreeSet<Event>> eventsMap;
public static GregorianCalendar calendarN = new GregorianCalendar();
private static Scanner fileScanner;
String[] monthNames = {"January","February","March","April","May","June","July","August",
"September","October","November","December"};
String[] dayNames = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
/**
* Constructor for the Calendar Class
*/
public CalendarClass()
{
eventsMap = new TreeMap<>();
}
/**
* Returns the value of the following Month
* @return nextMonth
*/
public String nextMonth(GregorianCalendar calendar) {
calendar.add(Calendar.MONTH, 1);
String nextMonth= printMonth(calendar);
return nextMonth;
}
/**
* Returns the value of the previous Month
* @return previousMonth
*/
public String previousMonth(GregorianCalendar calendar) {
calendar.add(Calendar.MONTH, -1);
String previousMonth= printMonth(calendar);
return previousMonth;
}
/**
* Prints the Month Display
*
*/
public String printMonth(GregorianCalendar calendar){
int monthIndex= calendar.get(Calendar.MONTH); // retrieves the index of the month from 0-11
int yearValue= calendar.get(Calendar.YEAR); // retrieves the year value from the calendar parameter
GregorianCalendar calMonthStart = new GregorianCalendar(yearValue, monthIndex, 1);
int dayIndex = calMonthStart.get(Calendar.DAY_OF_WEEK)-1; // 1st day of month index
String d = dayNames[dayIndex]; // 1st day of month name
int daysInMonth=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);// getting the days in a month
// Printing the Month value and the Year value
System.out.println(monthNames[monthIndex]+" "+ yearValue);
System.out.println("Su Mo Tu We Th Fr Sa");
for(int j=0; j<dayIndex;j++) // j< first day of week
{
System.out.print(" ");
}
for(int i=1; i<=daysInMonth; i++)
{
String dayS;
if(i<10){dayS="0"+i;}else{dayS=String.valueOf(i);}
int monthIndx= calendar.get(Calendar.MONTH)+1;
String monthS;
if( monthIndx<10){monthS="0"+monthIndx ;}else{monthS=String.valueOf( monthIndx);}
String yearS= String.valueOf(calendar.get(Calendar.YEAR));
String dateOfi= monthS+"/"+dayS+"/"+yearS;
GregorianCalendar key=new GregorianCalendar();
key= stringToGC(dateOfi);
if(i == calendar.get(Calendar.DAY_OF_MONTH) && calendarN.get(Calendar.MONTH)==calendar.get(Calendar.MONTH))
System.out.print("["+i+"] ");
else if(eventsMap.containsKey(key))
System.out.print("{"+i+"} ");
else if(i<10)
System.out.print(i+" ");
else
System.out.print(i+" ");
if( ((dayIndex+ i)%7==0) || (i==daysInMonth) ){System.out.println("\n");}
}
String x="";
return x;
}
/**
* Returns the value of the following Day
* @return nextDay
*/
public String nextDay(GregorianCalendar calendar) {
calendar.add(Calendar.DAY_OF_WEEK, 1);
String nextDay= printDay(calendar);
return nextDay;
}
/**
* Returns the value of the previous Day
* @return previousDay
*/
public String previousDay(GregorianCalendar calendar) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
String previousDay= printDay(calendar);
return previousDay;
}
/**
* Prints the Day Display (TODAY)
*
*/
public String printDay(GregorianCalendar calendar){
String today = dayNames[calendar.get(Calendar.DAY_OF_WEEK)-1];
String monthShort = monthNames[calendar.get(Calendar.MONTH)];
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int year = calendar.get(Calendar.YEAR);
System.out.println(today+", "+monthShort+" "+ dayOfMonth+", "+year);
String calendarString = gcToString(calendar);
getEventsOnThisDay(calendarString);
String x="";
return x;
}
/**
* Function to convert a String to a GregorianCalendar object
* @param string
* @return c
*/
public GregorianCalendar stringToGC(String string){
Date dummyDate = null;
GregorianCalendar c = new GregorianCalendar();
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
try{
dummyDate = dateFormatter.parse(string);
c.setTime(dummyDate);
}
catch(ParseException e) {
System.out.println(e.getMessage());
}
return c;
}
/**
* Function to convert a GregorianCalendar to a String object
* @param calendar
* @return x
*/
public String gcToString(GregorianCalendar calendar){
String dayS;
int dayI=calendar.get(Calendar.DAY_OF_MONTH);
if(dayI<10){dayS="0"+dayI;}
else{dayS=String.valueOf(dayI);}
String monthS;
int monthI=calendar.get(Calendar.MONTH)+1;
if(monthI<10){monthS="0"+monthI;}
else{monthS=String.valueOf(monthI);}
String yearS;
int yearI=calendar.get(Calendar.YEAR);
yearS=String.valueOf(yearI);
String x=monthS+"/"+dayS+"/"+yearS;
return x;
}
/**
* Checks the eventsTree map and prints the treeSet associated with the key ()
* @return s
*/
public String getEventsOnThisDay(String date){
String s = "";
String title="",startT="",endT="";
Date dummyDate = null;
GregorianCalendar c = new GregorianCalendar();
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
try{
dummyDate = dateFormatter.parse(date);
c.setTime(dummyDate);
}
catch(ParseException e) {
System.out.println(e.getMessage());
}
// check the events map if that key (GC Object from string conversion) exists.
TreeSet<Event> events = new TreeSet<>();
//printDay(c);
if (eventsMap.containsKey(c))
{
events = eventsMap.get(c);
if (!events.equals(null))
for (Event e : events){
// s += e.toString() + "\n";
title=e.getTitle();
startT=e.getStartTime();
endT=e.getEndTime();
System.out.println(title+" "+startT+" - "+endT+"\n");
}
}
else
System.out.println("No Events Scheduled");
return s;
}
/**
* Prints all events
* @return s
*/
public String printAllEvents(){
String s = "";
String nameOfDay="", monthLong="", startT="",endT="",title="";
int dayOfMonth;
for(GregorianCalendar key : eventsMap.keySet()){
for (Event e : eventsMap.get(key)){
nameOfDay=dayNames[key.get(Calendar.DAY_OF_WEEK)-1];
monthLong=monthNames[key.get(Calendar.MONTH)];
dayOfMonth=key.get(Calendar.DAY_OF_MONTH);
startT = e.getStartTime().toString();
endT = e.getEndTime().toString();
title=e.getTitle();
System.out.println(nameOfDay+" "+monthLong+" "+dayOfMonth+" "+startT+" - "+endT+" "+title);
}
}
return s;
}
/**
* Deletes all events
*/
public void deleteAll(){
eventsMap.clear();;
}
/**
* Deletes selected events
*/
public void deleteSelected(String dateStringToDelete){
Date dummyDate = null;
GregorianCalendar key = new GregorianCalendar() ;
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
try{
dummyDate= dateFormatter.parse(dateStringToDelete);
key.setTime(dummyDate);
}
catch(ParseException e) {
System.out.println(e.getMessage());
}
eventsMap.remove(key);
}
/**
* Quits and saves events
* @throws IOException
*/
public void quit(){
String title="",date="",startT="",endT="";
try{
PrintWriter printWriter = new PrintWriter("events.txt");
for(GregorianCalendar key : eventsMap.keySet()){
for (Event e : eventsMap.get(key)){
title=e.getTitle();
date=e.getInputDate();
startT=e.getStartTime();
endT=e.getEndTime();
printWriter.println(title+" "+date+" "+startT+" "+endT);
}
}
printWriter.close();
System.exit(0);
}
catch(IOException e){System.out.println("IOException");}
}
/**
* Loads events
*/
public void load(){
try{
fileScanner = new Scanner(new File("events.txt"));
}
catch(Exception e){
System.out.println("could not find file");
}
if(fileScanner.hasNext() != true)
{
System.out.println("This is the first run");
}else
{
while(fileScanner.hasNext())
{
String titleReader = fileScanner.next();
String dateReader = fileScanner.next(); // reading Date
String startTimeReader = fileScanner.next(); // reading Start Time
String endTimeReader = fileScanner.next(); // reading End Time
createEvent(titleReader,dateReader,startTimeReader,endTimeReader);
}
}
fileScanner.close();
}
/**
* Creates an Event
* @param title
* @param inputDate
* @param startTime
* @param endTime
*/
public void createEvent(String title,String inputDate,String startTime,String endTime){
// create an event object
Event newEvent = new Event(title, inputDate, startTime, endTime);
// add event object to EventsMap
GregorianCalendar c = new GregorianCalendar();
c.setTime(newEvent.getUserInputStringToDate());
if(!checkEventCollision(newEvent)){
if(eventsMap.containsKey(c)){
eventsMap.get(c).add(newEvent); //accessing the tree set inside the tree map
} else{
TreeSet<Event> events = new TreeSet<>();
events.add(newEvent);
eventsMap.put(c, events);
}
}
}
/**
* Checks event Collision
* @param event
* @return collision
*/
public boolean checkEventCollision(Event event){
boolean collision=false;
for(GregorianCalendar key : eventsMap.keySet()){
if(key.getTime().equals(event.userInputStringToDate))
{
for(Event e : eventsMap.get(key)){
if(e.startTime.equals(event.startTime) )
{
System.out.println("Collision!!");
collision=true;
}
}
}
}
return collision;
}
}