forked from nus-cs2113-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a7ba07
commit 77d2c4d
Showing
1 changed file
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |