diff --git a/build.gradle b/build.gradle index d31250e089..a5647311a5 100644 --- a/build.gradle +++ b/build.gradle @@ -61,4 +61,5 @@ shadowJar { run{ standardInput = System.in + enableAssertions = true } diff --git a/data/tasks.txt b/data/tasks.txt index df99c40787..e69de29bb2 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -1 +0,0 @@ -D | 0 | task 1 | Jan. 1 2023 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e77128dae2..d3020ff5f6 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -8,12 +8,15 @@ import duke.storage.Storage; import duke.storage.TaskList; +/** + * Duke class + */ public class Duke { public static final Scanner SCANNER = new Scanner(System.in); public static final String FILEPATH = "./data/tasks.txt"; private TaskList taskList; - + /** * Constructs the Duke class. * @@ -51,12 +54,12 @@ public String getResponse(String input) { * * @param args arguments */ - public static void main(String[] args) throws DukeException{ + public static void main(String[] args) throws DukeException { new Duke("data/tasks.txt"); Ui.greet(); String input = SCANNER.nextLine(); - while(!input.equals("bye")) { + while (!input.equals("bye")) { try { Parser.parse(input); } catch (DukeException e) { diff --git a/src/main/java/duke/helper/DukeException.java b/src/main/java/duke/helper/DukeException.java index 93d3c57bf0..d7d123bb9a 100644 --- a/src/main/java/duke/helper/DukeException.java +++ b/src/main/java/duke/helper/DukeException.java @@ -1,5 +1,8 @@ package duke.helper; +/** + * Duke Exception class + */ public class DukeException extends Exception { /** @@ -10,7 +13,7 @@ public class DukeException extends Exception { public DukeException(String message) { super(message); } - + /** * returns the detail message of this throwable * @@ -27,8 +30,7 @@ public String getMessage() { * @return the message of the exception */ @Override - public String toString() - { + public String toString() { return ":( Oh no! " + super.getMessage(); } -} \ No newline at end of file +} diff --git a/src/main/java/duke/helper/Parser.java b/src/main/java/duke/helper/Parser.java index abc0ec5222..8a6ab8a35f 100644 --- a/src/main/java/duke/helper/Parser.java +++ b/src/main/java/duke/helper/Parser.java @@ -12,9 +12,12 @@ import duke.tasks.Task; import duke.tasks.ToDo; +/** + * Parser class for taking in user input and analyzing it + */ public class Parser { private static TaskList taskList; - + /** * initializes the task list * @@ -33,30 +36,30 @@ public static void setTaskList(TaskList tasks) { public static String parse(String input) throws DukeException { String command = input.split("\\s")[0].toUpperCase(); String content = input.replace(input.split("\\s")[0], ""); - switch(command){ - case "BYE": - return parseBye(content); - case "CLEAR": - return parseClear(content); - case "DELETE": - return parseDelete(content); - case "MARK": - return parseMark(content); - case "UNMARK": - return parseUnmark(content); - case "FIND": - return parseFind(content); - case "PRINT": - return parsePrint(content); - case "TODO": - return parseTodo(content); - case "EVENT": - return parseEvent(content); - case "DEADLINE": - return parseDeadline(content); - default: - throw new DukeException("Sorry, I don't recognize this command. Please try again."); - } + switch(command) { + case "BYE": + return parseBye(content); + case "CLEAR": + return parseClear(content); + case "DELETE": + return parseDelete(content); + case "MARK": + return parseMark(content); + case "UNMARK": + return parseUnmark(content); + case "FIND": + return parseFind(content); + case "PRINT": + return parsePrint(content); + case "TODO": + return parseTodo(content); + case "EVENT": + return parseEvent(content); + case "DEADLINE": + return parseDeadline(content); + default: + throw new DukeException("Sorry, I don't recognize this command. Please try again."); + } } /** @@ -71,7 +74,6 @@ private static String parseBye(String content) throws DukeException { } else { throw new DukeException("The clear command will clear all stored tasks, please try again."); } - } /** @@ -208,23 +210,22 @@ private static String parseTodo(String content) throws DukeException { */ private static String parseEvent(String content) throws DukeException { //when user didn't provide title and start & end time - if (content.isBlank() || content.isEmpty() - || !content.contains(" /from ") || !content.contains(" /to ") || content == null) { + if (content.isBlank() || content.isEmpty() + || !content.contains(" /from ") || !content.contains(" /to ") || content == null) { throw new DukeException("Sorry, this event must have a title, start time, and end time."); } String[] event = new String[3]; try { event[0] = content.substring(1, content.indexOf(" /")); - event[1] = content.substring(content.indexOf("/from") + 6, + event[1] = content.substring(content.indexOf("/from") + 6, content.indexOf(" /to")); event[2] = content.substring(content.indexOf("/to") + 4); - } - catch (StringIndexOutOfBoundsException e) { + } catch (StringIndexOutOfBoundsException e) { return ("Sorry, this event must have a title, start time, and end time."); } //when user provide empty title if (event[0].isBlank() || event[0].isEmpty() || event[0] == null) { - throw new DukeException("Sorry, the event must have a title."); + throw new DukeException("Sorry, the event must have a title."); } //when user didn't provide the starting time if (event[1].isBlank() || event[1].isEmpty() || event[1] == null) { @@ -257,8 +258,7 @@ private static String parseDeadline(String content) throws DukeException { String[] ddl = new String[2]; try { ddl = content.split(" /by "); - } - catch (ArrayIndexOutOfBoundsException e) { + } catch (ArrayIndexOutOfBoundsException e) { return "Sorry, this deadline task must have a title and a deadline."; } //when user provide empty title @@ -308,6 +308,12 @@ private static String formatTime(String input) { } } + /** + * dummy method for testing parse + * + * @param input the user input + * @return the detected command + */ public static String parseTest(String input) throws DukeException { String testCommand = input.split("\\s")[0].toUpperCase(); return testCommand; diff --git a/src/main/java/duke/helper/Ui.java b/src/main/java/duke/helper/Ui.java index 95b80026f6..65071c59f1 100644 --- a/src/main/java/duke/helper/Ui.java +++ b/src/main/java/duke/helper/Ui.java @@ -1,5 +1,8 @@ package duke.helper; +/** + * Ui class + */ public class Ui { private static final String NAME = "Duke Max"; diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index d178ed73b1..ee81f4b29f 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -14,6 +14,9 @@ import duke.helper.Ui; import duke.tasks.Task; +/** + * Storage class for import and export between txt file and TaskList. + */ public class Storage { private static String path; private static ArrayList taskList = new ArrayList(); @@ -26,7 +29,7 @@ public class Storage { public static void setPath(String filePath) { path = filePath; } - + /** * save the arraylist of tasks in txt format * @@ -35,7 +38,7 @@ public static void setPath(String filePath) { public static void save(ArrayList tasks) { try (PrintWriter printwriter = new PrintWriter(new FileWriter(path))) { for (Task task : tasks) { - printwriter.write(task.toFile() +"\n"); + printwriter.write(task.toFile() + "\n"); } } catch (IOException e) { System.out.println("There is an error saving this file: " + e.getMessage()); @@ -47,7 +50,7 @@ public static void save(ArrayList tasks) { * * @return an arraylist translated from the txt file */ - public static ArrayList load() throws DukeException{ + public static ArrayList load() throws DukeException { handleMissing(path); try (BufferedReader reader = new BufferedReader(new FileReader(path))) { @@ -69,21 +72,20 @@ public static ArrayList load() throws DukeException{ * @param testPath the file path to test if exist */ private static void handleMissing(String testPath) { - try{ + try { //if directory or path doesn't exist - Path directoryPath = Paths.get(".", "data"); + Path directoryPath = Paths.get(".", "data"); if (!Files.exists(directoryPath)) { Files.createDirectories(directoryPath); } - Path path = directoryPath.resolve("tasks.txt"); + Path path = directoryPath.resolve("tasks.txt"); if (!Files.exists(path)) { - Files.createFile(path); + Files.createFile(path); } } catch (IOException e) { e.printStackTrace(); System.out.println("There is an error loading file: " + e.getMessage()); } } - } diff --git a/src/main/java/duke/storage/TaskList.java b/src/main/java/duke/storage/TaskList.java index bf7671cd46..4428fed914 100644 --- a/src/main/java/duke/storage/TaskList.java +++ b/src/main/java/duke/storage/TaskList.java @@ -5,6 +5,9 @@ import duke.helper.Ui; import duke.tasks.Task; +/** + * TskList that runs task-related commands and stores task in an arrayList. + */ public class TaskList { private static ArrayList taskList; @@ -43,8 +46,8 @@ public String clear() { * @return the bot response */ public String delete(int num) { - String[] messageList = {"I've removed this task:", taskList.get(num - 1).getStatus(), - "Current # of " + plural(taskList.size() - 1, "task") + ": " + (taskList.size() - 1)}; + String taskNumber = "Current # of " + plural(taskList.size() - 1, "task") + ": " + (taskList.size() - 1); + String[] messageList = {"I've removed this task:", taskList.get(num - 1).getStatus(), taskNumber}; taskList.remove(num - 1); Storage.save(taskList); return Ui.print(messageList); @@ -73,9 +76,9 @@ public String find(String keyword) { int index = 1; String message = ""; for (Task task: taskList) { - if(task.getTask().contains(keyword)) { + if (task.getTask().contains(keyword)) { message = message + (index + ". " + task.getStatus()) + "\n"; - index ++; + index++; } } index -= 1; @@ -97,7 +100,7 @@ public String print() { String message = ""; for (Task task: taskList) { message = message + (index + ". " + task.getStatus()) + "\n"; - index ++; + index++; } message = message + ("Current # of " + plural(taskList.size(), "task") + ": " + taskList.size()); return Ui.print(message); @@ -113,9 +116,9 @@ public String print(String time) { int index = 1; String message = ""; for (Task task: taskList) { - if(task.getTime() != null && task.getTime().contains(time)) { + if (task.getTime() != null && task.getTime().contains(time)) { message = message + (index + ". " + task.getStatus()) + "\n"; - index ++; + index++; } } index -= 1; @@ -136,17 +139,19 @@ public String print(String time) { public String add(Task input) { taskList.add(input); Storage.save(taskList); - String[] messageList = {("Got it! This task has been added: "), - (input.getStatus()), - ("Current # of " + plural(taskList.size(), "task") + ": " + taskList.size())}; + String taskNumber = "Current # of " + plural(taskList.size(), "task") + ": " + taskList.size(); + String[] messageList = {("Got it! This task has been added: "), (input.getStatus()), taskNumber}; return Ui.print(messageList); } - + /** + * dummy test method for add command + * + * @param input the task to add to the taskList + */ public void addTest(Task input) { taskList.add(input); - String[] messageList = {("Got it! This task has been added: "), - (input.getStatus()), - ("Current # of " + plural(taskList.size(), "task") + ": " + taskList.size())}; + String taskNumber = "Current # of " + plural(taskList.size(), "task") + ": " + taskList.size(); + String[] messageList = {("Got it! This task has been added: "), (input.getStatus()), taskNumber}; Ui.print(messageList); } diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index a8ae774102..b6e8aa9204 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -1,5 +1,8 @@ package duke.tasks; +/** +* Deadline class with due time +*/ public class Deadline extends Task { private String deadline; @@ -13,7 +16,7 @@ public Deadline(String task, String deadline) { super(task); this.deadline = deadline; } - + /** * returns the status of the deadline task * @@ -40,4 +43,4 @@ public String toFile() { return super.isDone ? ("D | 1 | " + super.task + " | " + this.deadline) : ("D | 0 | " + super.task + " | " + this.deadline); } -} \ No newline at end of file +} diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index d22c7de320..24c86d98f0 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -1,5 +1,8 @@ package duke.tasks; +/** +* Event class with start and end time +*/ public class Event extends Task { private String from; private String to; @@ -23,7 +26,7 @@ public Event(String task, String from, String to) { * @return a formatted string of the status of the event */ @Override - public String getStatus(){ + public String getStatus() { String time = "(from: " + from + " to: " + to + ")"; return "[Event]" + super.getStatus() + " " + time; } @@ -43,4 +46,4 @@ public String toFile() { return super.isDone ? ("E | 1 | " + super.task + " | " + this.from + "-" + this.to) : ("E | 0 | " + super.task + " | " + this.from + "-" + this.to); } -} \ No newline at end of file +} diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 5274200caa..c9e3ff9eec 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -2,9 +2,12 @@ import duke.helper.Ui; +/** +* Task class with task description and completeness indicator. +*/ public class Task { - String task; - boolean isDone; + protected String task; + protected boolean isDone; /** * constructs the task class @@ -12,11 +15,11 @@ public class Task { * @param task the description of the task * @param isDone a boolean value that indicate whether the task is done or not */ - public Task(String task){ + public Task(String task) { this.task = task; this.isDone = false; } - + /** * getter method that return the description of the task * @@ -49,14 +52,14 @@ public String getTime() { public String markItem(Boolean isDone) { this.isDone = isDone; if (this.isDone) { - String[] messageList = {("Nice! I've marked this task as complete:"), - (this.getStatus()), - ("Here's a lollipop.")}; + String greet = "Nice! I've marked this task as complete:"; + String encouragement = "Here's a lollipop."; + String[] messageList = {greet, this.getStatus(), encouragement}; return Ui.print(messageList); } else { - String[] messageList = {("OK, I've marked this task as incomplete yet:"), - (this.getStatus()), - ("Keep up with the good work.")}; + String greet = "OK, I've marked this task as incomplete yet:"; + String encouragement = "Keep up with the good work."; + String[] messageList = {greet, this.getStatus(), encouragement}; return Ui.print(messageList); } } @@ -83,14 +86,14 @@ public static Task convertStringToTask(String text) { return temp; } else if (type.equals("D")) { String ddl = tasks[3].trim(); - Deadline temp = new Deadline(task, ddl); + Deadline temp = new Deadline(task, ddl); temp.markItem(isCompleted); return temp; } else if (type.equals("E")) { String[] timeDuration = tasks[3].trim().split("-"); String from = timeDuration[0].trim(); String to = timeDuration[1].trim(); - Event temp = new Event(task, from, to); + Event temp = new Event(task, from, to); temp.markItem(isCompleted); return temp; } else { diff --git a/src/main/java/duke/tasks/ToDo.java b/src/main/java/duke/tasks/ToDo.java index ed9a41b782..0b355eff1e 100644 --- a/src/main/java/duke/tasks/ToDo.java +++ b/src/main/java/duke/tasks/ToDo.java @@ -1,5 +1,8 @@ package duke.tasks; +/** +* ToDo class +*/ public class ToDo extends Task { /** @@ -25,4 +28,4 @@ public String getStatus() { public String toFile() { return super.isDone ? ("T | 1 | " + super.task) : ("T | 0 | " + super.task); } -} \ No newline at end of file +} diff --git a/src/test/java/duke/ParserTest.java b/src/test/java/duke/ParserTest.java index d8f8ad8fe6..f0ccf8e3be 100644 --- a/src/test/java/duke/ParserTest.java +++ b/src/test/java/duke/ParserTest.java @@ -1,23 +1,24 @@ package duke; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + import duke.helper.DukeException; import duke.helper.Parser; -public class ParserTest{ +public class ParserTest { @Test - public void todoTest() throws DukeException{ + public void todoTest() throws DukeException { String input = "todo task 1"; String command = Parser.parseTest(input); assertEquals(command, "TODO"); } @Test - public void deadlineTest() throws DukeException{ + public void deadlineTest() throws DukeException { String input = "deadline task 2 /by 2023-01-01"; String command = Parser.parseTest(input); assertEquals(command, "DEADLINE"); } -} \ No newline at end of file +} diff --git a/src/test/java/duke/TodoTest.java b/src/test/java/duke/TodoTest.java index 1bd1ba2e0f..b1a137dc37 100644 --- a/src/test/java/duke/TodoTest.java +++ b/src/test/java/duke/TodoTest.java @@ -1,8 +1,9 @@ package duke; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + import duke.storage.TaskList; import duke.tasks.Task; import duke.tasks.ToDo;