forked from nus-cs2103-AY2122S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
70 lines (58 loc) · 2.08 KB
/
Parser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package seedu.duke;
import java.util.HashMap;
import seedu.commands.ByeCommand;
import seedu.commands.Command;
import seedu.commands.DeadlineCommand;
import seedu.commands.DeleteCommand;
import seedu.commands.EventCommand;
import seedu.commands.FindCommand;
import seedu.commands.ListCommand;
import seedu.commands.MarkCommand;
import seedu.commands.PriorityCommand;
import seedu.commands.TodoCommand;
import seedu.commands.UnmarkCommand;
/**
* The Parser class
*/
public class Parser {
private HashMap<String, Command> converter = new HashMap<>();
/**
* A dictionary converting string to their appropriate commands
*/
public Parser() {
converter.put("bye", new ByeCommand());
converter.put("todo", new TodoCommand());
converter.put("list", new ListCommand());
converter.put("find", new FindCommand());
converter.put("mark", new MarkCommand());
converter.put("event", new EventCommand());
converter.put("delete", new DeleteCommand());
converter.put("unmark", new UnmarkCommand());
converter.put("deadline", new DeadlineCommand());
converter.put("priority", new PriorityCommand());
}
/**
* Find and returns the appropriate command based on user input
*
* @param fullCmd The entire user input
* @return A command object corresponding to the input command type
* @throws DukeException There is no command or the command input has the wrong type
*/
public Command parse(String fullCmd) throws DukeException {
String[] cmds = fullCmd.trim().split(" ", 2);
if (cmds.length == 0) {
throw new DukeException("There is no command.");
}
if (!converter.containsKey(cmds[0].trim())) {
throw new DukeException("Wrong command.");
}
Command cmd = converter.get(cmds[0].trim());
if (cmds.length == 2) {
cmd.validate(cmds[1].trim());
} else {
assert cmds.length == 1 : "Array not suppose to contain 2 items";
cmd.validate("");
}
return cmd;
}
}