Skip to content

Commit

Permalink
Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierLiau committed Mar 13, 2024
1 parent 0a7ba07 commit 77d2c4d
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/main/java/seedu/binbash/Parser.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
package seedu.binbash;public class Parser {
package seedu.binbash;

import java.util.regex.Matcher;

import seedu.binbash.command.Command;
import seedu.binbash.command.DeleteCommand;
import seedu.binbash.command.ListCommand;

public class Parser {
private final ItemList itemList;

public Parser(ItemList itemList) {
this.itemList = itemList;
}

public Command parseCommand(String userInput) {
String[] tokens = userInput.trim().split("\\s+", 2);
String commandString = tokens[0].toLowerCase();
String arguments = tokens.length > 1 ? tokens[1] : "";

switch (commandString) {
case "delete":
return parseDeleteCommand(arguments);
case "list":
return parseListCommand(arguments);
default:
return null;
}
}

private Command parseDeleteCommand(String arguments) {
Matcher matcher = DeleteCommand.COMMAND_FORMAT.matcher(arguments);
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group("index"));
return new DeleteCommand(itemList, index);
} else {
return null;
}
}

private Command parseListCommand(String arguments) {
return new ListCommand(itemList);
}
}

0 comments on commit 77d2c4d

Please sign in to comment.