Skip to content

Commit

Permalink
Create Parser Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Cohii2 committed Mar 7, 2024
1 parent ef5c735 commit d2948f8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
55 changes: 55 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -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<Task> 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;
}
}
}
46 changes: 3 additions & 43 deletions src/main/java/duke/list.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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!!";
Expand Down

0 comments on commit d2948f8

Please sign in to comment.