Skip to content

Commit c34b347

Browse files
weixue123weixue123
authored andcommitted
Major refactor of the Commands classes and the handling of responses
1 parent 53c260b commit c34b347

16 files changed

Lines changed: 106 additions & 309 deletions

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

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
package duke.commands;
22

3-
import java.time.LocalDateTime;
4-
5-
import duke.tasks.Deadline;
3+
import duke.tasks.Task;
64
import duke.tasks.TaskList;
7-
import duke.ui.Ui;
85

96
/**
10-
* Handles the logic of adding a <code>Deadline</code> to the to-do list.
7+
* Handles the logic of adding a <code>Task</code> to the to-do list.
118
*/
12-
public class AddDeadlineCommand extends Command {
13-
private final Deadline deadline;
9+
public class AddTaskCommand extends Command {
10+
private final Task task;
1411

1512
/**
16-
* Initializes a command to add a <code>Deadline</code> with a description and a datetime.
13+
* Initializes a command to add a <code>Task</code> to the to-do list.
1714
*
18-
* @param description Description of the deadline task.
19-
* @param byDateTime The task's deadline.
15+
* @param task The <code>Task</code> to be added to the to-do list.
2016
*/
21-
public AddDeadlineCommand(String description, LocalDateTime byDateTime) {
22-
this.deadline = new Deadline(description, byDateTime);
17+
public AddTaskCommand(Task task) {
18+
this.task = task;
2319
}
2420

2521
/**
@@ -32,21 +28,23 @@ public boolean isExit() {
3228
}
3329

3430
/**
35-
* Adds the created <code>Deadline</code> to the input <code>TaskList</code>.
31+
* Adds the created <code>Task</code> to the input <code>TaskList</code>.
3632
*
3733
* @param tasks A collection of <code>Task</code> objects representing the application's state.
3834
*/
3935
public void execute(TaskList tasks) {
40-
tasks.addTask(this.deadline);
36+
tasks.addTask(this.task);
4137
}
4238

4339
/**
44-
* Computes a response to notify the users the adding of the <code>Deadline</code>.
40+
* Computes a response to notify the users the adding of the <code>Task</code>.
4541
*
4642
* @param tasks A collection of <code>Task</code> objects representing the application's state.
4743
* @return A <code>String</code> to respond to the adding of the <code>Deadline</code>.
4844
*/
4945
public String getResponse(TaskList tasks) {
50-
return Ui.getAddTaskResponse(tasks, this.deadline);
46+
return "Got it. I've added this task:\n"
47+
+ this.task.getStatusString() + "\n"
48+
+ "Now you have " + tasks.getSize() + " task(s) in the list.";
5149
}
5250
}

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

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

Lines changed: 1 addition & 2 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 terminating the application.
@@ -32,6 +31,6 @@ public void execute(TaskList tasks) {
3231
* @return A <code>String</code> to respond to the closing of the application.
3332
*/
3433
public String getResponse(TaskList tasks) {
35-
return Ui.getByeResponse();
34+
return "Bye. Hope to see you again soon!";
3635
}
3736
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package duke.commands;
22

3+
import duke.exceptions.TaskNumberNotExistException;
34
import duke.tasks.Task;
45
import duke.tasks.TaskList;
5-
import duke.ui.Ui;
66

77
/**
88
* Handles the logic of deleting a task from the to-do list.
@@ -48,6 +48,14 @@ public void execute(TaskList tasks) {
4848
* @return A <code>String</code> to respond to the deletion of a <code>Task</code> (if any).
4949
*/
5050
public String getResponse(TaskList tasks) {
51-
return Ui.getDeleteResponse(this.deletedTask, this.index);
51+
try {
52+
if (null == this.deletedTask) {
53+
throw new TaskNumberNotExistException(this.index);
54+
}
55+
} catch (TaskNumberNotExistException e) {
56+
return e.getMessage();
57+
}
58+
59+
return "Noted. I've removed this task:\n" + this.deletedTask.getStatusString();
5260
}
5361
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package duke.commands;
22

3+
import duke.exceptions.TaskNumberNotExistException;
34
import duke.tasks.Task;
45
import duke.tasks.TaskList;
5-
import duke.ui.Ui;
66

77
/**
88
* Handles the logic of marking a task in the to-do as done.
@@ -50,6 +50,14 @@ public void execute(TaskList tasks) {
5050
* @return A <code>String</code> to respond to marking a <code>Task</code> as done.
5151
*/
5252
public String getResponse(TaskList tasks) {
53-
return Ui.getDoneResponse(this.doneTask, this.index);
53+
try {
54+
if (null == this.doneTask) {
55+
throw new TaskNumberNotExistException(this.index);
56+
}
57+
} catch (TaskNumberNotExistException e) {
58+
return e.getMessage();
59+
}
60+
61+
return "Nice! I've marked this task as done:\n" + this.doneTask.getStatusString();
5462
}
5563
}

src/main/java/duke/commands/FindCommand.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import duke.tasks.Task;
44
import duke.tasks.TaskList;
5-
import duke.ui.Ui;
65

76
/**
87
* Handles the logic of searching the to-do list for tasks that matches certain keywords.
@@ -57,6 +56,10 @@ public String getResponse(TaskList tasks) {
5756
}
5857
}
5958

60-
return Ui.getFindResponse(matchingTasks, this.keywords);
59+
if (matchingTasks.getSize() == 0) {
60+
return "There are no tasks matching the '" + this.keywords + "' in your list :O";
61+
} else {
62+
return "Here are the matching tasks in your list:\n" + matchingTasks.getTaskListAsString();
63+
}
6164
}
6265
}

src/main/java/duke/commands/DoNothingCommand.java renamed to src/main/java/duke/commands/InvalidInputCommand.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package duke.commands;
22

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

65
/**
76
* Handles the logic for when the users input invalid commands
87
*/
9-
public class DoNothingCommand extends Command {
10-
private final String exceptionMessage;
8+
public class InvalidInputCommand extends Command {
9+
private final String message;
1110

12-
public DoNothingCommand(String exceptionMessage) {
13-
this.exceptionMessage = exceptionMessage;
11+
public InvalidInputCommand(String message) {
12+
this.message = message;
1413
}
1514

1615
/**
@@ -28,7 +27,7 @@ public void execute(TaskList tasks) {
2827
* @return Error message
2928
*/
3029
public String getResponse(TaskList tasks) {
31-
return this.exceptionMessage;
30+
return this.message;
3231
}
3332

3433
/**

src/main/java/duke/commands/ListCommand.java

Lines changed: 5 additions & 2 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 listing the tasks in the to-do list.
@@ -34,6 +33,10 @@ public boolean isExit() {
3433
* @return A <code>String</code> displaying the existing tasks.
3534
*/
3635
public String getResponse(TaskList tasks) {
37-
return Ui.getListResponse(tasks);
36+
if (tasks.getSize() == 0) {
37+
return "You have no tasks in your list yet :)";
38+
} else {
39+
return "Here are the task(s) in your list:\n" + tasks.getTaskListAsString();
40+
}
3841
}
3942
}

src/main/java/duke/tasks/Storage.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public void saveTasks(TaskList tasks) {
7070
} catch (IOException e) {
7171
e.printStackTrace();
7272
}
73-
7473
}
7574

7675
/**
@@ -91,15 +90,21 @@ private Task createTaskFromSavedString(String taskDetails) {
9190
LocalDateTime dateTime = Parser.convertToDateTime(dateTimeString);
9291

9392
Task newTask;
94-
if (taskType.equals("T")) {
93+
switch (taskType) {
94+
case "T":
9595
newTask = new ToDo(description);
96-
} else if (taskType.equals("D")) {
96+
break;
97+
case "D":
9798
newTask = new Deadline(description, dateTime);
98-
} else {
99+
break;
100+
case "E":
99101
newTask = new Event(description, dateTime);
102+
break;
103+
default:
104+
newTask = null;
100105
}
101106

102-
if (done.equals("1")) {
107+
if (done.equals("1") && newTask != null) {
103108
newTask.markAsDone();
104109
}
105110

@@ -125,8 +130,10 @@ private String convertTaskToSavableString(Task task) {
125130
} else if (task instanceof Event) {
126131
taskType = "E";
127132
dateTimeString = ((Event) task).getAtDateTimeString();
128-
} else {
133+
} else if (task instanceof ToDo) {
129134
taskType = "T";
135+
} else {
136+
return "";
130137
}
131138

132139
String done = task.isDone() ? "1" : "0";

0 commit comments

Comments
 (0)