55import duke .tasks .Storage ;
66import duke .tasks .TaskList ;
77import duke .ui .Parser ;
8- import duke .ui .Ui ;
98
109public class Duke {
1110 private final TaskList tasks ;
1211 private final Storage storage ;
13- private final Ui ui ;
1412
1513 /**
16- * Initializes an instance of the Duke chatbot/todo list .
14+ * Initializes an instance of Duke to handle ALL the logic of the application .
1715 *
1816 * @param filepath Path to text file from which tasks are loaded when the app starts, and to
1917 * which tasks are saved when the app terminates.
@@ -22,22 +20,53 @@ public class Duke {
2220 * corresponding to the input path will be created, to which existing tasks are saved.
2321 */
2422 public Duke (String filepath ) {
25- this .ui = new Ui ();
2623 this .storage = new Storage (filepath );
2724 this .tasks = this .storage .loadTasks ();
2825 }
2926
3027 /**
31- * Get response
28+ * Executes the command w.r.t. the users' input.
29+ *
30+ * @param input A line of raw user input.
3231 */
33- public String getResponse (String input ) {
34- Command c = Parser . parse (input );
35- c .execute (this .tasks );
32+ public void execute (String input ) {
33+ Command command = this . getCommand (input );
34+ command .execute (this .tasks );
3635
37- if (c instanceof ByeCommand ) {
38- storage .saveTasks (this .tasks );
36+ if (command instanceof ByeCommand ) {
37+ this . storage .saveTasks (this .tasks );
3938 }
39+ }
40+
41+ /**
42+ * Computes a response to display to the users w.r.t. the users' input.
43+ *
44+ * @param input A line of raw user input.
45+ * @return A <code>String</code> of response.
46+ */
47+ public String getResponse (String input ) {
48+ Command command = this .getCommand (input );
49+ return command .getResponse (this .tasks );
50+ }
4051
41- return c .getResponse (this .tasks , this .ui );
52+ /**
53+ * Determines if the application should be exited w.r.t the users' input.
54+ *
55+ * @param input A line of raw user input.
56+ * @return true if the application should be terminated, and false otherwise.
57+ */
58+ public boolean isExit (String input ) {
59+ Command command = this .getCommand (input );
60+ return command .isExit ();
61+ }
62+
63+ /**
64+ * Gets the <code>Command</code> object w.r.t the users' input.
65+ *
66+ * @param input A line or raw user input.
67+ * @return A <code>Command</code> object corresponding to the input.
68+ */
69+ private Command getCommand (String input ) {
70+ return Parser .parse (input );
4271 }
4372}
0 commit comments