From d2948f8ed7278c1103633adde842067aae7f66e5 Mon Sep 17 00:00:00 2001 From: Cohii Date: Fri, 8 Mar 2024 02:02:02 +0800 Subject: [PATCH] Create Parser Class --- src/main/java/duke/Parser.java | 55 ++++++++++++++++++++++++++++++++++ src/main/java/duke/list.java | 46 ++-------------------------- 2 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 src/main/java/duke/Parser.java diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java new file mode 100644 index 000000000..0fe08fe74 --- /dev/null +++ b/src/main/java/duke/Parser.java @@ -0,0 +1,55 @@ +package duke; + +import java.util.Arrays; +import java.util.List; + +public class Parser { + public static final String[] validCommands = + {"list", "mark", "unmark", "todo", "deadline", "event", "bye", "delete"}; + protected TaskList taskList; + + public Parser(TaskList taskList) { + this.taskList = taskList; + } + + public List getList() { + return taskList.getTaskList(); + } + + public void executeCommand(String command, String argument) + throws MissingParamsException, DukeException.EndListException, + DukeException.InvalidCommandException, DukeException.InvalidIntegerException, + DukeException.IntegerOutOfBoundsException { + if (command.equalsIgnoreCase("bye")) { + throw new DukeException.EndListException(); + } + + if (Arrays.stream(validCommands).noneMatch(command::equals)){ + throw new DukeException.InvalidCommandException(); + } + + switch (command) { + case "list": + taskList.printList(); + break; + case "mark": + taskList.markTask(argument, true); + break; + case "unmark": + taskList.markTask(argument, false); + break; + case "todo": + taskList.addToDo(argument); + break; + case "deadline": + taskList.addDeadline(argument); + break; + case "event": + taskList.addEvent(argument); + break; + case "delete": + taskList.deleteTask(argument); + break; + } + } +} diff --git a/src/main/java/duke/list.java b/src/main/java/duke/list.java index 37867c538..3eb51a4ab 100644 --- a/src/main/java/duke/list.java +++ b/src/main/java/duke/list.java @@ -9,46 +9,6 @@ import static duke.TaskList.*; public class list { - public static final String[] validCommands = - {"list", "mark", "unmark", "todo", "deadline", "event", "bye", "delete"}; - - public static void executeCommand(TaskList taskList, String command, String argument) - throws MissingParamsException, DukeException.EndListException, - DukeException.InvalidCommandException, DukeException.InvalidIntegerException, - DukeException.IntegerOutOfBoundsException { - if (command.equalsIgnoreCase("bye")) { - throw new DukeException.EndListException(); - } - - if (Arrays.stream(validCommands).noneMatch(command::equals)){ - throw new DukeException.InvalidCommandException(); - } - - switch (command) { - case "list": - taskList.printList(); - break; - case "mark": - taskList.markTask(argument, true); - break; - case "unmark": - taskList.markTask(argument, false); - break; - case "todo": - taskList.addToDo(argument); - break; - case "deadline": - taskList.addDeadline(argument); - break; - case "event": - taskList.addEvent(argument); - break; - case "delete": - taskList.deleteTask(argument); - break; - } - } - /** * Creates a list that users can add tasks to, read, and mark tasks as done or undone. */ @@ -63,7 +23,7 @@ public static void startList(){ throw new RuntimeException(); } - TaskList taskList = new TaskList(list); + Parser parser = new Parser(new TaskList(list)); String line; Scanner in = new Scanner(System.in); @@ -82,7 +42,7 @@ public static void startList(){ } try { - executeCommand(taskList, command, argument); + parser.executeCommand(command, argument); } catch (DukeException.EndListException e) { break; } catch (DukeException.InvalidCommandException e) { @@ -100,7 +60,7 @@ public static void startList(){ } try { - storage.saveTasks(taskList.getTaskList()); + storage.saveTasks(parser.getList()); } catch (IOException e) { String errorMessage = "Failed to save existing tasks to database!!\n" + "GO!! GO!!";