Skip to content

Commit 67360f0

Browse files
authored
Merge pull request #119 from Brian030601/master
Add Exceptions
2 parents 9e3a64d + 9503102 commit 67360f0

File tree

10 files changed

+111
-18
lines changed

10 files changed

+111
-18
lines changed

data/events/event.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
hello | 2023-12-20T12:30:30 | 2023-12-20T12:30:30
2-
2023-12-20T12:30:30 | 2023-12-20T12:30:30 | 2023-12-20T12:30:30
32
EC3333 | 2023-12-20T12:30:30 | 2023-12-20T12:30:40

src/main/java/seedu/duke/calendar/Calendar.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
package seedu.duke.calendar;
44

55
public class Calendar {
6+
/**
7+
* Here for integrating Flashcard goals with Calendar
8+
*/
9+
610
EventList eventList;
711
}

src/main/java/seedu/duke/calendar/CalendarCommandParser.java

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,65 @@
1010
import seedu.duke.calendar.command.FindEventCommand;
1111
import seedu.duke.calendar.command.ListCalendarEventsCommand;
1212

13+
import seedu.duke.calendar.Exceptions.AddEventException;
14+
import seedu.duke.calendar.Exceptions.DeleteEventException;
15+
import seedu.duke.calendar.Exceptions.DeleteAllException;
16+
import seedu.duke.calendar.Exceptions.FindEventException;
17+
import seedu.duke.calendar.Exceptions.ListEventException;
18+
19+
import java.util.Scanner;
20+
1321
public class CalendarCommandParser {
22+
public static void manageException(String userInput) throws AddEventException, DeleteEventException,
23+
DeleteAllException, FindEventException, ListEventException {
24+
25+
Scanner input = new Scanner(userInput);
26+
String command = input.next();
27+
28+
if (command.equals("add") && !input.hasNext()) {
29+
throw new AddEventException();
30+
}
31+
if (command.equals("delete all") && !input.hasNext()) {
32+
throw new DeleteAllException();
33+
}
34+
if (command.equals("delete") && !input.hasNext()) {
35+
throw new DeleteEventException();
36+
}
37+
if (command.equals("find") && !input.hasNext()) {
38+
throw new FindEventException();
39+
}
40+
if (command.equals("list") && !input.hasNext()) {
41+
throw new ListEventException();
42+
}
43+
}
44+
1445
public EventCommand parseInput(String input) {
1546
assert input != null : "input is null";
1647

17-
if (input.startsWith("add event")) {
18-
return new AddEventCommand();
19-
} else if (input.startsWith("delete event")) {
20-
return new DeleteEventCommand();
21-
} else if (input.startsWith("list events")) {
22-
return new ListCalendarEventsCommand();
23-
} else if (input.startsWith("delete all events")) {
24-
return new DeleteAllEventsCommand();
25-
} else if (input.startsWith("find event")) {
26-
return new FindEventCommand();
48+
try {
49+
manageException(input);
50+
if (input.startsWith("add event")) {
51+
return new AddEventCommand();
52+
} else if (input.startsWith("delete event")) {
53+
return new DeleteEventCommand();
54+
} else if (input.startsWith("list events")) {
55+
return new ListCalendarEventsCommand();
56+
} else if (input.startsWith("delete all events")) {
57+
return new DeleteAllEventsCommand();
58+
} else if (input.startsWith("find event")) {
59+
return new FindEventCommand();
60+
}
61+
62+
} catch (AddEventException exception) {
63+
System.out.println("☹ OOPS!!! The description of an add cannot be empty.");
64+
} catch (DeleteAllException exception) {
65+
System.out.println("☹ OOPS!!! The description of a delete all cannot be empty.");
66+
} catch (DeleteEventException exception) {
67+
System.out.println("☹ OOPS!!! The description of a delete cannot be empty.");
68+
} catch (FindEventException exception) {
69+
System.out.println("☹ OOPS!!! The description of an find cannot be empty.");
70+
} catch (ListEventException exception) {
71+
System.out.println("☹ OOPS!!! The description of a list cannot.");
2772
}
2873

2974
return new UnknownCommand();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package seedu.duke.calendar.Exceptions;
2+
3+
public class AddEventException extends Exception {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package seedu.duke.calendar.Exceptions;
2+
3+
public class DeleteAllException extends Exception {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package seedu.duke.calendar.Exceptions;
2+
3+
public class DeleteEventException extends Exception {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package seedu.duke.calendar.Exceptions;
2+
3+
public class EventTimeErrorException extends Exception {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package seedu.duke.calendar.Exceptions;
2+
3+
public class FindEventException extends Exception {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package seedu.duke.calendar.Exceptions;
2+
3+
public class ListEventException extends Exception {
4+
5+
}
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
1-
//@@ kherlenbayasgalan & Cheezeblokz
1+
//@@ kherlenbayasgalan
22

33
package seedu.duke.calendar.command;
44

5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeFormatter;
7+
58
import seedu.duke.calendar.Event;
69
import seedu.duke.calendar.EventList;
710

11+
import java.time.format.DateTimeParseException;
12+
813
import java.util.Scanner;
9-
import java.time.LocalDateTime;
1014

1115
public class AddEventCommand extends EventCommand {
16+
1217
public void execute(Scanner scanner, EventList eventList) {
1318
System.out.print("What's the event?: ");
1419
String eventName = scanner.nextLine();
15-
System.out.print("When does it start? (yyyy-mm-ddThh:mm:ss) (eg. 2023-12-20T12:30:30): ");
16-
LocalDateTime startTime = LocalDateTime.parse(scanner.nextLine());
17-
System.out.print("When does it end? (yyyy-mm-ddThh:mm:ss) (eg. 2023-12-20T12:30:30): ");
18-
LocalDateTime endTime = LocalDateTime.parse(scanner.nextLine());
20+
21+
LocalDateTime startTime = parseDateTimeInput(scanner, "When does it start? (yyyy-MM-ddTHH:mm:ss) (e.g., 2023-12-20T12:30:30): ");
22+
LocalDateTime endTime = parseDateTimeInput(scanner, "When does it end? (yyyy-MM-ddTHH:mm:ss) (e.g., 2023-12-20T12:30:30): ");
1923

2024
if (endTime.isAfter(startTime)) {
2125
Event event = new Event(eventName, startTime, endTime);
2226
eventList.addEvent(event);
2327
System.out.println(event + " has been added to your Calendar");
2428
} else {
25-
System.out.println(" End time is wrong. Enter the correct end time");
29+
System.out.println(" End time is before or equal to the start time. Please enter the correct end time.");
2630
}
31+
}
2732

33+
private LocalDateTime parseDateTimeInput(Scanner scanner, String prompt) {
34+
while (true) {
35+
System.out.print(prompt);
36+
String userInput = scanner.nextLine();
37+
try {
38+
return LocalDateTime.parse(userInput, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
39+
} catch (DateTimeParseException e) {
40+
System.out.println(" Invalid date and time format. Please try again.");
41+
}
42+
}
2843
}
2944
}

0 commit comments

Comments
 (0)