Skip to content

Commit

Permalink
Parser: extract methods from parse method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrechuakj committed Sep 18, 2023
1 parent e0ee922 commit 9be0400
Showing 1 changed file with 160 additions and 117 deletions.
277 changes: 160 additions & 117 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import duke.task.Task;
import duke.task.Todo;


/**
* The Parser class is responsible for parsing user input and converting data strings
* into task objects for the Duke application.
Expand All @@ -33,135 +32,179 @@ public class Parser {
public static Command parse(String strCommand) throws DukeException {
assert strCommand != null : "Command should not be null";
int firstSpaceIndex = strCommand.indexOf(" ");
Command command = null;
String commandType = firstSpaceIndex != -1
? strCommand.substring(0, firstSpaceIndex)
: strCommand;
ArrayList<String> commandDetailList = new ArrayList<>();
switch (commandType) {
case "list":
commandDetailList.add(strCommand);
command = new ListCommand(commandDetailList);
break;
case "bye":
commandDetailList.add(strCommand);
command = new ByeCommand(commandDetailList);
break;
case "todo":
if (firstSpaceIndex == -1 || strCommand.length() < 6) {
throw new DukeException("OOPS!!! The description of a todo cannot be empty.");
}
String todoDesc = strCommand.substring(firstSpaceIndex + 1);
commandDetailList.add(todoDesc);
command = new AddCommand(commandDetailList, "T");
break;
case "event":
int fromIndex = strCommand.indexOf("/from");
int toIndex = strCommand.indexOf("/to");
if (firstSpaceIndex == -1 || fromIndex == -1 || toIndex == -1 || toIndex < fromIndex) {
throw new DukeException("OOPS!!! The format of the event command is invalid.\n"
+ "Here is an example of a valid format:"
+ " event coding /from 2023-01-01 /to 2023-12-31");
}
String eventDesc = strCommand.substring(firstSpaceIndex + 1, fromIndex - 1);
String from = strCommand.substring(fromIndex + "/from ".length(), toIndex - 1);
String to = strCommand.substring(toIndex + "/to ".length());
if (eventDesc.isBlank() || from.isBlank() || to.isBlank()) {
throw new DukeException("OOPS!!! The format of the event command is invalid.\n"
+ "Here is an example of a valid format:"
+ " event coding /from 2023-01-01 /to 2023-12-31");
}
commandDetailList.add(eventDesc);
commandDetailList.add(from);
commandDetailList.add(to);
command = new AddCommand(commandDetailList, "E");
break;
case "deadline":
int byIndex = strCommand.indexOf("/by");
if (byIndex == -1 || firstSpaceIndex == -1) {
throw new DukeException("OOPS!!! The format of the deadline command is invalid.\n"
+ "Here is an example of a valid format:"
+ " deadline coding /by 2023-09-04");
}
String deadlineDesc = strCommand.substring(firstSpaceIndex + 1, byIndex - 1);
String by = strCommand.substring(byIndex + "/by ".length());
if (deadlineDesc.isBlank() || by.isBlank()) {
throw new DukeException("OOPS!!! The format of the deadline command is invalid.\n"
+ "Here is an example of a valid format:"
+ " deadline coding /by 2023-09-04");
}
commandDetailList.add(deadlineDesc);
commandDetailList.add(by);
command = new AddCommand(commandDetailList, "D");
break;
case "mark":
if (firstSpaceIndex == -1 || strCommand.length() < "mark ".length()) {
throw new DukeException("OOPS!!! The task number to mark cannot be empty.");
}
String taskToMark = strCommand.substring(firstSpaceIndex + 1);
if (taskToMark.isBlank()) {
throw new DukeException("OOPS!!! The task number to mark cannot be empty.");
}
commandDetailList.add(taskToMark);
command = new MarkCommand(commandDetailList);
break;
case "unmark":
if (firstSpaceIndex == -1 || strCommand.length() < "unmark ".length()) {
throw new DukeException("OOPS!!! The task number to unmark cannot be empty.");
}
String taskToUnmark = strCommand.substring(firstSpaceIndex + 1);
if (taskToUnmark.isBlank()) {
throw new DukeException("OOPS!!! The task number to unmark cannot be empty.");
}
commandDetailList.add(taskToUnmark);
command = new UnmarkCommand(commandDetailList);
break;
return parseAddDeadlineCommand(strCommand);
case "event":
return parseAddEventCommand(strCommand);
case "todo":
return parseAddTodoCommand(strCommand);
case "bye":
return parseByeCommand(strCommand);
case "delete":
if (firstSpaceIndex == -1 || strCommand.length() < "delete ".length()) {
throw new DukeException("OOPS!!! The task number to delete cannot be empty.");
}
String taskToDelete = strCommand.substring(firstSpaceIndex + 1);
if (taskToDelete.isBlank()) {
throw new DukeException("OOPS!!! The task number to delete cannot be empty.");
}
commandDetailList.add(taskToDelete);
command = new DeleteCommand(commandDetailList);
break;
return parseDeleteCommand(strCommand);
case "find":
if (firstSpaceIndex == -1 || strCommand.length() < "find ".length()) {
throw new DukeException("OOPS!!! The find keyword cannot be empty.");
}
String keyword = strCommand.substring(firstSpaceIndex + 1);
if (keyword.isBlank()) {
throw new DukeException("OOPS!!! The find keyword cannot be empty.");
}
commandDetailList.add(keyword);
command = new FindCommand(commandDetailList);
break;
return parseFindCommand(strCommand);
case "list":
return parseListCommand(strCommand);
case "mark":
return parseMarkCommand(strCommand);
case "unmark":
return parseUnmarkCommand(strCommand);
case "update":
int secondSpaceIndex = strCommand.indexOf(" ", firstSpaceIndex + 1);
int slashIndex = strCommand.indexOf("/", secondSpaceIndex);
int thirdSpaceIndex = strCommand.indexOf(" ", slashIndex);
if (firstSpaceIndex == -1 || strCommand.length() < "update ".length()) {
throw new DukeException("OOPS!!! The update details cannot be empty.");
} else if (slashIndex == -1 || secondSpaceIndex == -1) {
throw new DukeException("OOPS!!! Please use the format: update 1 /desc newName");
}
String taskNumber = strCommand.substring(firstSpaceIndex + 1, secondSpaceIndex);
String field = strCommand.substring(slashIndex + 1, thirdSpaceIndex);
String details = strCommand.substring(thirdSpaceIndex + 1);
commandDetailList.add(taskNumber);
commandDetailList.add(field);
commandDetailList.add(details);
System.out.println(field);
command = new UpdateCommand(commandDetailList);
break;
return parseUpdateCommand(strCommand);
default:
throw new DukeException("OOPS!!! This command is invalid.");
}
return command;
}

private static AddCommand parseAddDeadlineCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
int byIndex = strCommand.indexOf("/by");
String exception = "OOPS!!! The format of the deadline command is invalid.\n"
+ "Here is an example of a valid format:"
+ " deadline coding /by 2023-09-04";
if (byIndex == -1 || firstSpaceIndex == -1) {
throw new DukeException(exception);
}
String deadlineDesc = strCommand.substring(firstSpaceIndex + 1, byIndex - 1);
String by = strCommand.substring(byIndex + "/by ".length());
if (deadlineDesc.isBlank() || by.isBlank()) {
throw new DukeException(exception);
}
commandDetailList.add(deadlineDesc);
commandDetailList.add(by);
return new AddCommand(commandDetailList, "D");
}

private static AddCommand parseAddEventCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
int fromIndex = strCommand.indexOf("/from");
int toIndex = strCommand.indexOf("/to");
String exception = "OOPS!!! The format of the event command is invalid.\n"
+ "Here is an example of a valid format:"
+ " event coding /from 2023-01-01 /to 2023-12-31";
if (firstSpaceIndex == -1 || fromIndex == -1 || toIndex == -1 || toIndex < fromIndex) {
throw new DukeException(exception);
}
String eventDesc = strCommand.substring(firstSpaceIndex + 1, fromIndex - 1);
String from = strCommand.substring(fromIndex + "/from ".length(), toIndex - 1);
String to = strCommand.substring(toIndex + "/to ".length());
if (eventDesc.isBlank() || from.isBlank() || to.isBlank()) {
throw new DukeException(exception);
}
commandDetailList.add(eventDesc);
commandDetailList.add(from);
commandDetailList.add(to);
return new AddCommand(commandDetailList, "E");
}

private static AddCommand parseAddTodoCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
if (firstSpaceIndex == -1 || strCommand.length() < 6) {
throw new DukeException("OOPS!!! The description of a todo cannot be empty.");
}
String todoDesc = strCommand.substring(firstSpaceIndex + 1);
ArrayList<String> commandDetailList = new ArrayList<>();
commandDetailList.add(todoDesc);
return new AddCommand(commandDetailList, "T");
}

private static ByeCommand parseByeCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
commandDetailList.add(strCommand);
return new ByeCommand(commandDetailList);
}

private static DeleteCommand parseDeleteCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
if (firstSpaceIndex == -1 || strCommand.length() < "delete ".length()) {
throw new DukeException("OOPS!!! The task number to delete cannot be empty.");
}
String taskToDelete = strCommand.substring(firstSpaceIndex + 1);
if (taskToDelete.isBlank()) {
throw new DukeException("OOPS!!! The task number to delete cannot be empty.");
}
commandDetailList.add(taskToDelete);
return new DeleteCommand(commandDetailList);
}

private static FindCommand parseFindCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
if (firstSpaceIndex == -1 || strCommand.length() < "find ".length()) {
throw new DukeException("OOPS!!! The find keyword cannot be empty.");
}
String keyword = strCommand.substring(firstSpaceIndex + 1);
if (keyword.isBlank()) {
throw new DukeException("OOPS!!! The find keyword cannot be empty.");
}
commandDetailList.add(keyword);
return new FindCommand(commandDetailList);
}

private static ListCommand parseListCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
commandDetailList.add(strCommand);
return new ListCommand(commandDetailList);
}

private static MarkCommand parseMarkCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
if (firstSpaceIndex == -1 || strCommand.length() < "mark ".length()) {
throw new DukeException("OOPS!!! The task number to mark cannot be empty.");
}
String taskToMark = strCommand.substring(firstSpaceIndex + 1);
if (taskToMark.isBlank()) {
throw new DukeException("OOPS!!! The task number to mark cannot be empty.");
}
commandDetailList.add(taskToMark);
return new MarkCommand(commandDetailList);
}

private static UnmarkCommand parseUnmarkCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
if (firstSpaceIndex == -1 || strCommand.length() < "unmark ".length()) {
throw new DukeException("OOPS!!! The task number to unmark cannot be empty.");
}
String taskToUnmark = strCommand.substring(firstSpaceIndex + 1);
if (taskToUnmark.isBlank()) {
throw new DukeException("OOPS!!! The task number to unmark cannot be empty.");
}
commandDetailList.add(taskToUnmark);
return new UnmarkCommand(commandDetailList);
}

private static UpdateCommand parseUpdateCommand(String strCommand) throws DukeException {
int firstSpaceIndex = strCommand.indexOf(" ");
ArrayList<String> commandDetailList = new ArrayList<>();
int secondSpaceIndex = strCommand.indexOf(" ", firstSpaceIndex + 1);
int slashIndex = strCommand.indexOf("/", secondSpaceIndex);
int thirdSpaceIndex = strCommand.indexOf(" ", slashIndex);
if (firstSpaceIndex == -1 || strCommand.length() < "update ".length()) {
throw new DukeException("OOPS!!! The update details cannot be empty.");
} else if (slashIndex == -1 || secondSpaceIndex == -1) {
throw new DukeException("OOPS!!! Please use the format: update 1 /desc newName");
}
String taskNumber = strCommand.substring(firstSpaceIndex + 1, secondSpaceIndex);
String field = strCommand.substring(slashIndex + 1, thirdSpaceIndex);
String details = strCommand.substring(thirdSpaceIndex + 1);
commandDetailList.add(taskNumber);
commandDetailList.add(field);
commandDetailList.add(details);
System.out.println(field);
return new UpdateCommand(commandDetailList);
}
/**
* Converts a data string into a Task object.
*
Expand Down

0 comments on commit 9be0400

Please sign in to comment.