22
33import java .util .HashMap ;
44
5- import javafx .util .Pair ;
6-
75import duke .ui .Ui ;
86import duke .tasks .TaskList ;
97import duke .storage .Storage ;
10- import duke .parsers .DateTimeParser ;
8+ import duke .parsers .CommandParser ;
119import duke .exceptions .DukeException ;
1210
1311/**
14- * Parses user-entered strings and executes them as Commands.
15- * Remains active until user deactivates with "bye"
12+ * Parses user-entered strings and executes them as Commands. Remains active
13+ * until user deactivates with "bye"
1614 */
1715public class CommandHandler {
1816 private TaskList tasks ;
1917 private Ui ui ;
2018 private Storage storage ;
2119 private HashMap <String , Command > commands ;
20+ private CommandParser commandParser ;
2221 private boolean isActive ;
2322
2423 /**
2524 * Creates a CommandHandler.
26- * @param tasks List of tasks.
27- * @param ui User interface.
25+ *
26+ * @param tasks List of tasks.
27+ * @param ui User interface.
2828 * @param storage Persistent storage.
2929 */
3030 public CommandHandler (TaskList tasks , Ui ui , Storage storage ) {
3131 this .tasks = tasks ;
3232 this .ui = ui ;
3333 this .storage = storage ;
3434 this .commands = new HashMap <>();
35+ this .commandParser = new CommandParser ();
3536 isActive = true ;
3637
37- // Create parsers
38- DateTimeParser dtParser = new DateTimeParser ();
39-
4038 // Register commands
41- commands .put ("list" , new ListAll ());
42- commands .put ("done" , new MarkTaskAsDone ());
43- commands .put ("todo" , new CreateTodo ());
44- commands .put ("deadline" , new CreateDeadline (dtParser ));
45- commands .put ("event" , new CreateEvent (dtParser ));
46- commands .put ("delete" , new DeleteTask ());
47- commands .put ("find" , new FindTasks ());
48- commands .put ("reschedule" , new RescheduleTask (dtParser ));
39+ commands .put ("list" , new ListAll (commandParser ));
40+ commands .put ("done" , new MarkTaskAsDone (commandParser ));
41+ commands .put ("todo" , new CreateTodo (commandParser ));
42+ commands .put ("deadline" , new CreateDeadline (commandParser ));
43+ commands .put ("event" , new CreateEvent (commandParser ));
44+ commands .put ("delete" , new DeleteTask (commandParser ));
45+ commands .put ("find" , new FindTasks (commandParser ));
46+ commands .put ("reschedule" , new RescheduleTask (commandParser ));
47+ }
48+
49+ public boolean isActive () {
50+ return isActive ;
4951 }
5052
5153 /**
@@ -61,28 +63,16 @@ public void handleCommand(String command) {
6163 }
6264
6365 // Extract out command word and arguments
64- Pair <String , String > commandAndArg = getCommandAndArg (command );
65- String commandWord = commandAndArg .getKey ();
66- String arg = commandAndArg .getValue ();
66+ String [] commandAndArg = commandParser .splitByDelimiter (command , " " );
67+ String commandWord = commandAndArg [0 ].toLowerCase ().strip ();
6768
6869 // Execute command
69- executeCommand (commandWord , arg );
70- }
71-
72- public boolean isActive () {
73- return isActive ;
74- }
75-
76- private Pair <String , String > getCommandAndArg (String str ) {
77- int spaceIndex = str .indexOf (" " );
78- if (spaceIndex == -1 ) {
79- // No spaces found, so must be single-word command
80- return new Pair <String , String >(str .toLowerCase (), "" );
70+ if (commandAndArg .length < 2 ) {
71+ // No args provided
72+ executeCommand (commandWord , "" );
8173 } else {
82- // Split string into command word and arguments
83- String commandWord = str .substring (0 , spaceIndex ).toLowerCase ();
84- String arg = str .substring (spaceIndex + 1 );
85- return new Pair <String , String >(commandWord , arg );
74+ // Use provided args
75+ executeCommand (commandWord , commandAndArg [1 ]);
8676 }
8777 }
8878
0 commit comments