diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 108ce68d31..15581d26ea 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -24,10 +24,10 @@ public Duke() { ui = new Ui(); storage = new Storage(filePath); try { - taskList = new TaskList(storage.loadData()); + taskList = new TaskList(storage.loadData(), ui); } catch (DukeException e) { ui.showError(e.getMessage()); - taskList = new TaskList(); + taskList = new TaskList(ui); } } @@ -42,17 +42,17 @@ public String getResponse(String input) { String fullCommand = input; String[] commandArr = fullCommand.split(" ", 2); Command c = Parser.parse(commandArr[0].trim()); - if (commandArr.length > 1) { - return c.execute(storage, taskList, ui, commandArr[1].trim()); - } else { + if (commandArr.length <= 1) { return c.execute(storage, taskList, ui, ""); } + return c.execute(storage, taskList, ui, commandArr[1].trim()); } catch (DukeException e) { return ui.showError(e.getMessage()); } } /* + // Legacy code for starting up Duke in Command Line /** * Method to start up Duke Chatbot. * diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index e4ae364aad..325dc38f1a 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -6,21 +6,25 @@ public class TaskList { private ArrayList list; + private Ui ui; /** - * Default Constructor to generate an empty ArrayList of Task type. + * Constructor to generate an empty ArrayList of Task type and takes Ui for print functionality. */ - public TaskList() { + public TaskList(Ui ui) { list = new ArrayList<>(); + this.ui = ui; } /** - * Constructor to take in a populated ArrayList of Task type (i.e. from text file). + * Constructor to take in a populated ArrayList of Task type (i.e. from text file) + * and takes Ui for print functionality. * * @param list ArrayList of Task Type */ - public TaskList(ArrayList list) { + public TaskList(ArrayList list, Ui ui) { this.list = list; + this.ui = ui; } /** @@ -31,9 +35,7 @@ public TaskList(ArrayList list) { */ public String addTask(Task task) { list.add(task); - String message = "Got it. I've added this task:\n" + task - + "\nNow you have " + list.size() + " tasks in the list."; - return message; + return ui.showAdd(task, list.size()); } /** @@ -43,11 +45,9 @@ public String addTask(Task task) { * @return confirmation message of task being deleted */ public String deleteTask(int deleteIndex) { - assert deleteIndex < list.size() : "deleteIndex should not be larger than amount of tasks in list"; + assert deleteIndex <= list.size() : "deleteIndex should not be larger than amount of tasks in list"; Task temp = list.remove(deleteIndex - 1); - String message = "Noted. I've removed this task:\n" + temp - + "\nNow you have " + list.size() + " tasks in the list."; - return message; + return ui.showDelete(temp, list.size()); } /** @@ -102,11 +102,9 @@ public String setDoneTask(int doneIndex) { // Code for testing assertion doneIndex = 20; */ - assert doneIndex < list.size() : "doneIndex should not be larger than amount of tasks in list"; + assert doneIndex <= list.size() : "doneIndex should not be larger than amount of tasks in list"; list.get(doneIndex - 1).markAsDone(); - String message = "Nice! I've marked this task as done:\n" - + list.get(doneIndex - 1); - return message; + return ui.showDone(list.get(doneIndex - 1)); } /** diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index ff65cdce7e..35df9afbdc 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,7 +1,7 @@ import java.util.Scanner; /** - * Represents the User Interface of Duke, including the user input fields. + * Handles the printing of Duke messages. */ public class Ui { @@ -41,13 +41,6 @@ public String showGoodbye() { return endMessage; } - /** - * Prints a straight line for clarity purposes. - */ - public void showLine() { - System.out.print(lineSpace); - } - /** * Prints a specified error message. * @@ -58,8 +51,56 @@ public String showError(String error) { return error; } + /** + * Prints a message when tasks are added. + * + * @param task Task being added + * @param listSize Size of list + * @return Message of successful add + */ + public String showAdd(Task task, int listSize) { + String message = "Got it. I've added this task:\n" + task + + "\nNow you have " + listSize + " tasks in the list."; + return message; + } + + /** + * Prints a message when tasks are set to done. + * + * @param task Task selected to be done + * @return Message of marking done + */ + public String showDone(Task task) { + String message = "Nice! I've marked this task as done:\n" + + task; + return message; + } + + /** + * Prints a message when tasks are deleted. + * + * @param task Task selected for deletion + * @param listSize Size of list + * @return Message of successful deletion + */ + public String showDelete(Task task, int listSize) { + String message = "Noted. I've removed this task:\n" + task + + "\nNow you have " + listSize + " tasks in the list."; + return message; + } + + + /** + * Prints a straight line for clarity purposes. + * Legacy code for Command Line display. + */ + public void showLine() { + System.out.print(lineSpace); + } + /** * Reads the user input and returns a String. + * Legacy code for Command Line display. * * @return User input */ diff --git a/src/test/java/AddCommandTest.java b/src/test/java/AddCommandTest.java index 771ad67b37..2dfa31adce 100644 --- a/src/test/java/AddCommandTest.java +++ b/src/test/java/AddCommandTest.java @@ -1,11 +1,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; - import java.io.ByteArrayInputStream; import java.io.InputStream; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; public class AddCommandTest { @@ -17,7 +15,8 @@ public class AddCommandTest { @BeforeEach void init() { storage = new Storage(""); - taskList = new TaskList(); + ui = new Ui(); + taskList = new TaskList(ui); } @Test @@ -25,7 +24,6 @@ void testTodoExecute() { String input = "test ToDo"; InputStream in = new ByteArrayInputStream((input.getBytes())); System.setIn(in); - ui = new Ui(); try { AddToDoCommand ac = new AddToDoCommand(); ac.execute(storage, taskList, ui, input); @@ -39,7 +37,6 @@ void testDeadlineExecute() { String input = "test Deadline /by 08/08/2022 2359"; InputStream in = new ByteArrayInputStream((input.getBytes())); System.setIn(in); - ui = new Ui(); try { AddDeadlineCommand ac = new AddDeadlineCommand(); ac.execute(storage, taskList, ui, input); @@ -53,7 +50,6 @@ void testEventExecute() { String input = "party /at a certain place"; InputStream in = new ByteArrayInputStream((input.getBytes())); System.setIn(in); - ui = new Ui(); try { AddEventCommand ac = new AddEventCommand(); ac.execute(storage, taskList, ui, input); diff --git a/src/test/java/DeleteCommandTest.java b/src/test/java/DeleteCommandTest.java index dfa5dec5a9..6155af0f21 100644 --- a/src/test/java/DeleteCommandTest.java +++ b/src/test/java/DeleteCommandTest.java @@ -15,7 +15,8 @@ public class DeleteCommandTest { @BeforeEach void init() { storage = new Storage(""); - taskList = new TaskList(); + ui = new Ui(); + taskList = new TaskList(ui); taskList.addTask(new Task("test task")); } @@ -24,7 +25,6 @@ void testExecute() { String input = "1"; InputStream in = new ByteArrayInputStream((input.getBytes())); System.setIn(in); - ui = new Ui(); try { DeleteCommand dc = new DeleteCommand(); dc.execute(storage, taskList, ui, input); diff --git a/src/test/java/DoneCommandTest.java b/src/test/java/DoneCommandTest.java index 8e7bda5308..1a8be9a8a7 100644 --- a/src/test/java/DoneCommandTest.java +++ b/src/test/java/DoneCommandTest.java @@ -15,7 +15,8 @@ public class DoneCommandTest { @BeforeEach void init() { storage = new Storage(""); - taskList = new TaskList(); + ui = new Ui(); + taskList = new TaskList(ui); taskList.addTask(new Task("test task")); } @@ -24,7 +25,6 @@ void testExecute() { String input = "1"; InputStream in = new ByteArrayInputStream((input.getBytes())); System.setIn(in); - ui = new Ui(); try { DoneCommand dc = new DoneCommand(); dc.execute(storage, taskList, ui, input);