Skip to content

Commit 7e6ae05

Browse files
committed
Fix bug for Undo command to mark and unmark tasks
1 parent 89662e8 commit 7e6ae05

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed

src/main/java/zbot/Parser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public class CommandFactory {
6161
public static Command getCommand(String input, TaskList taskList) throws Exception {
6262
String[] parts = input.split(" ", 2);
6363
final String supportedCommands =
64-
"- list\n- mark\n- unmark\n- find\n- delete\n- todo\n- deadline\n- event\n- undo\n- bye";
64+
"- list\n- mark\n- unmark\n- find\n- delete\n- todo\n- deadline\n- event\n- undo\n- bye\n\n"
65+
+ "Date inputs are in the format of yyyy-MM-dd";
6566
switch (parts[0]) {
6667
case "list":
6768
if (parts.length != 1) {

src/main/java/zbot/gui/MainWindow.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ public class MainWindow extends AnchorPane {
2525
private Image dukeImage = new
2626
Image(this.getClass().getResourceAsStream("/images/Cat.png"));
2727

28+
/**
29+
* Initializes the Zbot GUI by setting up the dialog container and displaying an introductory message.
30+
* This method is called during the initialization of the main window.
31+
* It binds the scroll pane's vertical value property to the height of the dialog container
32+
* and adds a welcome message from Zbot to the dialog container.
33+
*/
2834
@FXML
2935
public void initializeZbot() {
3036
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
37+
dialogContainer.getChildren().add(DialogBox.getZbotDialog(
38+
"Hello! I'm ZoZo! How can I assist you today?", dukeImage));
3139
}
3240

3341
/** Injects the Zbot instance */

src/main/java/zbot/tasks/DeadlineTask.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ public String getDeadline() {
4343
return this.deadline.format(inputFormatter);
4444
}
4545

46+
/**
47+
* Creates a copy of the current task.
48+
* This method is intended to return a new instance of the task with the same description and state.
49+
*
50+
* @return A new {@link Task} object with the same description and deadline as the current task.
51+
*/
52+
@Override
53+
public Task copy() {
54+
DeadlineTask taskCopy = new DeadlineTask(this.getDescription(), this.getDeadline());
55+
if (this.getDoneStatus()) {
56+
taskCopy.markDone();
57+
} else {
58+
taskCopy.markUndone();
59+
}
60+
return taskCopy;
61+
}
62+
4663
/**
4764
* Returns a string representation of the {@code DeadlineTask}.
4865
* The string includes the task's description, completion status, and deadline.

src/main/java/zbot/tasks/EventTask.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ public String toDateString() {
6868
return this.toDate.format(outputFormatter);
6969
}
7070

71+
/**
72+
* Creates a copy of the current task.
73+
* This method is intended to return a new instance of the task with the same description and state.
74+
*
75+
* @return A new {@link Task} object with the same description and dates as the current task.
76+
*/
77+
@Override
78+
public Task copy() {
79+
EventTask taskCopy = new EventTask(this.getDescription(), this.getFromDate(), this.getToDate());
80+
if (this.getDoneStatus()) {
81+
taskCopy.markDone();
82+
} else {
83+
taskCopy.markUndone();
84+
}
85+
return taskCopy;
86+
}
87+
7188
/**
7289
* Returns a string representation of the {@code EventTask}.
7390
* The string includes the task's description, completion status, and the event's date range.

src/main/java/zbot/tasks/Task.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ public Task(String description) {
1919
this.isDone = false;
2020
}
2121

22+
/**
23+
* Creates a copy of the current task.
24+
* This method is intended to be overridden by subclasses of {@link Task}.
25+
* It should return a new instance of the task with the same description and relevant state.
26+
*
27+
* @return A new {@link Task} object with the same description and relevant state as the current task.
28+
*/
29+
public abstract Task copy();
30+
2231
/**
2332
* Marks the task as done.
2433
*/

src/main/java/zbot/tasks/TaskList.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ public boolean restore() {
160160
*
161161
*/
162162
private void saveStateForUndo() {
163-
undoHistory.push(new ArrayList<>(taskList));
163+
ArrayList<Task> taskListCopy = new ArrayList<>();
164+
for (Task task : taskList) {
165+
taskListCopy.add(task.copy());
166+
}
167+
undoHistory.push(taskListCopy);
164168
}
165169

166170
/**

src/main/java/zbot/tasks/ToDoTask.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ public ToDoTask(String description) {
1515
super(description);
1616
}
1717

18+
/**
19+
* Creates a copy of the current task.
20+
* This method is intended to return a new instance of the task with the same description and state.
21+
*
22+
* @return A new {@link Task} object with the same description as the current task.
23+
*/
24+
@Override
25+
public Task copy() {
26+
ToDoTask taskCopy = new ToDoTask(this.getDescription());
27+
if (this.getDoneStatus()) {
28+
taskCopy.markDone();
29+
} else {
30+
taskCopy.markUndone();
31+
}
32+
return taskCopy;
33+
}
34+
1835
/**
1936
* Returns a string representation of the to-do task.
2037
* The format is "[T][status] description".

0 commit comments

Comments
 (0)