diff --git a/docs/README.md b/docs/README.md index 2375cea7f7..4805cae976 100644 --- a/docs/README.md +++ b/docs/README.md @@ -93,15 +93,15 @@ Using the command with no arguments displays the index numbers of all tasks, to **Example usage (with and without optional arguments):** -`list` - Standard list command (no arguments) +`list` - List all items (no arguments) `list /date DD/MM/YYYY` - Search for tasks at date -`list /name taskname` - Search for tasks with name +`list /name taskname` - Search for tasks that contains name Arguments can be stacked, order of arguments does not matter. -`list /name taskname /date DD/MM/YYYY` - Search for tasks with specified name AND date +`list /name taskname /date DD/MM/YYYY` - Search for tasks that contains specified name AND date * `DD/MM/YYYY` - Date and month can be 1-2 digits long diff --git a/src/main/java/duke/command/Command.java b/src/main/java/duke/command/Command.java index 707dc82555..b5334a2dee 100644 --- a/src/main/java/duke/command/Command.java +++ b/src/main/java/duke/command/Command.java @@ -1,9 +1,5 @@ package duke.command; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; - import duke.ui.Ui; /** @@ -28,9 +24,9 @@ public abstract class Command { */ @Override public String toString() { - assert commandName != null: "Command Name cannot be null"; - assert description != null: "Description cannot be null"; - assert arguments != null: "Arguments cannot be null"; + assert commandName != null : "Command Name cannot be null"; + assert description != null : "Description cannot be null"; + assert arguments != null : "Arguments cannot be null"; StringBuilder argString = new StringBuilder(); for (String arg : arguments) { diff --git a/src/main/java/duke/command/CommandAddDeadline.java b/src/main/java/duke/command/CommandAddDeadline.java index 4c282f185c..c351cd5111 100644 --- a/src/main/java/duke/command/CommandAddDeadline.java +++ b/src/main/java/duke/command/CommandAddDeadline.java @@ -4,7 +4,7 @@ import java.time.format.DateTimeParseException; import duke.ui.Ui; -import duke.util.DukeParser; +import duke.util.DateTimeUtils; import task.TaskDeadline; import task.TaskList; @@ -42,7 +42,7 @@ public String execute() { try { return taskList.add(new TaskDeadline( input[0], - DukeParser.getDate(input[1]), + DateTimeUtils.getDate(input[1]), input[2], false, LocalDateTime.now())); diff --git a/src/main/java/duke/command/CommandAddEvent.java b/src/main/java/duke/command/CommandAddEvent.java index 45abd5cecd..73261749e2 100644 --- a/src/main/java/duke/command/CommandAddEvent.java +++ b/src/main/java/duke/command/CommandAddEvent.java @@ -4,7 +4,7 @@ import java.time.format.DateTimeParseException; import duke.ui.Ui; -import duke.util.DukeParser; +import duke.util.DateTimeUtils; import task.TaskEvent; import task.TaskList; @@ -42,7 +42,7 @@ public String execute() { try { return taskList.add(new TaskEvent( input[0], - DukeParser.getDate(input[1]), + DateTimeUtils.getDate(input[1]), input[2], false, LocalDateTime.now())); diff --git a/src/main/java/duke/command/CommandAddTodo.java b/src/main/java/duke/command/CommandAddTodo.java index 197d9bbe95..6329cc377f 100644 --- a/src/main/java/duke/command/CommandAddTodo.java +++ b/src/main/java/duke/command/CommandAddTodo.java @@ -1,10 +1,10 @@ package duke.command; +import java.time.LocalDateTime; + import task.TaskList; import task.TaskTodo; -import java.time.LocalDateTime; - /** * Command to add a to-do. */ diff --git a/src/main/java/duke/command/CommandExit.java b/src/main/java/duke/command/CommandExit.java index ca99668e6f..6d1c8d2034 100644 --- a/src/main/java/duke/command/CommandExit.java +++ b/src/main/java/duke/command/CommandExit.java @@ -1,11 +1,11 @@ package duke.command; -import duke.ui.Ui; -import javafx.application.Platform; - import java.util.Timer; import java.util.TimerTask; +import duke.ui.Ui; +import javafx.application.Platform; + /** * Command to exit program. */ @@ -26,7 +26,9 @@ public CommandExit() { @Override public String execute() { new Timer().schedule(new TimerTask() { - public void run () { Platform.exit(); } + public void run () { + Platform.exit(); + } }, 2000); return Ui.goodByeMessage(); diff --git a/src/main/java/duke/command/CommandList.java b/src/main/java/duke/command/CommandList.java index 80c1feb418..ebb651739e 100644 --- a/src/main/java/duke/command/CommandList.java +++ b/src/main/java/duke/command/CommandList.java @@ -6,7 +6,7 @@ import java.util.function.Predicate; import duke.ui.Ui; -import duke.util.DukeParser; +import duke.util.DateTimeUtils; import task.Task; import task.TaskList; @@ -93,7 +93,7 @@ private ArrayList> listStringToFilter(String stringToParse) .toLowerCase().contains(arg.toLowerCase())); break; case ("date"): - LocalDate date = DukeParser.getDate(arg); + LocalDate date = DateTimeUtils.getDate(arg); results.add(task -> task.isDate(date)); break; default: @@ -105,7 +105,7 @@ private ArrayList> listStringToFilter(String stringToParse) } private String getCommand(String str) - throws IllegalArgumentException{ + throws IllegalArgumentException { int index = getCommandArgumentSplitIndex(str); return str.substring(0, index); } diff --git a/src/main/java/duke/command/CommandSort.java b/src/main/java/duke/command/CommandSort.java index 0753729cf5..62a9da2f80 100644 --- a/src/main/java/duke/command/CommandSort.java +++ b/src/main/java/duke/command/CommandSort.java @@ -1,17 +1,15 @@ package duke.command; -import java.time.LocalDate; import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Comparator; -import java.util.function.Predicate; import duke.ui.Ui; import task.Task; import task.TaskList; /** - * Command to sort the tasklist + * Command to sort the task list */ public class CommandSort extends Command { @@ -21,18 +19,18 @@ public class CommandSort extends Command { /** * Constructor for this command. * - * @param taskList Task list to list. + * @param taskList Task list to sort. * @param sortFilters Un-parsed list of filters. */ public CommandSort(TaskList taskList, String sortFilters) { this.commandName = "sort /name /date"; - this.description = "Sorts list based on arguments provided. Having no arguments would default to" + - " sorting by /name. If there is more than 1 argument, the list will be sorted by the first." + - "followed by the second, etc..."; + this.description = "Sorts list based on arguments provided. Having no arguments would default to" + + " sorting by /name. If there is more than 1 argument, the list will be sorted by the first." + + "followed by the second, etc..."; this.arguments = new String[]{ - "/name Optional argument to sort by name", - "/date Optional argument to sort by date", - "/done Optional argument to sort by completion status" + "/name Optional argument to sort by name", + "/date Optional argument to sort by date", + "/done Optional argument to sort by completion status" }; this.taskList = taskList; @@ -41,7 +39,6 @@ public CommandSort(TaskList taskList, String sortFilters) { /** * Lists out tasks based on given filters. - * TODO */ @Override public String execute() { @@ -57,7 +54,7 @@ public String execute() { * Gets arguments from a multi-argument string. * * @param stringToParse String to parse. - * @return Arraylist with all filters to use to display list. + * @return Arraylist with all filters to use to sort list. * @throws DateTimeParseException Thrown if error in parsing dates. * @throws IllegalArgumentException Thrown if an argument is in a wrong format. */ @@ -123,4 +120,4 @@ private Comparator createDoneComparator() { private Comparator createAddedComparator() { return Comparator.comparing(Task::getAdded); } -} \ No newline at end of file +} diff --git a/src/main/java/duke/ui/ErrorDialog.java b/src/main/java/duke/ui/ErrorDialog.java index 6fd3dbf5b5..1d09f5dc40 100644 --- a/src/main/java/duke/ui/ErrorDialog.java +++ b/src/main/java/duke/ui/ErrorDialog.java @@ -1,19 +1,12 @@ package duke.ui; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; +import java.io.IOException; + import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; -import javafx.geometry.Pos; -import javafx.scene.Node; import javafx.scene.control.Label; -import javafx.scene.image.Image; -import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; -import java.io.IOException; -import java.util.Collections; - /** * This control represents a dialog box consisting of an error message to be displayed * more prominently than a normal message. diff --git a/src/main/java/duke/ui/MainWindow.java b/src/main/java/duke/ui/MainWindow.java index a5424f54c9..0a59c7cc8f 100644 --- a/src/main/java/duke/ui/MainWindow.java +++ b/src/main/java/duke/ui/MainWindow.java @@ -39,8 +39,8 @@ public class MainWindow extends AnchorPane { */ @FXML public void initialize() { - assert this.getClass().getResourceAsStream(USER_IMAGE_LOCATION) != null:" User Image not found"; - assert this.getClass().getResourceAsStream(USER_IMAGE_LOCATION) != null:" Duke Image not found"; + assert this.getClass().getResourceAsStream(USER_IMAGE_LOCATION) != null : " User Image not found"; + assert this.getClass().getResourceAsStream(USER_IMAGE_LOCATION) != null : " Duke Image not found"; userImage = new Image(this.getClass().getResourceAsStream(USER_IMAGE_LOCATION)); dukeImage = new Image(this.getClass().getResourceAsStream(DUKE_IMAGE_LOCATION)); diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 817cdb1b99..5b692ebe5e 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -71,6 +71,13 @@ public static String messageListSize(int size) { : "There are " + size + " tasks in your list"; } + /** + * Displays message when toggling the completion status of a task. + * + * @param isDone New status of task. + * @param task Task in question. + * @return Corresponding message string. + */ public static String messageToggleDone(boolean isDone, Task task) { return (isDone ? "Sugoi! Duke-san marked this task as done!" diff --git a/src/main/java/duke/util/DateTimeUtils.java b/src/main/java/duke/util/DateTimeUtils.java new file mode 100644 index 0000000000..7ebebe1972 --- /dev/null +++ b/src/main/java/duke/util/DateTimeUtils.java @@ -0,0 +1,35 @@ +package duke.util; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +public class DateTimeUtils { + + /** + * Takes in a String date and returns its corresponding LocalDate object. + * + * @param date String date in format DD/MM/YYYY, with 1-2 digits from Day and Month. + * @return LocalDate object with the corresponding day, month and year. + * @throws DateTimeParseException Thrown if date passed is an invalid one. + */ + public static LocalDate getDate(String date) throws DateTimeParseException { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy"); + return LocalDate.parse(date, formatter); + } + + /** + * Checks if a time is in an invalid format + * + * @param time Time to check + * @return False if the time is valid, true if invalid (outside the range of 0000-2359) + */ + public static boolean checkInvalidTime(String time) { + if (time.equals("")) { + return false; + } + + int timeInt = Integer.parseInt(time); + return (timeInt >= 2400 || timeInt < 0); + } +} diff --git a/src/main/java/duke/util/DukeParser.java b/src/main/java/duke/util/DukeParser.java index bc343537f3..7fe7986d5f 100644 --- a/src/main/java/duke/util/DukeParser.java +++ b/src/main/java/duke/util/DukeParser.java @@ -1,12 +1,19 @@ package duke.util; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import duke.command.*; +import duke.command.Command; +import duke.command.CommandAddDeadline; +import duke.command.CommandAddEvent; +import duke.command.CommandAddTodo; +import duke.command.CommandDelete; +import duke.command.CommandDone; +import duke.command.CommandExit; +import duke.command.CommandHelp; +import duke.command.CommandInvalid; +import duke.command.CommandList; +import duke.command.CommandSort; import task.TaskList; /** @@ -46,7 +53,7 @@ public DukeParser(TaskList tasks) { * @param input String input from the Listener given by the User. */ public Command parseInput(String input) { - assert input != null: "Input to parse cannot be null"; + assert input != null : "Input to parse cannot be null"; if (input.equals("gubbai")) { return new CommandExit(); @@ -87,18 +94,4 @@ public Command parseInput(String input) { return new CommandInvalid(input); } } - - /** - * Takes in a String date and returns its corresponding LocalDate object. - * - * @param date String date in format DD/MM/YYYY, with 1-2 digits from Day and Month. - * @return LocalDate object with the corresponding day, month and year. - * @throws DateTimeParseException Thrown if date passed is an invalid one. - */ - public static LocalDate getDate(String date) throws DateTimeParseException { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy"); - return LocalDate.parse(date, formatter); - } - - } diff --git a/src/main/java/duke/util/Storage.java b/src/main/java/duke/util/Storage.java index 80b4e965d1..0772af5ddf 100644 --- a/src/main/java/duke/util/Storage.java +++ b/src/main/java/duke/util/Storage.java @@ -97,11 +97,11 @@ private static Task stringToTask(String task) throws ParseException { switch (taskType) { case "T": // Create new To-do - assert args.length == 3: "Invalid loaded task"; + assert args.length == 3 : "Invalid loaded task"; return new TaskTodo(args[2], args[1].equals("1"), LocalDateTime.parse(args[3])); case "D": // Create new Deadline - assert args.length == 5 || args.length == 6: "Invalid loaded task"; + assert args.length == 5 || args.length == 6 : "Invalid loaded task"; return args.length == 5 ? new TaskDeadline(args[2], LocalDate.parse(args[3]), null, !args[1].equals("0"), LocalDateTime.parse(args[4])) @@ -109,7 +109,7 @@ private static Task stringToTask(String task) throws ParseException { !args[1].equals("0"), LocalDateTime.parse(args[5])); case "E": // Create new Event - assert args.length == 5 || args.length == 6: "Invalid loaded task"; + assert args.length == 5 || args.length == 6 : "Invalid loaded task"; return args.length == 5 ? new TaskEvent(args[2], LocalDate.parse(args[3]), null, !args[1].equals("0"), LocalDateTime.parse(args[4])) diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 722c5d6a3b..6990b39633 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -44,28 +44,38 @@ public String getDescription() { return description; } + /** + * Obtains the completion status of the task. + * + * @return Boolean to check if task is done. + */ public boolean getDone() { return isDone; } + /** + * Obtains the date in which the task was created. + * + * @return LocalDateTime which indicates when the task was created. + */ public LocalDateTime getAdded() { return dateTimeAdded; } - - + /** + * Returns a LocalDate if present in task. + * + * @return LocalDate of which the task occurs. + */ public abstract LocalDate getDate(); + /** + * Returns a String representing time if present in task. + * + * @return Time of which task occurs. + */ public abstract String getTime(); - //TODO - protected static boolean checkInvalidTime(String time) { - if (time.equals("")) return false; - - int timeInt = Integer.parseInt(time); - return ( timeInt >= 2400 || timeInt < 0); - } - /** * Represents string of Task. * diff --git a/src/main/java/task/TaskDeadline.java b/src/main/java/task/TaskDeadline.java index 5ddbb1b844..420d7ec5ed 100644 --- a/src/main/java/task/TaskDeadline.java +++ b/src/main/java/task/TaskDeadline.java @@ -1,12 +1,12 @@ package task; -import duke.ui.Ui; - import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeParseException; import java.util.Optional; +import duke.util.DateTimeUtils; + /** * Deadline task event. */ @@ -30,7 +30,7 @@ public TaskDeadline(String description, LocalDate date, String time, boolean don .map(String::strip) .orElse(""); - if (checkInvalidTime(this.time)) { + if (DateTimeUtils.checkInvalidTime(this.time)) { throw new DateTimeParseException("INVALID TIME", this.time, 0); } } @@ -76,11 +76,21 @@ public boolean isDate(LocalDate date) { return date.equals(by); } + /** + * Returns a LocalDate of the deadline. + * + * @return LocalDate of which the deadline is due. + */ @Override public LocalDate getDate() { return by; } + /** + * Returns a String representing time of deadline if present. + * + * @return Time of which deadline is due, "" if no time is present + */ @Override public String getTime() { return time; diff --git a/src/main/java/task/TaskEvent.java b/src/main/java/task/TaskEvent.java index 02d2dac546..0577d7713b 100644 --- a/src/main/java/task/TaskEvent.java +++ b/src/main/java/task/TaskEvent.java @@ -5,6 +5,8 @@ import java.time.format.DateTimeParseException; import java.util.Optional; +import duke.util.DateTimeUtils; + /** * Event task event. */ @@ -27,7 +29,7 @@ public TaskEvent(String description, LocalDate date, String time, boolean done, .map(String::strip) .orElse(""); - if (checkInvalidTime(this.time)) { + if (DateTimeUtils.checkInvalidTime(this.time)) { throw new DateTimeParseException("INVALID TIME", this.time, 0); } } @@ -73,11 +75,21 @@ public boolean isDate(LocalDate date) { return date.equals(at); } + /** + * Returns a LocalDate date of Event. + * + * @return LocalDate of which the event occurs. + */ @Override public LocalDate getDate() { return at; } + /** + * Returns a String representing time if present in event. + * + * @return Time of which event occurs. "" if no time is present + */ @Override public String getTime() { return time; diff --git a/src/main/java/task/TaskList.java b/src/main/java/task/TaskList.java index 4996f0bf4d..78c5d876da 100644 --- a/src/main/java/task/TaskList.java +++ b/src/main/java/task/TaskList.java @@ -29,7 +29,7 @@ public TaskList() { * @param tasks Existing task list to use. */ public TaskList(ArrayList tasks) { - assert tasks != null: "Tasks must be an arraylist, not null"; + assert tasks != null : "Tasks must be an arraylist, not null"; this.tasks = tasks; } @@ -51,8 +51,9 @@ public String add(Task task) { } tasks.add(task); - assert tasks.contains(task): "Task list should contain added task"; + assert tasks.contains(task) : "Task list should contain added task"; Storage.saveList(tasks); + return Ui.messageAddTask(task, getSize()); } @@ -83,7 +84,7 @@ public String toggleDone(int index) { public String delete(int index) { try { Task removedTask = tasks.remove(index - 1); - assert !tasks.contains(removedTask): "Task list should not contain removed task"; + assert !tasks.contains(removedTask) : "Task list should not contain removed task"; Storage.saveList(tasks); return Ui.messageRemoveTask(removedTask, getSize()); @@ -95,7 +96,7 @@ public String delete(int index) { /** * Displays the list of tasks based on predicate filter. * - * @param filters Predicates to filter; return true to display task. + * @param filters Array of Predicates to filter; return true to display task. */ public String displayList(ArrayList> filters) { if (tasks.size() == 0) { @@ -121,6 +122,12 @@ public String displayList(ArrayList> filters) { return result.toString(); } + /** + * Sorts the list of tasks based on the Comparator filters. + * Saves the list afterwards. + * + * @param filters Array of Comparators to use. + */ public void sort(ArrayList> filters) { for (int i = filters.size() - 1; i >= 0; i--) { tasks.sort(filters.get(i)); diff --git a/src/main/java/task/TaskTodo.java b/src/main/java/task/TaskTodo.java index 7d395efa50..2e506e1ba5 100644 --- a/src/main/java/task/TaskTodo.java +++ b/src/main/java/task/TaskTodo.java @@ -55,14 +55,26 @@ public boolean isDate(LocalDate date) { return false; } + /** + * Returns a LocalDate if present in task. + * Since a to-do has no date, it should be prioritised after deadlines or events + * that do contain dates, as they are most likely to be of higher priority. + * + * @return LocalDate.MAX to represent the lack of date. + */ @Override public LocalDate getDate() { return LocalDate.MAX; } + /** + * Returns a String representing time if present in task. + * + * @return Since no time is present, to-do returns an empty string. + */ @Override public String getTime() { - return "2400"; + return ""; } }