diff --git a/.gitignore b/.gitignore index f69985ef1..af6bf95f1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bin/ /text-ui-test/ACTUAL.txt text-ui-test/EXPECTED-UNIX.TXT +duke.txt diff --git a/README.md b/README.md index 8715d4d91..ab7d58d49 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +# Duke + +* Duke helps you record your tasks and manage them. +* It is a CLI program, where you type in commands to the program. +* If you forget or are unsure about Duke's functions, enter **help** to learn how to use them. + +## Functionalities # Duke project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -13,7 +20,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. If there are any further prompts, accept the defaults. 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).
In the same dialog, set the **Project language level** field to the `SDK default` option. -3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: +3. After that, locate the `src/main/java/duke/Duke.java` file, right-click it, and choose `Run duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: ``` Hello from ____ _ @@ -21,4 +28,4 @@ Prerequisites: JDK 11, update Intellij to the most recent version. | | | | | | | |/ / _ \ | |_| | |_| | < __/ |____/ \__,_|_|\_\___| - ``` + ``` \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index 8077118eb..af1226cfe 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,28 +2,256 @@ ## Features -### Feature-ABC +### Add Task -Description of the feature. +Allows you to add different kinds of tasks such as ToDos, Deadlines and Events +* ToDo - A task that can be done anytime +* Deadline - A task that needs to be done before a certain time +* Events - A task that needs to be done at a certain period of time -### Feature-XYZ +### List -Description of the feature. +Shows you the list of tasks you have and their status of completion. + +### Find + +Allows you to find tasks with a specific description or date. + +### Done + +Allows you to mark a task as done + +### Delete + +Allows you to remove a task from the list + +### Load + +Loads tasks from the "duke.txt" file if it exists. Creates a new "duke.txt" file if it does not + +### Save + +Saves the tasks to the "duke.txt" file when the program is terminated ## Usage -### `Keyword` - Describe action +### `todo` - todo *description* + +* Adds a ToDo task with its description to the list. +* If successful, the system will print out a success message, the task added, and the number of tasks in the list + +Example of usage: + +`todo read book` + +Expected outcome: + +* The success message is shown, along with the description of the task, the type of task added and the number of tasks in the list. + +``` + ___________________________________________________________ + I've added this to your list :D + [T][ ] read book + Now you have 1 task(s) in the list + ___________________________________________________________ +``` + +### `deadline` - deadline *description* /by *date* + +* Adds a Deadline task with its description and the date and time of the deadline to the list. +* If successful, the system will print out a success message, the task added, and the number of tasks in the list. +* The date of deadline is in the format of *yyyy-MM-ddTHH:mm* , where *yyyy* is the year, *MM* is the month, *dd* is the day, *HH* is the hour and *mm* is the minute. + +Example of usage: + +`deadline finish homework /by 2021-09-30T23:00` + +Expected outcome: + +* The success message is shown, along with the description of the task, date and time of the deadline, the type of task added, and the number of tasks in the list. + +``` + ___________________________________________________________ + I've added this to your list :D + [D][ ] finish homework (by: Sep 30 2021 23:00) + Now you have 2 task(s) in the list + ___________________________________________________________ +``` + + +### `event` - event *description* /at *start time* /to *end time* + +* Adds an Event task with its description and the date and time of the event to the list. +* If successful, the system will return a success message, the task added, and the number of tasks in the list. +* The date and time of the start of the event should be added before */to* and the date and time of the end of the event should be added after */to* +* The date of deadline is in the format of *yyyy-MM-ddTHH:mm* , where *yyyy* is the year, *MM* is the month, *dd* is the day, *HH* is the hour and *mm* is the minute. + +Example of usage: + +`event celebrate birthday /at 2021-09-30T00:00 /to 2021-09-30T02:00` + +Expected outcome: + +* The success message is shown, along with the description of the task, date and time of the event, the type of task added, and the number of tasks in the list. + +``` + ___________________________________________________________ + I've added this to your list :D + [E][ ] celebrate birthday (at: Sep 30 2021 00:00 to Sep 30 02:00) + Now you have 3 task(s) in the list + ___________________________________________________________ +``` + +### `list` - list + +* Displays the list of all tasks and their status of completion. + +Example of usage: + +`list` + +Expected outcome: + +* The list of tasks with their status of completion. +* If **X** appears in the second box, it means the task is completed. +* If the second box is empty, it means the task is not completed. + +``` + ___________________________________________________________ + Here are the things you need to do : + 1.[T][ ] read book + 2.[D][ ] finish homework (by: Sep 30 2021 23:00) + 3.[E][ ] celebrate birthday (at: Sep 30 2021 00:00 to Sep 30 02:00) + ___________________________________________________________ +``` + +### `find` - find *keyword* **or** find /d *date* + +* Searches through the list for tasks containing the keyword or with the same date, then displays all such tasks to the user if there is any. +* The date should be typed in the format of *yyyy-MM-dd* , where *yyyy* is the year, *MM* is the month and *dd* is the day + +Example of usage: + +1. `find read` +2. `find /d 2021-09-30` + +Expected outcome: + +1. A list of tasks containing the keyword given is shown to the user +2. A list of tasks with the same date is shown to the user + +``` + ___________________________________________________________ + Here are the matching tasks in your list : + 1.[T][ ] read book + ___________________________________________________________ + + ___________________________________________________________ + Here are the matching tasks in your list : + 1.[D][ ] finish homework (by: Sep 30 2021 23:00) + 2.[E][ ] celebrate birthday (at: Sep 30 2021 00:00 to Sep 30 02:00) + ___________________________________________________________ +``` + +### `done` - done *integer* + +* Marks the task with the specific number on the list as done. +* If successful, the system will print out the task marked as done and the task will have an **X** in the second box. + +Example of usage: + +`done 1` + +Expected outcome: + +* The first task in the list is marked as done. + +``` + ___________________________________________________________ + You've completed the task! Well done! + [T][X] read book + ___________________________________________________________ +``` + +### `delete` - delete *integer* + +* Deletes the task with the specific number on the list. +* If successful, the system will print out a success message, the deleted task, and the number of remaining tasks. + +Example of usage: + +`delete 1` + +Expected outcome: + +* The first task is deleted. The deleted task and the number of remaining tasks are shown. + +``` + ___________________________________________________________ + Alright, I've removed this task: + [T][X] read book + Now you have 2 task(s) in the list + ___________________________________________________________ +``` + +### `help` - help + +* Asks Duke to show the list of commands. + +Example of usage: + +`help` + +Expected outcome: +* The list of all the commands is printed out. + +``` + ___________________________________________________________ + list + - Shows you the list of tasks you have + + todo (description) + - Saves a general todo task + + deadline (description) /by yyyy-MM-ddTHH:mm + - Saves a task with a given deadline + - yyyy is the year, MM is the month, dd is the day, HH is the hour, + and mm is the minute + + event (description) /at yyyy-MM-ddTHH:mm /to yyyy-MM-ddTHH:mm + - Saves an event happening at a certain time period + - yyyy is the year, MM is the month, dd is the day, HH is the hour, + and mm is the minute + + delete (integer) + - Deletes a task with the corresponding number on the list + + done (integer) + - Marks the task with the corresponding number as done + + find (keyword) + - Finds tasks that contains the String given + + find /d yyyy-MM-dd + - Finds tasks with the same date as the date given + - yyyy is the year, MM is the month and dd is the day + ___________________________________________________________ +``` + +### `bye` - bye -Describe the action and its outcome. +* Terminates the program and saves the tasks to the file "duke.txt". Example of usage: -`keyword (optional arguments)` +`bye` Expected outcome: -Description of the outcome. +* The exit message is printed. ``` -expected output + ___________________________________________________________ + Bye, hope to see you again soon! :) + ___________________________________________________________ ``` diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..2c9a9745c --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: duke.Duke + diff --git a/src/main/java/commands/AddCommand.java b/src/main/java/commands/AddCommand.java new file mode 100644 index 000000000..0d6914d6d --- /dev/null +++ b/src/main/java/commands/AddCommand.java @@ -0,0 +1,105 @@ +package commands; + +import static constants.Message.INDENTATION; +import duke.DefaultException; +import task.Task; +import task.Event; +import task.Deadline; +import task.ToDo; +import task.TaskType; + +import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; + +/** + * A Command class that contains methods for adding tasks to the ArrayList. + */ +public class AddCommand extends Command{ + + private static final int EXPECTED_LENGTH_FOR_DEADLINE = 2; + private static final int EXPECTED_LENGTH_FOR_EVENT = 3; + public static final String DEADLINE_FORMAT_ERROR = "Please type the deadline in the format : \n" + + INDENTATION + "'deadline (description) /by yyyy-MM-ddThh:mm' :)"; + public static final String EVENT_FORMAT_ERROR = "Please type the event in the format: \n" + + INDENTATION + "'event (description) /at yyyy-MM-ddThh:mm /to yyyy-MM-ddThh:mm' :)"; + private static final String DATE_TIME_FORMAT_ERROR_FOR_DEADLINE = "Please write the date and time in the" + + " format : 'yyyy-mm-ddThh:mm' :)"; + private static final String DATE_TIME_FORMAT_ERROR_FOR_EVENT = "Please write the date and time in the format : \n" + + INDENTATION + "'yyyy-mm-ddThh:mm /to yyy-mm-ddThh:mm' :)"; + public static final String ADDED_TO_LIST = "I've added this to your list :D"; + public static final String TODO_COMMAND = "todo"; + public static final String EVENT_COMMAND = "event"; + public static final String DEADLINE_COMMAND = "deadline"; + + private final String[] parsedOutputs; + private final TaskType type; + + public AddCommand(String[] parsedOutputs, TaskType type) { + this.parsedOutputs = parsedOutputs; + this.type = type; + } + + /** + * Creates a task object and adds it to the ArrayList depending on the type of task. + * + * @return A CommandResult that tells the Ui to print the status of the execution + * and the task added (if successful). + * @throws DefaultException Throws an exception if something unexpected happens. + */ + @Override + public CommandResult execute() throws DefaultException { + switch(type){ + case TODO: + tasks.addTask(new ToDo(parsedOutputs[0])); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + case EVENT: + return getCommandResultForEvent(); + case DEADLINE: + return getCommandResultForDeadline(); + default : + throw new DefaultException(); + } + } + + /** + * Tries to add a deadline to the ArrayList. + * + * @return A CommandResult that tells the Ui to print the status of the execution + * and the task added (if successful). + */ + private CommandResult getCommandResultForDeadline() { + if(parsedOutputs.length < EXPECTED_LENGTH_FOR_DEADLINE) { + return new CommandResult(DEADLINE_FORMAT_ERROR,PrintOptions.DEFAULT); + } + try { + tasks.addTask(new Deadline(parsedOutputs[0], LocalDateTime.parse(parsedOutputs[1]))); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + } catch (DateTimeParseException error) { + return new CommandResult(DATE_TIME_FORMAT_ERROR_FOR_DEADLINE,PrintOptions.DEFAULT); + } + } + + /** + * Tries to add an event to the ArrayList. + * + * @return A CommandResult that tells the Ui to print the status of the execution + * and the task added (if successful). + */ + private CommandResult getCommandResultForEvent() { + if(parsedOutputs.length < EXPECTED_LENGTH_FOR_EVENT) { + return new CommandResult(EVENT_FORMAT_ERROR,PrintOptions.DEFAULT); + } + try { + tasks.addTask(new Event(parsedOutputs[0], LocalDateTime.parse(parsedOutputs[1]), + LocalDateTime.parse(parsedOutputs[2]))); + return new CommandResult(ADDED_TO_LIST,tasks.getTask(Task.getTotalTasks()-1), + PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + } catch (DateTimeParseException error) { + return new CommandResult(DATE_TIME_FORMAT_ERROR_FOR_EVENT,PrintOptions.DEFAULT); + } + } + + +} diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java new file mode 100644 index 000000000..c3528c73c --- /dev/null +++ b/src/main/java/commands/Command.java @@ -0,0 +1,21 @@ +package commands; + +import duke.DefaultException; +import duke.DukeException; +import tasklist.TaskList; + + +/** + * An abstract class with the common methods and attributes for different types of Commands. + */ +public abstract class Command { + protected TaskList tasks; + + public Command() { + this.tasks = new TaskList(); + } + public abstract CommandResult execute() throws DukeException, DefaultException; + public void passTaskList(TaskList tasks) { + this.tasks = tasks; + } +} diff --git a/src/main/java/commands/CommandResult.java b/src/main/java/commands/CommandResult.java new file mode 100644 index 000000000..db4a4dfb2 --- /dev/null +++ b/src/main/java/commands/CommandResult.java @@ -0,0 +1,56 @@ +package commands; + +import task.Task; +import tasklist.TaskList; + +/** + * A class for passing the message from the execution of the Command to the Ui. + */ +public class CommandResult{ + public final String message; + public final Task task; + public final TaskList tasks; + public final PrintOptions type; + + /** + * Creates a CommandResult that allows the list of tasks to be shown to the user + * + * @param message Message to the user + * @param tasks The list of tasks + * @param type The options for printing in Ui.showResult + */ + public CommandResult(String message, TaskList tasks, PrintOptions type) { + this.message = message; + this.task = null; + this.tasks = tasks; + this.type = type; + } + + /** + * Creates a CommandResult that only needs to show a message to the user. + * + * @param message Message to the user + * @param type The options for printing in Ui.showResult + */ + public CommandResult(String message, PrintOptions type) { + this.message = message; + this.task = null; + this.tasks = null; + this.type = type; + } + + /** + * Creates a CommandResult that requires a deleted/added task to be shown to the user. + * + * @param message Message to the user + * @param task The task shown to the user + * @param type The options for printing in Ui.showResult + */ + public CommandResult(String message, Task task, PrintOptions type) { + this.message = message; + this.task = task; + this.tasks = null; + this.type = type; + } + +} diff --git a/src/main/java/commands/DeleteCommand.java b/src/main/java/commands/DeleteCommand.java new file mode 100644 index 000000000..be8751171 --- /dev/null +++ b/src/main/java/commands/DeleteCommand.java @@ -0,0 +1,31 @@ +package commands; + +import task.Task; + +/** + * A Command class that deletes a task in the ArrayList. + */ +public class DeleteCommand extends Command{ + + public static final String DELETE_COMMAND = "delete"; + public static final String TASK_REMOVED = "Alright, I've removed this task:"; + private final int index; + + public DeleteCommand(int index) { + this.index = index; + } + + /** + * Deletes a task with a given index on the ArrayList + * + * @return A CommandResult that tells the Ui to print the status of execution + * and the deleted task. + * @throws NumberFormatException Throws an exception if the input given is not a number. + */ + @Override + public CommandResult execute() throws IndexOutOfBoundsException { + Task temp = tasks.getTask(index); + tasks.deleteTask(index); + return new CommandResult(TASK_REMOVED,temp,PrintOptions.WITH_TASK_AND_NUMBER_OF_TASK); + } +} diff --git a/src/main/java/commands/DoneCommand.java b/src/main/java/commands/DoneCommand.java new file mode 100644 index 000000000..f666673f4 --- /dev/null +++ b/src/main/java/commands/DoneCommand.java @@ -0,0 +1,29 @@ +package commands; + +/** + * A Command class that marks a task in the ArrayList as done + */ +public class DoneCommand extends Command{ + + private final int index; + private static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; + public static final String DONE_COMMAND = "done"; + + public DoneCommand(int index) { + this.index = index; + } + + /** + * Marks a task of a given index in the ArrayList as done + * + * @return A CommandResult that that tells the Ui to print the status of execution + * and the task that is done. + * @throws IndexOutOfBoundsException Throws an exception if the input given is not a number. + */ + @Override + + public CommandResult execute() throws IndexOutOfBoundsException { + tasks.getTask(index).markAsDone(); + return new CommandResult(TASK_COMPLETED_MESSAGE,tasks.getTask(index),PrintOptions.ONLY_TASK); + } +} diff --git a/src/main/java/commands/ExitCommand.java b/src/main/java/commands/ExitCommand.java new file mode 100644 index 000000000..69a342c41 --- /dev/null +++ b/src/main/java/commands/ExitCommand.java @@ -0,0 +1,20 @@ +package commands; + +/** + * A Command class that passes the exit message to the Ui + */ +public class ExitCommand extends Command{ + + public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; + public static final String EXIT_COMMAND = "bye"; + + /** + * Sends the exit message to the Ui + * + * @return A CommandResult that tells the Ui to print the exit message. + */ + @Override + public CommandResult execute() { + return new CommandResult(GOODBYE_MESSAGE,PrintOptions.DEFAULT); + } +} diff --git a/src/main/java/commands/FindCommand.java b/src/main/java/commands/FindCommand.java new file mode 100644 index 000000000..4aab2f56d --- /dev/null +++ b/src/main/java/commands/FindCommand.java @@ -0,0 +1,110 @@ +package commands; + +import task.Deadline; +import task.Event; +import task.Task; +import tasklist.TaskList; + +import java.time.LocalDate; +import java.time.LocalDateTime; + +/** + * A Command class that contains methods for finding tasks with specific description or date. + */ +public class FindCommand extends Command{ + + private final String input; + private final LocalDate dateTime; + private static final String LIST_CONTAINING_STRING_MESSAGE = "Here are the matching tasks in your list : "; + private static final String LIST_OF_SPECIFIC_DATE = "Here are the things you need to do on that day :"; + private static final String NO_SIMILAR_WORDS = "There are no matching tasks :("; + public static final String FIND_COMMAND = "find"; + + public FindCommand(String input) { + this.input = input; + this.dateTime = null; + } + public FindCommand(LocalDate dateTime) { + this.input = null; + this.dateTime = dateTime; + } + + /** + * Searches through the ArrayList for tasks which contains the descriptions + * given or have the same date. + * + * @return A CommandResult that tells the Ui to print the status of the execution + * and the list of tasks with similar descriptions / dates (if successful). + */ + public CommandResult execute() { + int totalTasks; + String message; + TaskList taskContainingInput = new TaskList(); + if(input == null) { + totalTasks = getTasksOfSpecificDate(taskContainingInput); + message = LIST_OF_SPECIFIC_DATE; + } else { + totalTasks = getTasksOfSpecificString(taskContainingInput); + message = LIST_CONTAINING_STRING_MESSAGE; + } + taskContainingInput.setNumberOfTask(totalTasks); + if(totalTasks == 0) { + return new CommandResult(NO_SIMILAR_WORDS,PrintOptions.DEFAULT); + } + return new CommandResult(message, + taskContainingInput,PrintOptions.LIST_WITH_SPECIFIC_CONDITIONS); + } + + /** + * Iterates through the ArrayList to get tasks with specific descriptions + * + * @param taskContainingString The new TaskList created for adding tasks + * with specific descriptions + * @return The total number of tasks in the new TaskList + */ + private int getTasksOfSpecificString(TaskList taskContainingString) { + int totalTasks = 0; + for (int i = 0; i < Task.getTotalTasks(); i++) { + if(tasks.getTask(i).getDescription().contains(input)){ + taskContainingString.addTaskForFindCommand(tasks.getTask(i)); + totalTasks++; + } + } + return totalTasks; + } + + /** + * Iterates through the ArrayList to get tasks with a specific date + * + * @param tasksOfSpecificDate The new TaskList created for adding tasks + * with a specific date + * @return The total number of tasks in the new TaskList + */ + private int getTasksOfSpecificDate(TaskList tasksOfSpecificDate) { + int totalTasks = 0; + for (int i = 0; i tasks; + private int numberOfTasks; + + public TaskList() { + this.tasks = new ArrayList<>(); + this.numberOfTasks = 0; + } + + /** + * @return Returns the number of tasks in the list produced by execution of FindCommand. + */ + public int getNumberOfTasks() { + return numberOfTasks; + } + + public void setNumberOfTask(int numberOfTasks) { + this.numberOfTasks = numberOfTasks; + } + + public void addTask(Task task) { + tasks.add(task); + Task.setTotalTasks(Task.getTotalTasks() + 1); + } + + /** + * Adds existing tasks to a new list when FindCommand is executed. + * This is to separate from the usual addTask method which increases + * the total number of tasks in the main ArrayList. + * + * @param task The task that needs to be printed after executing FindCommand. + */ + public void addTaskForFindCommand(Task task) { + tasks.add(task); + } + + public void deleteTask(int index) throws IndexOutOfBoundsException { + tasks.remove(index); + Task.setTotalTasks(Task.getTotalTasks() - 1); + } + + public Task getTask(int index) throws IndexOutOfBoundsException{ + return tasks.get(index); + } +} diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java new file mode 100644 index 000000000..9c69ee370 --- /dev/null +++ b/src/main/java/ui/Ui.java @@ -0,0 +1,152 @@ +package ui; + +import commands.CommandResult; + +import commands.PrintOptions; +import constants.Message; +import task.Task; + +import java.io.InputStream; +import java.util.Scanner; + +/** + * A class with methods that interacts with the user by accepting inputs and printing results. + */ +public class Ui { + + private final Scanner in; + private static final Boolean NOTIFY_NUMBER_OF_TASK = true; + private static final Boolean PRINT_TASK = true; + + public Ui(){ + this(System.in); + } + + public Ui(InputStream in){ + this.in = new Scanner(in); + } + + public String getUserCommand() { + return in.nextLine(); + } + + /** + * Divides the outputs so that it looks neater + */ + public void printIndentationAndDivider() { + System.out.print(Message.INDENTATION); + System.out.println(Message.DIVIDER); + } + + public void printWordsWithIndentation(String words) { + System.out.print(Message.INDENTATION); + System.out.println(words); + } + + /** + * Prints the logo and the welcome message in the given format. + */ + public void printStartingMessage() { + System.out.println("Hello from\n" + Message.LOGO); + printIndentationAndDivider(); + printWordsWithIndentation(Message.HELLO_MESSAGE_2); + printIndentationAndDivider(); + System.out.println(); + } + + /** + * Prints messages to the user not issued by a CommandResult in the given format. + * + * @param message The message that needs to be printed + */ + public void printMessage(String message) { + printIndentationAndDivider(); + printWordsWithIndentation(message); + printIndentationAndDivider(); + System.out.println(); + } + + /** + * Prints the result from the executed Commands in the given format. + * + * @param result CommandResult which contains the contents to be printed. + * @param notifyNumberOfTasks Determines whether the number of tasks in + * the list needs to be printed. + * @param printTask Determines whether the selected task needs to be printed. + */ + private void printCommandResultMessage(CommandResult result, Boolean notifyNumberOfTasks, Boolean printTask) { + printIndentationAndDivider(); + printWordsWithIndentation(result.message); + if(printTask) { + printWordsWithIndentation(result.task.getStatusIconAndDescription()); + } + if(notifyNumberOfTasks){ + printWordsWithIndentation(notifyNumberOfTasks()); + } + printIndentationAndDivider(); + System.out.println(); + } + + /** + * Selects the type of function needed to show the result of different executed Commands + * This reduces the number of methods needed in the Ui class. + * + * @param result CommandResult which contains the contents to be printed. + */ + public void showResult(CommandResult result) { + switch(result.type){ + case WITH_TASK_AND_NUMBER_OF_TASK: + printCommandResultMessage(result,NOTIFY_NUMBER_OF_TASK,PRINT_TASK); + break; + case ONLY_TASK: + printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,PRINT_TASK); + break; + case LIST_WITH_SPECIFIC_CONDITIONS: + case LIST: + printList(result); + break; + case DEFAULT: + printCommandResultMessage(result,!NOTIFY_NUMBER_OF_TASK,!PRINT_TASK); + break; + default: + printMessage(result.message); + } + } + + + + /** + * Prints the task's description with its completion status + * + * @param result A CommandResult object which passes the list that needs to be printed + */ + private void printTask(CommandResult result) { + int totalTasks; + if(result.type == PrintOptions.LIST_WITH_SPECIFIC_CONDITIONS) { + totalTasks = result.tasks.getNumberOfTasks(); + } else { + totalTasks = Task.getTotalTasks(); + } + for(int i = 0; i < totalTasks; i++) { + printWordsWithIndentation(i + 1 + "." + result.tasks.getTask(i).getStatusIconAndDescription()); + } + } + + /** + * Prints the list of tasks with the given format. + * + * @param result The CommandResult determines the message shown to the user + */ + private void printList(CommandResult result) { + printIndentationAndDivider(); + printWordsWithIndentation(result.message); + printTask(result); + printIndentationAndDivider(); + System.out.println(); + } + + private String notifyNumberOfTasks() { + int taskNumber = Task.getTotalTasks(); + return "Now you have " + taskNumber + " task(s) in the list"; + } +} diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e..b3ddd0653 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -5,3 +5,114 @@ Hello from | |_| | |_| | < __/ |____/ \__,_|_|\_\___| + ___________________________________________________________ + Hello! I'm Duke, your friendly neighbourhood task manager + What can I do for you? :D + (type help if you're not sure what to do!) + ___________________________________________________________ + + ___________________________________________________________ + Getting your tasks..... + ___________________________________________________________ + + ___________________________________________________________ + Done! + ___________________________________________________________ + + ___________________________________________________________ + I've added this to your list :D + [T][ ] borrow book + Now you have 1 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + I've added this to your list :D + [D][ ] return book (by: Sunday) + Now you have 2 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + I've added this to your list :D + [E][ ] project meeting (at: 4th Sep 2-3pm) + Now you have 3 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + Sorry, you're missing some arguments, do type 'help' if you're unsure :) + ___________________________________________________________ + + ___________________________________________________________ + Please tell me which task you want to select :) + ___________________________________________________________ + + ___________________________________________________________ + Here are the things you need to do : + 1.[T][ ] borrow book + 2.[D][ ] return book (by: Sunday) + 3.[E][ ] project meeting (at: 4th Sep 2-3pm) + ___________________________________________________________ + + ___________________________________________________________ + You've completed the task! Well done! + [T][X] borrow book + ___________________________________________________________ + + ___________________________________________________________ + Here are the things you need to do : + 1.[T][X] borrow book + 2.[D][ ] return book (by: Sunday) + 3.[E][ ] project meeting (at: 4th Sep 2-3pm) + ___________________________________________________________ + + ___________________________________________________________ + Please give me a number :) + ___________________________________________________________ + + ___________________________________________________________ + Please give a number between 1 and 3 :) + ___________________________________________________________ + + ___________________________________________________________ + Please give a number between 1 and 3 :) + ___________________________________________________________ + + ___________________________________________________________ + Alright, I've removed this task: + [T][X] borrow book + Now you have 2 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + Please tell me which task you want to select :) + ___________________________________________________________ + + ___________________________________________________________ + Please give a number between 1 and 2 :) + ___________________________________________________________ + + ___________________________________________________________ + Here are the things you need to do : + 1.[D][ ] return book (by: Sunday) + 2.[E][ ] project meeting (at: 4th Sep 2-3pm) + ___________________________________________________________ + + ___________________________________________________________ + Alright, I've removed this task: + [D][ ] return book (by: Sunday) + Now you have 1 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + Alright, I've removed this task: + [E][ ] project meeting (at: 4th Sep 2-3pm) + Now you have 0 task(s) in the list + ___________________________________________________________ + + ___________________________________________________________ + You're a free man :) + ___________________________________________________________ + + ___________________________________________________________ + Bye, hope to see you again soon! :) + ___________________________________________________________ + diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb..cc8592357 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,19 @@ +todo borrow book +DeadLine return book /by Sunday +Event project meeting /at 4th Sep 2-3pm +DEADLINE /by Monday +done +list +done 1 +list +done - +done 0 +done 5 +delete 1 +delete +delete 0 +list +delete 1 +delete 1 +list +bye \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..d2d47e6e5 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,8 @@ REM delete output from previous run if exist ACTUAL.TXT del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java +javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\duke\*.java ..\src\main\java\task\*.java ..\src\main\java\tasklist\*.java ..\src\main\java\ui\*.java ..\src\main\java\commands\*.java ..\src\main\java\constants\*.java ..\src\main\java\parser\*.java ..\src\main\java\storage\*.java + IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 @@ -15,7 +16,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin duke.Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT