From 456b267a76f2d85f30b8c20110195330af763c14 Mon Sep 17 00:00:00 2001 From: gachia Date: Sun, 1 Sep 2019 18:49:42 +0800 Subject: [PATCH] Completed A-JavaDoc Completed Level A-JavaDoc Fixed several bugs: - Enable a line in gradle to run Duke with System input, otherwise it will not do so and fail the build upon running - Fixed Exit Command to exit Duke when file path is not specified. Previously it did not exit due to throwing a DukeException which skipped its exit line. --- build.gradle | 4 ++++ src/main/java/AddCommand.java | 20 +++++++++++++++++- src/main/java/Command.java | 19 +++++++++++++++++ src/main/java/Deadline.java | 25 ++++++++++++++++++++++ src/main/java/DeleteCommand.java | 15 +++++++++++-- src/main/java/DoneCommand.java | 14 +++++++++++++ src/main/java/Duke.java | 16 ++++++++------ src/main/java/DukeException.java | 7 +++++++ src/main/java/Event.java | 24 +++++++++++++++++++++ src/main/java/ExitCommand.java | 14 ++++++++++++- src/main/java/ListCommand.java | 11 ++++++++++ src/main/java/Parser.java | 9 ++++++++ src/main/java/Storage.java | 25 ++++++++++++++++++++-- src/main/java/Task.java | 35 +++++++++++++++++++++++++++++++ src/main/java/TaskList.java | 36 ++++++++++++++++++++++++++++++-- src/main/java/Ui.java | 31 +++++++++++++++++++++++++++ 16 files changed, 291 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index 76dd0ce8eb..8f1dd5700b 100644 --- a/build.gradle +++ b/build.gradle @@ -31,6 +31,10 @@ test { useJUnitPlatform() } +run { + standardInput = System.in; +} + application { // Change this to your main class. mainClassName = "Duke" diff --git a/src/main/java/AddCommand.java b/src/main/java/AddCommand.java index 10467e9d6c..0449f9c026 100644 --- a/src/main/java/AddCommand.java +++ b/src/main/java/AddCommand.java @@ -2,18 +2,36 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +/* + * Represents the Command for adding Tasks and its subclasses + * A subclass of Command + */ public class AddCommand extends Command { private String taskCmd; - //Time final String timePattern = "d MMMM yyyy, h:mma"; DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern(timePattern); + /* + * Constructor for AddCommand to set the Task command + * @param taskCmd User input of what Task is being generated + */ public AddCommand(String taskCmd) { super(); this.taskCmd = taskCmd; } + /* + * Overridden execute method from Command to add a Task object into the list of tasks. + * The method will check the user input for a valid Command and adds the appropriate Task + * accordingly. It will throw an exception if the user inputs are unrecognisable for the + * method to execute correctly. + * @param storage Storage object for saving purposes + * @param tasks Contains the list of tasks + * @param ui Holds Ui printing methods and user input field + * @throws DukeException If taskCmd and taskName is invalid and format of subsequent fields, + * such as /by, /at and dateTime, is wrong + */ @Override public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException { String taskName; diff --git a/src/main/java/Command.java b/src/main/java/Command.java index 59207eba6d..d634ca2ea1 100644 --- a/src/main/java/Command.java +++ b/src/main/java/Command.java @@ -1,13 +1,32 @@ +/* + * Represents the user input as a Command object + */ public abstract class Command { protected boolean hasExit; + /* + * Default Constructor to set hasExit boolean variable to false + * Will be set to true if ExitCommand is called + */ public Command() { hasExit = false; } + /* + * Abstract method for the sub-classes of Command to use. This method will execute + * differently based on the sub-class called + * @param storage Storage object for saving purposes + * @param tasks Contains the list of tasks + * @param ui Holds Ui printing methods and user input field + * @throws DukeException If there is an error + */ abstract void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException; + /* + * Returns boolean variable hasExit for checking exit status + * @return hasExit boolean variable + */ public boolean isExit() { return hasExit; } diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index d70f3f6cd7..9030b23e34 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,21 +1,46 @@ +/* + * Represents a different type of Task called Deadline that holds an additional + * parameter for date and time + * A sub-class of Task + */ public class Deadline extends Task { protected String by; + /* + * Constructor to set the description of deadline and due date and time + * @param desc The description of the Deadline + * @param by The due date and time of the Deadline + */ public Deadline(String desc, String by) { super(desc); this.by = by; } + /* + * Constructor with additional parameter to set its completion status + * @param desc The description of the Deadline + * @param by The due date and time of the Deadline + * @param isDone The boolean variable to note if Deadline is completed + */ public Deadline(String desc, String by, boolean isDone) { super(desc, isDone); this.by = by; } + /* + * Overridden writeFormat method to specify that it is a Deadline + * when saving the data + * @return Format for saving data + */ @Override public String writeFormat() { return "D " + isDone + " " + description + "/" + by; } + /* + * Overridden toString method to print out Deadline object + * @return Printing format of Deadline + */ @Override public String toString() { return "[D]" + super.getTask() + " (by: " + by + ")"; diff --git a/src/main/java/DeleteCommand.java b/src/main/java/DeleteCommand.java index 4b22018e42..cd9e886e2d 100644 --- a/src/main/java/DeleteCommand.java +++ b/src/main/java/DeleteCommand.java @@ -1,7 +1,18 @@ -import java.util.Scanner; - +/* + * Represents the Command for deleting Tasks for the list + * A sub-class of Command + */ public class DeleteCommand extends Command { + /* + * Overridden execute method from Command to delete a Task object from the list of tasks. + * The method will check the user input for a valid index and deletes the selected + * Task object from the list. It will throw an exception if the index is invalid + * @param storage Storage object for saving purposes + * @param tasks Contains the list of tasks + * @param ui Holds Ui printing methods and user input field + * @throws DukeException If deletion < 0 or > size of list + */ @Override public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException { int deletion = ui.readIndex(); diff --git a/src/main/java/DoneCommand.java b/src/main/java/DoneCommand.java index c26524dcd7..19eda30116 100644 --- a/src/main/java/DoneCommand.java +++ b/src/main/java/DoneCommand.java @@ -1,5 +1,19 @@ +/* + * Represents the Command for setting Tasks as completed + * A sub-class of Command + */ public class DoneCommand extends Command { + /* + * Overridden execute method from Command to mark a Task object as completed. + * The method will check the user input for a valid index and change the selected + * Task object's isDone boolean variable to true. + * It will throw an exception if the index is invalid + * @param storage Storage object for saving purposes + * @param tasks Contains the list of tasks + * @param ui Holds Ui printing methods and user input field + * @throws DukeException If taskNo < 0 or > size of list + */ @Override public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException { int taskNo = ui.readIndex(); diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 95aaa828d0..101857d4c9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,15 +1,16 @@ -import java.io.FileNotFoundException; -import java.util.Scanner; -import java.util.ArrayList; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - +/* + * Main class responsible for running Duke Chatbot + */ public class Duke { private Storage storage; private TaskList taskList; private Ui ui; + /* + * Constructor of Duke class + * @param filepath The directory of the text file for populating task list + */ public Duke(String filePath) { ui = new Ui(); storage = new Storage(filePath); @@ -21,6 +22,9 @@ public Duke(String filePath) { } } + /* + * Method to start up Duke Chatbot + */ public void run() { ui.showWelcome(); boolean isExit = false; diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java index 0c1bd175a2..0aa6186bda 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/DukeException.java @@ -1,5 +1,12 @@ +/* + * Represents Exceptions from the Duke project + */ public class DukeException extends Exception { + /* + * Constructor to print custom error messages + * @param error Error message + */ public DukeException(String error) { super(error); } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index b70964dcc4..5dc1e24b18 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,22 +1,46 @@ +/* + * Represents a different type of Task called Event that holds an + * additional parameter for the venue + */ public class Event extends Task { protected String at; + /* + * Constructor to set the description of event and venue + * @param desc The description of the Event + * @param at The venue of the Event + */ public Event(String desc, String at) { super(desc); this.at = at; } + /* + * Constructor with additional parameter to set its completion status + * @param desc The description of the Event + * @param at The venue of the Event + * @param isDone The boolean variable to note if Event is completed + */ public Event(String desc, String at, boolean isDone) { super(desc, isDone); this.at = at; } + /* + * Overridden writeFormat method to specify that it is a Event + * when saving the data + * @return Format for saving data + */ @Override public String writeFormat() { return "E " + isDone + " " + description + "/" + at; } + /* + * Overridden toString method to print out Event object + * @return Printing format of Event + */ @Override public String toString() { return "[E]" + super.getTask() + " (at: " + at + ")"; diff --git a/src/main/java/ExitCommand.java b/src/main/java/ExitCommand.java index 9aa7761f13..ecd30bb003 100644 --- a/src/main/java/ExitCommand.java +++ b/src/main/java/ExitCommand.java @@ -1,7 +1,19 @@ +/* + * Represents the Command for exiting the Duke Chatbot + * A sub-class of Command + */ public class ExitCommand extends Command { + /* + * Overridden execute method from Command to save the list data to the specified + * text file. The method will set the hasExit boolean variable to true, call + * the saveData method from storage and display the Goodbye Message to the user + * @param storage Storage object for saving purposes + * @param tasks Contains the list of tasks + * @param ui Holds Ui printing methods and user input field + */ @Override - public void execute(Storage storage, TaskList tasks, Ui ui) throws DukeException { + public void execute(Storage storage, TaskList tasks, Ui ui) { hasExit = true; storage.saveData(tasks.getTaskList()); ui.showGoodbye(); diff --git a/src/main/java/ListCommand.java b/src/main/java/ListCommand.java index 6349917140..3b72a34851 100644 --- a/src/main/java/ListCommand.java +++ b/src/main/java/ListCommand.java @@ -1,5 +1,16 @@ +/* + * Represents the Command for printing out all of the tasks in the list + * A sub-class of Command + */ public class ListCommand extends Command { + /* + * Overridden execute method to print out all of the tasks inside the list + * using the method from TaskList + * @param storage Storage object for saving purposes + * @param tasks Contains the list of tasks + * @param ui Holds Ui printing methods and user input field + */ @Override public void execute(Storage storage, TaskList tasks, Ui ui) { tasks.showTaskList(); diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 976f19f297..ccec4c6eae 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -1,5 +1,14 @@ +/* + * Responsible for interpreting Commands + */ public class Parser { + /* + * Checks user input for a Command and returns the associated Command class + * @param userCmd The command from user input + * @return Command object + * @throws DukeException If user input is an invalid command + */ public static Command parse(String userCmd) throws DukeException { switch (userCmd) { case "list": diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 3d3ddd9bce..9b9cefbc1c 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -5,14 +5,28 @@ import java.io.FileWriter; import java.util.Scanner; +/* + * Represents the read and write of data into a text file + */ public class Storage { private String filePath; + /* + * Constructor to specify the file path of the text file used + * for saving and loading of Tasks for the list + * @param filePath directory of text file + */ public Storage(String filePath) { this.filePath = filePath; } + /* + * Returns a ArrayList of Task type for Duke to use as an initial list. + * Data is obtained from a text file with a specific format + * @return List of Tasks to be used + * @throws DukeException When format is wrong and filePath is invalid + */ public ArrayList loadData() throws DukeException { try { File f = new File(filePath); @@ -50,7 +64,14 @@ public ArrayList loadData() throws DukeException { } } - public void saveData(ArrayList list) throws DukeException { + /* + * Saves all of the Tasks inside the list into a text file for future usage. + * It will throw an exception if the file path was not specified in storage upon initialisation. + * However, an IOException is thrown instead of DukeException to be able + * to exit the program despite not saving + * @param list List of Tasks used in Duke + */ + public void saveData(ArrayList list) { try { FileWriter fw = new FileWriter(filePath); String data = ""; @@ -60,7 +81,7 @@ public void saveData(ArrayList list) throws DukeException { fw.write(data); fw.close(); } catch (IOException e) { - throw new DukeException("Error writing file."); + System.out.println("Failed to save data. File path may not have been specified."); } } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index e8165898d1..b146433fbd 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,38 +1,73 @@ +/* + * Represents the tasks the user enters into Duke + */ public class Task { protected String description; protected boolean isDone; + /* + * Constructor to set the description of the Task + * @param desc The description of the Task + */ public Task(String desc) { this.description = desc; this.isDone = false; } + /* + * Constructor to set the description of the Task and whether it is done + * @param desc The description of the Task + * @param isDone The boolean variable to note if Task is completed + */ public Task(String desc, boolean isDone) { this.description = desc; this.isDone = isDone; } + /* + * Returns a Tick or X symbol based on whether the Task is completed + * @return The status icon of the Task object + */ public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols } + /* + * Sets the boolean variable isDone to true, marking the Task as completed + */ public void markAsDone() { isDone = true; } + /* + * Returns the description of the Task with its status + * @return Task description and status in the format of "[Status] description" + */ public String getTask() { String output = "[" + getStatusIcon() + "] " + description; return output; } + /* + * Returns the boolean variable isDone of the Task object + * @return status of task + */ public boolean getIsDone() { return isDone; } + /* + * Returns the Task object in a String format for saving into a text file + * @returns description and status of Task for saving + */ public String writeFormat() { return "T " + isDone + " " + description; } + /* + * Overrides toString method for printing Task object, which includes Task type + * @return display format of Task, "[T][Status] description" + */ @Override public String toString() { return "[T]" + getTask(); diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index a6129f5ae8..e3c8b80543 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,35 +1,59 @@ -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; +/* + * Represents the list of tasks that Duke holds + */ public class TaskList { private ArrayList list; + /* + * Default Constructor to generate an empty ArrayList of Task type + */ public TaskList() { list = new ArrayList<>(); } + /* + * Constructor to take in a populated ArrayList of Task type (i.e. from text file) + * @param list ArrayList of Task Type + */ public TaskList(ArrayList list) { this.list = list; } + /* + * Adds a Task object into the list + * @param task A Task object + */ public void addTask(Task task) { list.add(task); System.out.println("Got it. I've added this task:\n" + task + "\nNow you have " + list.size() + " tasks in the list."); } + /* + * Deletes a Task from the list + * @param deleteIndex Index of selected Task Object, 1-based index + */ public void deleteTask(int deleteIndex) { Task temp = list.remove(deleteIndex - 1); System.out.println("Noted. I've removed this task:\n" + temp + "\nNow you have " + list.size() + " tasks in the list."); } + /* + * Returns selected Task object from list + * @param taskIndex Index of selected Task object, 1-based index + * @return Task object + */ public Task getTask(int taskIndex) { return list.get(taskIndex - 1); } + /* + * Prints out the list of tasks that Duke holds + */ public void showTaskList() { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < list.size(); i++) { @@ -37,12 +61,20 @@ public void showTaskList() { } } + /* + * Sets boolean variable isDone of selected Task object to true + * @param doneIndex Index of selected Task object, 1-based index + */ public void setDoneTask(int doneIndex) { list.get(doneIndex - 1).markAsDone(); System.out.println("Nice! I've marked this task as done:\n" + list.get(doneIndex - 1)); } + /* + * Returns the list of tasks that Duke holds as an object + * @return ArrayList of Task type + */ public ArrayList getTaskList() { return list; } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 093ac4b48e..bd9d3ba0a6 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,14 +1,23 @@ import java.util.Scanner; +/* + * Represents the User Interface of Duke, including the user input fields + */ public class Ui { final String lineSpace = "_______________________________\n"; private Scanner sc; + /* + * Default Constructor to initialise Scanner object for user input + */ public Ui() { sc = new Scanner(System.in); } + /* + * Prints out Welcome Message for the launch of Duke Chatbot + */ public void showWelcome() { final String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -19,29 +28,51 @@ public void showWelcome() { System.out.println("Hello from\n" + logo + startMessage); } + /* + * Prints Goodbye Message when user closes the Duke Chatbot + */ public void showGoodbye() { String endMessage = "Bye. Hope to see you again!"; System.out.println(endMessage); } + /* + * Prints a straight line for clarity purposes + */ public void showLine() { System.out.print(lineSpace); } + /* + * Prints a specified error message + * @param error Message of error + */ public void showError(String error) { System.out.println(error); } + /* + * Reads the user input and returns a String + * @return User input + */ public String readCommand() { String userCmd = sc.next(); return userCmd; } + /* + * Returns the description of Task specified in the user input + * Use only after reading the initial command input of the user + */ public String readDesc() { String desc = sc.nextLine(); return desc; } + /* + * Returns the index of the Task specified in the user input + * Use only after reading the initial command input of the user + */ public int readIndex() { int index = sc.nextInt(); return index;