-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppController.java
More file actions
503 lines (434 loc) · 20.9 KB
/
AppController.java
File metadata and controls
503 lines (434 loc) · 20.9 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package CalendarApp;
import java.util.*;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
public class AppController {
Scanner scan = new Scanner(System.in);
HashMap<String, Calendar> newCalendars = new HashMap<>();
HashMap<String, Event> eventsList = new HashMap<>();
void mainMenu() {
System.out.println(" ");
System.out.println(" WELCOME TO CALENDAR'S APP ");
System.out.println("----------------------------------------------------------------------------------------");
System.out.println(" A: Add a Calender (Private or Public) "); // done
System.out.println(" T: Change Timezone "); // done
System.out.println(" D: Delete a Calender "); // done
System.out.println(" V: Update and View Calendar List "); // done
System.out.println(" E: Add Event "); // done
System.out.println(" M: Monthly View(dark theme/light theme) "); // done
System.out.println(" Y: Yearly View "); // work
System.out.println(" R: Remove Event "); // work
System.out.println(" U: Update and View Events "); // done
System.out.println(" H: Share Calendar with User "); // work
System.out.println(" S: Search Event "); // work
System.out.println(" Q: Quit "); // done
System.out.println("----------------------------------------------------------------------------------------");
}
void changeTimezone(User user) {
user.timeZone = "";
SimpleDateFormat simpledateformat = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
while (true) {
if (user.eventsTitleList.keySet().size() == 0) {
System.out.println("First add your events and then come back to change timezone.");
mainMenu();
break;
}
try {
System.out.print("Set the timezone ");
String timeZone = scan.nextLine();
for (String TZ : ZoneId.getAvailableZoneIds()) {
if (timeZone.equals(TZ)) {
for (Event event : user.eventsTitleList.values()) {
String dateStartString = event.details.startDate;
dateStartString += " " + event.details.timeStart;
String dateEndString = event.details.endDate;
dateEndString += " " + event.details.timeEnd;
// start date and time
Date parseStartDate = simpledateformat.parse(dateStartString);
Date parseEndDate = simpledateformat.parse(dateEndString);
simpledateformat.setTimeZone(TimeZone.getTimeZone((timeZone)));
String splitThis = simpledateformat.format(parseStartDate);
String splitter = simpledateformat.format(parseEndDate);
String[] startParts = splitThis.split(" ", 2);
event.details.startDate = startParts[0];
event.details.timeStart = startParts[1];
String[] endParts = splitter.split(" ", 2);
event.details.endDate = endParts[0];
event.details.timeEnd = endParts[1];
user.sTimezone(timeZone);
}
}
}
System.out.println("Your timezone has been changed successfully to " + user.gTimezone());
mainMenu();
break;
} catch (Exception e) {
System.out.println(e);
}
}
}
void addCalendar(User user) {
Calendar myCalendar = new GregorianCalendar();
user.addCalendar(myCalendar);
System.out.print("Set the name of this calendar: ");
String id = scan.nextLine();
user.sUserID(id);
user.calendars.put(id, myCalendar);
while (true) {
try {
System.out.print("Do you want to make this calendar Private or Public? ");
String isPrivate = scan.nextLine().toLowerCase();
if (isPrivate.equals("private") || isPrivate.equals("public")) {
user.privatelist.put(id, isPrivate);
break;
} else {
System.out.println("Select either Public or Private for calendar");
}
} catch (Exception e) {
System.out.println("Please select Private or Public for your Calendar");
}
}
System.out.println("Added calendar " + id + " (" + user.privatelist.get(id) + ")" + "\n" + "Current list: "
+ user.calendars.keySet());
mainMenu();
}
void removeCalendar(User user) {
while (true) {
boolean flag = false;
try {
if (user.calendars.keySet().size() == 0) {
System.out.println("You don't have a calendar to remove. So add calendar first");
mainMenu();
break;
}
for (String id : user.calendars.keySet()) {
System.out.println("Here are your calendars: " + user.calendars.keySet());
System.out.print("Which calendar do you want to remove: ");
String userID = scan.nextLine();
if (userID.equals(id)) {
user.sUserID(userID);
user.removeCalendar(userID);
user.calendars.remove(userID);
System.out.print("Deleted Calendar: " + userID + "\n");
flag = true;
mainMenu();
break;
} else {
System.out.println("Not a valid calendar. Try again");
}
}
if (flag == true) {
break;
}
}
catch (Exception e) {
System.out.println("Error. Try again");
}
}
}
void updateCalendarList(User user) {
System.out.println("These are your up-to-date calendar list: " + user.calendars.keySet());
if (user.calendars.keySet().size() == 0) {
System.out.println("You don't any calendars to view an updated version of.");
mainMenu();
} else {
mainMenu();
}
}
void monthlyView(User user) {
CalendarView myCalendar = new CalendarView();
while (true) {
try {
if (user.calendars.keySet().size() == 0) {
System.out.println("You don't have a calendar to view. So add calendar first");
mainMenu();
break;
}
System.out.println("These are your calendars: " + user.calendars.keySet());
System.out.println("Which calendar do you want to view in monthly view form?: ");
String cID = scan.nextLine();
user.sUserID(cID);
if (user.calendars.containsKey(cID)) {
System.out.println("----------------------------------Viewing Calendar " + cID
+ " ----------------------------------");
myCalendar.displayCalendarMonthly(cID);
System.out.println("");
mainMenu();
break;
} else {
System.out.println("Not a valid Calendar you created before");
}
} catch (Exception e) {
System.out.println("Not a valid Calendar you created before");
}
}
}
void yearlyView(User user) { // implementing still
}
void weeklyView(User user) { // implementing still
}
void dailyView(User user) { // implementing still
}
void addEventToCalendar(User user) {
EventDetails info = new EventDetails();
Event myEvent = new Event();
System.out.println("--------------------- Setting up events ---------------------");
System.out.println("Here are your calendars that you could make events for " + user.calendars.keySet());
while (true) {
try {
if (user.calendars.keySet().size() == 0) {
System.out.println("You don't have a calendar to view. So add calendar first");
mainMenu();
break;
}
while (true) {
boolean flag = false;
try {
System.out.print("Which calendar would you like to set it for? ");
String cID = scan.nextLine();
for (String ID : user.calendars.keySet()) {
if (cID.equals(ID)) {
flag = true;
info.sCalendarID(cID);
break;
}
}
if (flag == true) {
break;
}
} catch (Exception e) {
System.out.println("Not a valid Calendar you have made");
}
}
System.out.print("What do you want to name this event? ");
String title = scan.nextLine();
info.sTitle(title);
while (true) {
try {
System.out.print("What time do you want start this event?(ex. 7:15 PM/AM) ");
String timeStart = scan.nextLine();
if (timeStart.matches("^([1-9]|0[1-9]|1[0-2]):[0-5][0-9] ([AaPp][Mm])$")) {
if (isTimeValid(timeStart)) {
info.sTimeStart(timeStart);
break;
}
}
} catch (Exception e) {
System.out.println("Not a valid start time input. Try again");
}
}
while (true) {
try {
System.out.print("What time do you want to end this event?(ex. 7:15 PM/AM) ");
String timeEnd = scan.nextLine();
if (timeEnd.matches("^([1-9]|0[1-9]|1[0-2]):[0-5][0-9] ([AaPp][Mm])$")) {
if (isTimeValid(timeEnd)) {
info.sTimeEnd(timeEnd);
break;
}
}
} catch (Exception e) {
System.out.println("Enter a valid end time. Try again");
}
}
while (true) {
try {
System.out.print("What date do you want to start this event?(Ex. 06/24/1990) ");
String dateStart = scan.nextLine();
if (dateStart.matches("[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9-]{4}")) {
if (isDateValid(dateStart)) {
info.sStartDate(dateStart);
break;
}
}
} catch (Exception e) {
System.out.println("Not a valid end time input. Try again");
}
}
while (true) {
try {
System.out.print("What date do you want to end this event?(Ex. 06/24/1990) ");
String dateEnd = scan.nextLine();
if (dateEnd.matches("[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9-]{4}")) {
if (isDateValid(dateEnd)) {
info.sEndDate(dateEnd);
break;
}
}
} catch (Exception e) {
System.out.println("Enter a valid end date");
}
}
myEvent.sEventInfo(info);
user.eventsTitleList.put(title, myEvent);
mainMenu();
break;
} catch (Exception e) {
System.out.println("");
}
}
}
public boolean isTimeValid(String theTime) {
String format = "hh:mm a";
DateFormat date = new SimpleDateFormat(format);
date.setLenient(false);
try {
date.parse(theTime);
return true;
} catch (ParseException e) {
return false;
}
}
public boolean isDateValid(String theDate) {
String pattern = "MM/dd/yyyy";
DateFormat formatter = new SimpleDateFormat(pattern);
formatter.setLenient(false);
try {
formatter.parse(theDate);
return true;
} catch (ParseException e) {
return false;
}
}
void removeEvent(User user) {
while (true) {
try {
if (user.calendars.keySet().size() == 0) {
System.out.println("You don't have any calendars, so you cant view events");
mainMenu();
break;
}
if (user.eventsTitleList.keySet().size() == 0) {
System.out.println("These are your calendars: " + user.calendars.keySet());
System.out.println("You don't have any events in any of your calendars");
mainMenu();
break;
}
System.out.println("What calendar's events do you want to view? " + user.calendars.keySet());
String cID = scan.nextLine();
for (String ID : user.calendars.keySet()) {
ArrayList<String> removeEvents = new ArrayList<String>();
if (cID.equals(ID)) {
for (Event event : user.eventsTitleList.values()) {
if (event.details.calendarID.equals(cID)) {
removeEvents.add(event.details.title);
}
}
System.out.println("What event do you want to remove from this calendar? " + removeEvents);
String titleChosen = scan.nextLine();
for (String event : removeEvents) {
if (titleChosen.equals(event)) {
removeEvents.remove(titleChosen);
user.eventsTitleList.remove(titleChosen);
System.out.println("The event " + titleChosen + " has been removed.");
break;
}
}
}
}
mainMenu();
break;
}
catch (Exception e) {
System.out.println("Event does not exist");
}
}
}
void displayEvents(User user) {
while (true) {
boolean flagger = false;
try {
if (user.calendars.keySet().size() == 0) {
System.out.println("You don't have any calendars, so you cant view events");
mainMenu();
break;
}
if (user.eventsTitleList.keySet().size() == 0) {
System.out.println("These are your calendars: " + user.calendars.keySet());
System.out.println("You don't have any events in any of your calendars");
mainMenu();
break;
}
System.out.println("What calendar's events do you want to view? " + user.calendars.keySet());
String cID = scan.nextLine();
for (String ID : user.calendars.keySet()) {
ArrayList<String> events = new ArrayList<String>();
if (cID.equals(ID)) {
for (Event event : user.eventsTitleList.values()) {
if (event.details.calendarID.equals(cID)) {
events.add(event.details.title);
}
}
System.out.println("What event do you want to view? " + events);
String titleChose = scan.nextLine();
for (String title : user.eventsTitleList.keySet()) {
if (titleChose.equals(title)) {
System.out.println(
"You are now viewing Event: " + user.eventsTitleList.get(title).details.title
+ " (" + user.gTimezone() + ")");
System.out.println("The start date for " + title + " is: "
+ user.eventsTitleList.get(title).details.startDate);
System.out.println("The end date for " + title + " is: "
+ user.eventsTitleList.get(title).details.endDate);
System.out.println("The time start date for " + title + " is: "
+ user.eventsTitleList.get(title).details.timeStart);
System.out.println("The time end date for " + title + " is: "
+ user.eventsTitleList.get(title).details.timeEnd);
flagger = true;
mainMenu();
}
}
}
}
if (flagger == true) {
break;
}
} catch (Exception e) {
System.out.println("Not valid responses");
}
}
}
void choices() {
mainMenu();
User user = new User();
while (true) {
try {
System.out.print("Make a selection: ");
String command = scan.nextLine();
switch (command.toLowerCase()) {
case ("a"):
addCalendar(user);
break;
case ("t"):
changeTimezone(user);
break;
case ("d"):
removeCalendar(user);
break;
case ("m"):
monthlyView(user);
break;
case ("v"):
updateCalendarList(user);
break;
case ("e"):
addEventToCalendar(user);
break;
case ("r"):
removeEvent(user);
break;
case ("u"):
displayEvents(user);
break;
case ("q"):
System.exit(1);
}
} catch (Exception e) {
System.out.println("Not a valid option. Try again");
}
}
}
}