|
10 | 10 | import seedu.duke.calendar.command.FindEventCommand;
|
11 | 11 | import seedu.duke.calendar.command.ListCalendarEventsCommand;
|
12 | 12 |
|
| 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 | + |
13 | 21 | public class CalendarCommandParser {
|
| 22 | + |
| 23 | + /** |
| 24 | + * The manageException method is used to throw exceptions if those exceptions have |
| 25 | + * been encountered. Each if case handles different exception errors. The method |
| 26 | + * has one parameter (String userInput), which is used to check if the input has |
| 27 | + * anything else after it. |
| 28 | + * @param userInput is taken to check if the condition matches the exception. |
| 29 | + * @throws AddEventException is thrown if the user only inputs add and nothing else. |
| 30 | + * @throws DeleteEventException is thrown if the user only inputs delete all and nothing else. |
| 31 | + * @throws DeleteAllException is thrown if the user only inputs delete and nothing else. |
| 32 | + * @throws FindEventException is thrown if the user only inputs find and nothing else. |
| 33 | + * @throws ListEventException is thrown if the user only inputs list and nothing else. |
| 34 | + */ |
| 35 | + |
| 36 | + public static void manageException(String userInput) throws AddEventException, DeleteEventException, |
| 37 | + DeleteAllException, FindEventException, ListEventException { |
| 38 | + |
| 39 | + Scanner input = new Scanner(userInput); |
| 40 | + String command = input.next(); |
| 41 | + |
| 42 | + if (command.equals("add") && !input.hasNext()) { |
| 43 | + throw new AddEventException(); |
| 44 | + } |
| 45 | + if (command.equals("delete all") && !input.hasNext()) { |
| 46 | + throw new DeleteAllException(); |
| 47 | + } |
| 48 | + if (command.equals("delete") && !input.hasNext()) { |
| 49 | + throw new DeleteEventException(); |
| 50 | + } |
| 51 | + if (command.equals("find") && !input.hasNext()) { |
| 52 | + throw new FindEventException(); |
| 53 | + } |
| 54 | + if (command.equals("list") && !input.hasNext()) { |
| 55 | + throw new ListEventException(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * The parseInput method is used to catch any exceptions that could occur. The method |
| 61 | + * has one parameter (String input). The input is used for asserting that it is not null. |
| 62 | + * If any exceptions are caught, then the corresponding messages are displayed. |
| 63 | + * Last resort unknown command will run if the command is not recognized. |
| 64 | + * @param input is used to check whether input is null or not. |
| 65 | + * @return runs the commands |
| 66 | + */ |
| 67 | + |
14 | 68 | public EventCommand parseInput(String input) {
|
| 69 | + // using asser to check whether the input is null. |
15 | 70 | assert input != null : "input is null";
|
16 | 71 |
|
17 |
| - if (input.startsWith("add event")) { |
18 |
| - return new AddEventCommand(input); |
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(); |
| 72 | + try { |
| 73 | + manageException(input); |
| 74 | + if (input.startsWith("add event")) { |
| 75 | + return new AddEventCommand(); |
| 76 | + } else if (input.startsWith("delete event")) { |
| 77 | + return new DeleteEventCommand(); |
| 78 | + } else if (input.startsWith("list events")) { |
| 79 | + return new ListCalendarEventsCommand(); |
| 80 | + } else if (input.startsWith("delete all events")) { |
| 81 | + return new DeleteAllEventsCommand(); |
| 82 | + } else if (input.startsWith("find event")) { |
| 83 | + return new FindEventCommand(); |
| 84 | + } |
| 85 | + |
| 86 | + } catch (AddEventException exception) { |
| 87 | + System.out.println("☹ OOPS!!! The description of an add cannot be empty."); |
| 88 | + } catch (DeleteAllException exception) { |
| 89 | + System.out.println("☹ OOPS!!! The description of a delete all cannot be empty."); |
| 90 | + } catch (DeleteEventException exception) { |
| 91 | + System.out.println("☹ OOPS!!! The description of a delete cannot be empty."); |
| 92 | + } catch (FindEventException exception) { |
| 93 | + System.out.println("☹ OOPS!!! The description of an find cannot be empty."); |
| 94 | + } catch (ListEventException exception) { |
| 95 | + System.out.println("☹ OOPS!!! The description of a list cannot."); |
27 | 96 | }
|
28 | 97 |
|
29 | 98 | return new UnknownCommand();
|
|
0 commit comments