Skip to content

Commit

Permalink
Refactor Ui code
Browse files Browse the repository at this point in the history
Abstracted Add, Delete and Done printing code to the Ui class as functions.
Also fixed assertion bug and test cases.
  • Loading branch information
gachia committed Sep 12, 2019
1 parent 876b4dd commit f6952f0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 39 deletions.
10 changes: 5 additions & 5 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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.
*
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
public class TaskList {

private ArrayList<Task> 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<Task> list) {
public TaskList(ArrayList<Task> list, Ui ui) {
this.list = list;
this.ui = ui;
}

/**
Expand All @@ -31,9 +35,7 @@ public TaskList(ArrayList<Task> 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());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down
57 changes: 49 additions & 8 deletions src/main/java/Ui.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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.
*
Expand All @@ -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
*/
Expand Down
10 changes: 3 additions & 7 deletions src/test/java/AddCommandTest.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,15 +15,15 @@ public class AddCommandTest {
@BeforeEach
void init() {
storage = new Storage("");
taskList = new TaskList();
ui = new Ui();
taskList = new TaskList(ui);
}

@Test
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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/DoneCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand All @@ -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);
Expand Down

0 comments on commit f6952f0

Please sign in to comment.