From 45d6446e8abe49cf42cfa5c4f4e03dc84ea65663 Mon Sep 17 00:00:00 2001 From: NicLJR Date: Fri, 3 Feb 2023 02:01:13 +0800 Subject: [PATCH] Duke A-JUnit: Testing behaviour of Code + Updated Printing Methods for Tasks --- src/main/java/duke/TaskList.java | 2 +- src/main/java/duke/Ui.java | 9 ++++----- src/main/java/duke/tasks/Deadline.java | 9 +++++++-- src/main/java/duke/tasks/Events.java | 9 ++++++--- src/main/java/duke/tasks/Task.java | 5 +++++ src/main/java/duke/tasks/ToDos.java | 7 ++++++- src/test/java/seedu/duke/DeadlineTest.java | 21 +++++++++++++++++++++ src/test/java/seedu/duke/ToDoTest.java | 22 ++++++++++++++++++++++ 8 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 src/test/java/seedu/duke/DeadlineTest.java create mode 100644 src/test/java/seedu/duke/ToDoTest.java diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 379c666f03..3d3be22239 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -58,7 +58,7 @@ public void printTasks() throws DukeException { } for (int i = 0; i < num_tasks; i++) { Task ref = tasks.get(i); - System.out.println((i + 1) + "." + ref.getStatusIcon() + " " + ref.getDescription()); + System.out.println((i + 1) + "." + ref.toString()); } } } diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index 5bfcf7bdc6..8110abc362 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -41,23 +41,22 @@ public void showList(TaskList taskList) { } public void printMarkTask(Task markedTask) { - System.out.println("Nice! I have marked this task as Done:\n" + "[X] " + - markedTask.getDescription() + "\n"); + System.out.println("Nice! I have marked this task as Done:\n" + markedTask.toString() + "\n"); } public void printAddTask(Task to_add, Integer num_tasks) { - System.out.println("Got it fam! I've added this task:\n " + to_add.getDescription()); + System.out.println("Got it fam! I've added this task:\n " + to_add.toString()); System.out.println("You currently have " + num_tasks + " tasks in this list!\n"); } public void printUnmarkTask(Task unmarkedTask) { System.out.println("Ok! I have marked this task as not done yet:\n" + - "[ ] " + unmarkedTask.getDescription() + "\n"); + unmarkedTask.toString() + "\n"); } public void printDeleteTask(Task removedTask, Integer num_tasks) { System.out.println("Noted!I have deleted the task for you:\n " + - removedTask.getDescription() + "\nyou currently have " + num_tasks + + removedTask.toString() + "\nyou currently have " + num_tasks + " tasks in this list!\n"); } } diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index e865720b8e..22aa13ece7 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -16,7 +16,7 @@ public Deadline(String description, String by) { @Override public String getStatusIcon() { - return "[D]" + super.getStatusIcon(); + return super.getStatusIcon(); } public String getBy() { @@ -25,7 +25,7 @@ public String getBy() { @Override public String getDescription() { - return super.getDescription() + " (by: " + this.getBy() + ")"; + return super.getDescription(); } public String parseBySaving() { @@ -36,4 +36,9 @@ public String parseBySaving() { public String saveString() { return String.format("D|%s|%s|%s", super.saveString(), super.description, this.parseBySaving()); } + + @Override + public String toString() { + return String.format("[D]%s (by: %s)", super.toString(), this.getBy()); + } } diff --git a/src/main/java/duke/tasks/Events.java b/src/main/java/duke/tasks/Events.java index 88356a44c3..af24437c42 100644 --- a/src/main/java/duke/tasks/Events.java +++ b/src/main/java/duke/tasks/Events.java @@ -28,13 +28,12 @@ public String getEnd() { @Override public String getStatusIcon() { - return "[E]" + super.getStatusIcon(); + return super.getStatusIcon(); } @Override public String getDescription() { - return super.getDescription() + " (from: " + this.getStart() + - " to: " + this.getEnd() + ")"; + return super.getDescription(); } public String parseStartSaving() { @@ -50,4 +49,8 @@ public String saveString() { return String.format("E|%s|%s|%s|%s", super.saveString(), super.description, this.parseStartSaving(), this.parseEndSaving()); } + + public String toString() { + return String.format("[E]%s (from: %s to: %s)", super.toString(), this.getStart(), this.getEnd()); + } } diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index e604d06b8e..b8ef841a15 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -38,4 +38,9 @@ public String saveString() { return "0"; } } + + @Override + public String toString() { + return String.format("%s %s", this.getStatusIcon(), this.getDescription()); + } } diff --git a/src/main/java/duke/tasks/ToDos.java b/src/main/java/duke/tasks/ToDos.java index f5890e6a81..798cf5150c 100644 --- a/src/main/java/duke/tasks/ToDos.java +++ b/src/main/java/duke/tasks/ToDos.java @@ -8,11 +8,16 @@ public ToDos(String description) { @Override public String getStatusIcon() { - return "[T]" + super.getStatusIcon(); + return super.getStatusIcon(); } @Override public String saveString() { return String.format("T|%s|%s", super.saveString(), super.description); } + + @Override + public String toString() { + return String.format("[T]%s", super.toString()); + } } diff --git a/src/test/java/seedu/duke/DeadlineTest.java b/src/test/java/seedu/duke/DeadlineTest.java new file mode 100644 index 0000000000..24aeb7175b --- /dev/null +++ b/src/test/java/seedu/duke/DeadlineTest.java @@ -0,0 +1,21 @@ +package seedu.duke; + +import duke.tasks.Deadline; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DeadlineTest { + @Test + public void testFetchingDescription() { + Deadline deadline = new Deadline("return book", "2022-12-12 2330"); + assertEquals("[D][ ] return book (by: Dec 12 2022 2330)", deadline.toString()); + } + + @Test + public void testMarking(){ + Deadline deadline = new Deadline("return book", "2022-12-12 2330"); + deadline.mark(); + assertEquals("[D][X] return book (by: Dec 12 2022 2330)", deadline.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/seedu/duke/ToDoTest.java b/src/test/java/seedu/duke/ToDoTest.java new file mode 100644 index 0000000000..dae8ead815 --- /dev/null +++ b/src/test/java/seedu/duke/ToDoTest.java @@ -0,0 +1,22 @@ +package seedu.duke; + +import duke.tasks.ToDos; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToDoTest { + @Test + public void testFetchingDescription() { + ToDos toDoTask = new ToDos("return book"); + assertEquals("[T][ ] return book", toDoTask.toString()); + } + + @Test + public void testMarking(){ + ToDos toDoTask = new ToDos("return book"); + toDoTask.mark(); + assertEquals("[T][X] return book", toDoTask.toString()); + } + +}