Skip to content

Commit

Permalink
Change return type of execute method of Command
Browse files Browse the repository at this point in the history
  • Loading branch information
chrystalquek committed Sep 8, 2020
1 parent 798a522 commit b28ea46
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 56 deletions.
3 changes: 2 additions & 1 deletion src/main/java/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.ArrayList;
import java.util.Date;

/**
Expand Down Expand Up @@ -31,7 +32,7 @@ public AddCommand(TaskType taskType, String description, Date date) {
* @throws DukeException
*/
@Override
public String execute(TaskList tasks, Storage storage) throws DukeException {
public ArrayList<String> execute(TaskList tasks, Storage storage) throws DukeException {
Task task = null;
if (taskType == TaskType.TODO) {
task = new ToDo(description, false);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/Command.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.ArrayList;

/**
* A Command is a parsed request from the user.
*/
Expand All @@ -10,7 +12,7 @@ public abstract class Command {
* @param storage Storage to be activated if there are any changes to TaskList.
* @throws DukeException
*/
public abstract String execute(TaskList tasks, Storage storage) throws DukeException, TaskException;
public abstract ArrayList<String> execute(TaskList tasks, Storage storage) throws DukeException, TaskException;

/**
* Returns false for all commands except ExitCommand.
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.ArrayList;

/**
* DeleteCommand is a request to delete a Task.
*/
Expand All @@ -16,9 +18,10 @@ public DeleteCommand(int idx) {
* @param tasks TaskList to be modified.
* @param storage Storage to be updated.
* @throws DukeException
* @return ArrayList containing response message from Duke.
*/
@Override
public String execute(TaskList tasks, Storage storage) throws DukeException {
public ArrayList<String> execute(TaskList tasks, Storage storage) throws DukeException {
storage.saveList(tasks);
return Ui.getDeletedTask(tasks.removeTask(idx), tasks);
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.ArrayList;

/**
* DoneCommand is a request to mark a Task as done.
*/
Expand All @@ -16,9 +18,10 @@ public DoneCommand(int idx) {
* @param tasks TaskList to be modified.
* @param storage Storage to be updated.
* @throws DukeException
* @return ArrayList containing response message from Duke.
*/
@Override
public String execute(TaskList tasks, Storage storage) throws DukeException, TaskException {
public ArrayList<String> execute(TaskList tasks, Storage storage) throws DukeException, TaskException {
tasks.markAsDone(idx);
storage.saveList(tasks);
return Ui.getDoneTask(tasks.getTask(idx));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Duke(String filePath) {
public String getResponse(String text) {
try {
Command c = Parser.parse(text);
return c.execute(tasks, storage);
return Ui.formatMultiLine(c.execute(tasks, storage));
} catch (DukeException | TaskException e) {
return e.getMessage();
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.ArrayList;

/**
* DeleteCommand is a request to stop Duke from running.
*/
Expand All @@ -9,9 +11,10 @@ public class ExitCommand extends Command {
*
* @param tasks TaskList to be modified.
* @param storage Storage to be activated if there are any changes to TaskList.
* @return ArrayList containing response message from Duke.
*/
@Override
public String execute(TaskList tasks, Storage storage) {
public ArrayList<String> execute(TaskList tasks, Storage storage) {
return Ui.getGoodbye();
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ListCommand.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.ArrayList;
import java.util.Date;

/**
Expand Down Expand Up @@ -25,10 +26,10 @@ public ListCommand(Date on, String keyWord) {
*
* @param tasks TaskList to be printed.
* @param storage Storage is not activated.
* @return ArrayList containing response message from Duke.
*/
@Override
public String execute(TaskList tasks, Storage storage) {
public ArrayList<String> execute(TaskList tasks, Storage storage) {
return Ui.getTaskList(tasks, on, keyWord);
// ui.showTaskList(tasks, on, keyWord);
}
}
4 changes: 2 additions & 2 deletions src/main/java/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class MainWindow extends AnchorPane {
@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
DialogBox dialogBox = DialogBox.getUserDialog(Ui.getWelcome(), dukeImage);
DialogBox dialogBox = DialogBox.getUserDialog(Ui.formatMultiLine(Ui.getWelcome()), dukeImage);
dialogBox.flip();
dialogContainer.getChildren().add(dialogBox);
}
Expand All @@ -50,7 +50,7 @@ private void handleUserInput() {
DialogBox.getDukeDialog(response, dukeImage)
);
userInput.clear();
if (response.equals(Ui.getGoodbye())) {
if (response.equals(Ui.formatMultiLine(Ui.getGoodbye()))) {
Platform.exit();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static Command parse(String userInput) throws DukeException, TaskExceptio
int idx = userInput.indexOf(" /by ");
if (idx == -1 || userInput.substring(idx + 5).isBlank()) {
throw new TaskException(TaskType.DEADLINE, "time", TaskExceptionType.IDENTIFY);
} else if (userInput.substring(9, idx).isBlank()) {
} else if (idx <= 9 || userInput.substring(9, idx).isBlank()) {
throw new TaskException(TaskType.DEADLINE, "description", TaskExceptionType.EMPTY);
} else {
if (parseDate(userInput.substring(idx + 5)) == null) {
Expand Down
94 changes: 49 additions & 45 deletions src/main/java/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class Ui {
private static final String LINE = "____________________________________________________________";
private static final ArrayList<String> WELCOME_MSG = new ArrayList<>(Arrays.asList("Hello! I'm Duke", "What can I"
+ " do for you?"));
private static final String GOODBYE_MSG = "Bye. Hope to see you again soon!";
private static final ArrayList<String> GOODBYE_MSG = new ArrayList<>(Arrays.asList("Bye. Hope to see you again " +
"soon!"));
private static final String DONE_MSG = "Nice! I've marked this task as done:";
private static final String DELETED_MSG = "Noted. I've removed this task:";
private static final String ADD_MSG = "Got it. I've added this task:";
Expand All @@ -27,67 +28,59 @@ public static String formatDate(Date date) {
* @return String of formatted responses.
*/
public static String formatResponse(ArrayList<String> response) {
String indentLine = LINE + "\n";
String indentLine = INDENT + LINE + "\n";
String result = indentLine;
for (String resp : response) {
result += resp + "\n";
result += INDENT + INDENT + resp + "\n";
}
result += indentLine;
return result;
}

/**
* Formats each feedback message, e.g. enclosed in two lines.
* Overloaded formatResponse, takes in a variable number of strings, puts them in a ArrayList and passes it to the
* other formatResponse method.
*
* @param response Variable number of Strings of feedback messages.
* @param response ArrayList of feedback messages.
* @return String of formatted responses.
*/
public static String formatResponse(String ... response) {
ArrayList<String> lst = new ArrayList<>();
for (String resp : response) {
lst.add(resp);
}
return formatResponse(lst);
}

public static String formatMultilineResponse(String ... response) {
ArrayList<String> lst = new ArrayList<>();
for (String resp : response) {
lst.add(resp);
}
return formatMultilineResponse(lst);
public static String formatResponse(String response) {
return formatResponse(new ArrayList<>(Arrays.asList(response)));
}

public static String formatMultilineResponse(ArrayList<String> response) {
/**
* Formats each feedback message.
*
* @param response ArrayList of feedback messages.
* @return String of formatted responses.
*/
public static String formatMultiLine(ArrayList<String> response) {
String result = "";
for (String resp : response) {
result += INDENT + resp + "\n";
result += resp + "\n";
}
return result;
}

/**
* Returns formatted welcome message.
*
* @return String Formatted welcome message.
* @return ArrayList welcome message.
*/
public static String getWelcome() {
return formatMultilineResponse(WELCOME_MSG);
public static ArrayList<String> getWelcome() {
return WELCOME_MSG;
}

/**
* Returns formatted goodbye message.
*
* @return String Formatted goodbye message.
* @return String goodbye message.
*/
public static String getGoodbye() {
return INDENT + GOODBYE_MSG;
public static ArrayList<String> getGoodbye() {
return GOODBYE_MSG;
}

/**
* Returns a String with count of tasks.
* Returns a ArrayList String with count of tasks.
*
* @param tasks TaskList to count number of tasks from.
* @return String with count of tasks.
Expand All @@ -97,50 +90,61 @@ public static String getListCount(TaskList tasks) {
}

/**
* Returns a String containing details of all the tasks that pass date and keyWord criteria.
* Returns a ArrayList String containing details of all the tasks that pass date and keyWord criteria.
*
* @param tasks Tasks to filter from.
* @param date Date to filter tasks by.
* @param keyWord Keyword to filter tasks by.
* @return String containing details of tasks that fulfil the criteria.
* @return ArrayList containing details of tasks that fulfil the criteria.
*/
public static String getTaskList(TaskList tasks, Date date, String keyWord) {
public static ArrayList<String> getTaskList(TaskList tasks, Date date, String keyWord) {
ArrayList<String> lst = tasks.toString(date, keyWord);
lst.add(0, "Here are the " + ((keyWord == null) ? "" : "matching ") + "tasks in your list" + ((date == null)
? "" : " that occur on " + formatDate(date)) + ":");
return formatMultilineResponse(lst);
return lst;
}

/**
* Returns a String of a feedback message that task is marked as done.
* Returns a ArrayList String of a feedback message that task is marked as done.
*
* @param task Task that is done.
* @return Feedback message that task is marked as done.
* @return ArrayList feedback message that task is marked as done.
*/
public static String getDoneTask(Task task) {
return formatMultilineResponse(DONE_MSG, INDENT + task.toString());
public static ArrayList<String> getDoneTask(Task task) {
ArrayList<String> lst = new ArrayList<>();
lst.add(DONE_MSG);
lst.add(INDENT + task.toString());
return lst;
}

/**
* Returns a String of a feedback message that task is deleted.
* Returns a ArrayList String of a feedback message that task is deleted.
*
* @param task Task that is deleted.
* @param taskList
* @return
* @return ArrayList
*/
public static String getDeletedTask(Task task, TaskList taskList) {
return formatMultilineResponse(DELETED_MSG, INDENT + task.toString(), getListCount(taskList));
public static ArrayList<String> getDeletedTask(Task task, TaskList taskList) {
ArrayList<String> lst = new ArrayList<>();
lst.add(DELETED_MSG);
lst.add(INDENT + task.toString());
lst.add(getListCount(taskList));
return lst;
}

/**
* Returns a String of a feedback message that task is added to taskList.
* Returns a ArrayList String of a feedback message that task is added to taskList.
*
* @param task Task that is added.
* @param taskList TaskList
* @return Feedback message that task is added to taskList.
* @return ArrayList feedback message that task is added to taskList.
*/
public static String getAddTask(Task task, TaskList taskList) {
return formatMultilineResponse(ADD_MSG, INDENT + task.toString(), getListCount(taskList));
public static ArrayList<String> getAddTask(Task task, TaskList taskList) {
ArrayList<String> lst = new ArrayList<>();
lst.add(ADD_MSG);
lst.add(INDENT + task.toString());
lst.add(getListCount(taskList));
return lst;
}

}

0 comments on commit b28ea46

Please sign in to comment.