Skip to content

Commit 53c260b

Browse files
weixue123weixue123
authored andcommitted
Refactored the Ui class' methods to be static
1 parent fcf06c9 commit 53c260b

16 files changed

Lines changed: 106 additions & 77 deletions

src/main/java/duke/Duke.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import duke.tasks.Storage;
66
import duke.tasks.TaskList;
77
import duke.ui.Parser;
8-
import duke.ui.Ui;
98

109
public 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
}

src/main/java/duke/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public class Main extends Application {
1616

17-
private Duke duke = new Duke("data/tasks.txt");
17+
private final Duke duke = new Duke("data/tasks.txt");
1818

1919
@Override
2020
public void start(Stage stage) {

src/main/java/duke/commands/AddDeadlineCommand.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public void execute(TaskList tasks) {
4444
* Computes a response to notify the users the adding of the <code>Deadline</code>.
4545
*
4646
* @param tasks A collection of <code>Task</code> objects representing the application's state.
47-
* @param ui A handler to manage the application's user-interface layer.
4847
* @return A <code>String</code> to respond to the adding of the <code>Deadline</code>.
4948
*/
50-
public String getResponse(TaskList tasks, Ui ui) {
51-
return ui.handleAddTask(tasks, this.deadline);
49+
public String getResponse(TaskList tasks) {
50+
return Ui.getAddTaskResponse(tasks, this.deadline);
5251
}
5352
}

src/main/java/duke/commands/AddEventCommand.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public void execute(TaskList tasks) {
4444
* Computes a response to notify the users the adding of the <code>Event</code>.
4545
*
4646
* @param tasks A collection of <code>Task</code> objects representing the application's state.
47-
* @param ui A handler to manage the application's user-interface layer.
4847
* @return A <code>String</code> to respond to the adding of the <code>Event</code>.
4948
*/
50-
public String getResponse(TaskList tasks, Ui ui) {
51-
return ui.handleAddTask(tasks, this.event);
49+
public String getResponse(TaskList tasks) {
50+
return Ui.getAddTaskResponse(tasks, this.event);
5251
}
5352
}

src/main/java/duke/commands/AddToDoCommand.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ public void execute(TaskList tasks) {
4141
* Computes a response to notify the users the adding of the <code>ToDo</code>.
4242
*
4343
* @param tasks A collection of <code>Task</code> objects representing the application's state.
44-
* @param ui A handler to manage the application's user-interface layer.
4544
* @return A <code>String</code> to respond to the adding of the <code>ToDo</code>.
4645
*/
47-
public String getResponse(TaskList tasks, Ui ui) {
48-
return ui.handleAddTask(tasks, this.toDo);
46+
public String getResponse(TaskList tasks) {
47+
return Ui.getAddTaskResponse(tasks, this.toDo);
4948
}
5049
}

src/main/java/duke/commands/ByeCommand.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ public void execute(TaskList tasks) {
2929
* Creates a response to notify the users that the application is shutting down.
3030
*
3131
* @param tasks A collection of <code>Task</code> objects representing the application's state.
32-
* @param ui A handler to manage the application's user-interface layer.
3332
* @return A <code>String</code> to respond to the closing of the application.
3433
*/
35-
public String getResponse(TaskList tasks, Ui ui) {
36-
return ui.handleBye();
34+
public String getResponse(TaskList tasks) {
35+
return Ui.getByeResponse();
3736
}
3837
}

src/main/java/duke/commands/Command.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package duke.commands;
22

33
import duke.tasks.TaskList;
4-
import duke.ui.Ui;
54

65
/**
76
* Handles the logic of the application.
@@ -19,10 +18,9 @@ public abstract class Command {
1918
* Computes a feedback or response to the users' input.
2019
*
2120
* @param tasks A collection of <code>Task</code> objects representing the application's state.
22-
* @param ui A handler to manage the application's user-interface layer.
2321
* @return A <code>String</code> of response.
2422
*/
25-
public abstract String getResponse(TaskList tasks, Ui ui);
23+
public abstract String getResponse(TaskList tasks);
2624

2725
/**
2826
* Determines whether or not to terminate the application.

src/main/java/duke/commands/DeleteCommand.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ public void execute(TaskList tasks) {
4545
* Computes a response to notify the users if a <code>Task</code> is deleted.
4646
*
4747
* @param tasks A collection of <code>Task</code> objects representing the application's state.
48-
* @param ui A handler to manage the application's user-interface layer.
4948
* @return A <code>String</code> to respond to the deletion of a <code>Task</code> (if any).
5049
*/
51-
public String getResponse(TaskList tasks, Ui ui) {
52-
return ui.handleDelete(this.deletedTask, this.index);
50+
public String getResponse(TaskList tasks) {
51+
return Ui.getDeleteResponse(this.deletedTask, this.index);
5352
}
5453
}

src/main/java/duke/commands/DoNothingCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public void execute(TaskList tasks) {
2525
* Computes a feedback or response to the users' input.
2626
*
2727
* @param tasks A collection of <code>Task</code> objects representing the application's state.
28-
* @param ui A handler to manage the application's user-interface layer.
2928
* @return Error message
3029
*/
31-
public String getResponse(TaskList tasks, Ui ui) {
30+
public String getResponse(TaskList tasks) {
3231
return this.exceptionMessage;
3332
}
3433

src/main/java/duke/commands/DoneCommand.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ public void execute(TaskList tasks) {
4747
* Computes a response to notify the users if a <code>Task</code> is marked as done.
4848
*
4949
* @param tasks A collection of <code>Task</code> objects representing the application's state.
50-
* @param ui A handler to manage the application's user-interface layer.
5150
* @return A <code>String</code> to respond to marking a <code>Task</code> as done.
5251
*/
53-
public String getResponse(TaskList tasks, Ui ui) {
54-
return ui.handleDone(this.doneTask, this.index);
52+
public String getResponse(TaskList tasks) {
53+
return Ui.getDoneResponse(this.doneTask, this.index);
5554
}
5655
}

0 commit comments

Comments
 (0)