From dcc482955a511bbc6f4c00cf3af057ae530e67ef Mon Sep 17 00:00:00 2001 From: QianChangru Date: Sun, 10 Sep 2023 09:17:13 +0800 Subject: [PATCH 1/5] Add part of Java Doc. --- src/main/java/duke/Deadline.java | 7 +++++++ src/main/java/duke/Event.java | 6 ++++++ src/main/java/duke/Parser.java | 9 +++++++++ src/main/java/duke/Task.java | 20 +++++++++++++++++++- src/main/java/duke/ToDo.java | 6 ++++++ 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main/java/duke/Deadline.java b/src/main/java/duke/Deadline.java index 91b42caf31..fefcc623a2 100644 --- a/src/main/java/duke/Deadline.java +++ b/src/main/java/duke/Deadline.java @@ -4,6 +4,10 @@ import java.time.format.DateTimeFormatter; import java.util.Locale; + +/** + * Encapsulates deadline which are Tasks with start and end. + */ public class Deadline extends Task { private LocalDate by; public Deadline(String description, LocalDate by) { @@ -22,6 +26,9 @@ public String toString() { + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")"; } + /** + * Returns a String representation of the task for storage. + */ @Override public String toTxt() { return "D | " + super.toTxt() diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/Event.java index f3e45a6969..101c8a07ef 100644 --- a/src/main/java/duke/Event.java +++ b/src/main/java/duke/Event.java @@ -4,6 +4,9 @@ import java.time.format.DateTimeFormatter; import java.util.Locale; +/** + * Encapsulates events which are Tasks with start and end. + */ public class Event extends Task{ private LocalDate startTime; private LocalDate endTime; @@ -25,6 +28,9 @@ public String toString() { + " to: " + endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")"; } + /** + * Returns a String representation of the task for storage. + */ @Override public String toTxt() { return "E | " + super.toTxt() + " | " + this.startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 6c5ab3ab6d..c7106c7b7f 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -2,6 +2,9 @@ import java.time.LocalDate; +/** + * Parses the user input. + */ public class Parser { private String command; private Storage storage; @@ -14,6 +17,9 @@ public Parser(String command, Storage storage, TaskList taskList) { this.taskList = taskList; } + /** + * Parses the user input and initiate following operations. + */ public void parse() { try{ if (command.startsWith("list")) { @@ -51,6 +57,9 @@ public void parse() { } } + /** + * Returns if the parser is ended. + */ public boolean isEnd() { return this.isEnd; } diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java index b66103b33b..8fea81fb64 100644 --- a/src/main/java/duke/Task.java +++ b/src/main/java/duke/Task.java @@ -1,9 +1,17 @@ package duke; +/** + * Encapsulates tasks for the chatbot. + */ public class Task { protected String description; protected boolean isDone; + /** + * Constructs a new Task object. + * + * @param description Description of the task. + */ public Task(String description) { this.description = description; this.isDone = false; @@ -14,10 +22,17 @@ public Task(String description, boolean isDone) { this.isDone = isDone; } + /** + * Gets the status Icon of the Task. + */ public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + // X for done task + return (isDone ? "X" : " "); } + /** + * Marks the task as done. + */ public void markAsDone() { this.isDone = true; } @@ -27,6 +42,9 @@ public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } + /** + * Returns a String representation of the task for storage. + */ public String toTxt() { return (this.isDone ? "1" : "0") + " | " + this.description; } diff --git a/src/main/java/duke/ToDo.java b/src/main/java/duke/ToDo.java index d6c8f8de76..ad980857af 100644 --- a/src/main/java/duke/ToDo.java +++ b/src/main/java/duke/ToDo.java @@ -1,5 +1,8 @@ package duke; +/** + * Encapsulates todos which are Tasks. + */ public class ToDo extends Task { public ToDo(String description) { super(description); @@ -14,6 +17,9 @@ public String toString() { return "[T]" + super.toString(); } + /** + * Returns a String representation of the task for storage. + */ @Override public String toTxt() { return "T | " + super.toTxt(); From a011a9ef6f0e096ad9f323c8691cf06ecfe97766 Mon Sep 17 00:00:00 2001 From: QianChangru Date: Sun, 10 Sep 2023 09:24:11 +0800 Subject: [PATCH 2/5] Implement coding standard. --- src/main/java/duke/Deadline.java | 6 ++++-- src/main/java/duke/Event.java | 9 ++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/duke/Deadline.java b/src/main/java/duke/Deadline.java index fefcc623a2..a3520f829f 100644 --- a/src/main/java/duke/Deadline.java +++ b/src/main/java/duke/Deadline.java @@ -23,7 +23,8 @@ public Deadline(String description, boolean isDone, LocalDate by) { @Override public String toString() { return "[D]" + super.toString() + " (by: " - + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")"; + + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + + ")"; } /** @@ -32,6 +33,7 @@ public String toString() { @Override public String toTxt() { return "D | " + super.toTxt() - + " | " + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); + + " | " + + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); } } diff --git a/src/main/java/duke/Event.java b/src/main/java/duke/Event.java index 101c8a07ef..265b80bfe2 100644 --- a/src/main/java/duke/Event.java +++ b/src/main/java/duke/Event.java @@ -24,8 +24,10 @@ public Event(String description, boolean isDone, LocalDate startTime, LocalDate @Override public String toString() { - return "[E]" + super.toString() + " (from: " + startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) - + " to: " + endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")"; + return "[E]" + super.toString() + " (from: " + + startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + + " to: " + + endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")"; } /** @@ -33,7 +35,8 @@ public String toString() { */ @Override public String toTxt() { - return "E | " + super.toTxt() + " | " + this.startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + return "E | " + super.toTxt() + " | " + + this.startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + " | " + this.endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); } } From b0af1ec175701908517fed9283e79de98aa59c34 Mon Sep 17 00:00:00 2001 From: QianChangru Date: Sun, 10 Sep 2023 10:04:25 +0800 Subject: [PATCH 3/5] Update JavaDoc --- data/tasks.txt | 1 - src/main/java/duke/Duke.java | 6 +++ src/main/java/duke/Storage.java | 55 +++++++++++++++++++++------- src/main/java/duke/TaskList.java | 23 +++++++++++- src/test/java/duke/TaskListTest.java | 7 ++++ src/test/java/duke/TaskTest.java | 7 ++++ 6 files changed, 83 insertions(+), 16 deletions(-) diff --git a/data/tasks.txt b/data/tasks.txt index 170108ab9f..e69de29bb2 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -1 +0,0 @@ -0 | test0 | test0 | test \ No newline at end of file diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 43aa9c1b4e..9f74969165 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -4,6 +4,9 @@ import java.io.IOException; +/** + * Encapsulates the chat bot. + */ public class Duke { private Storage storage; @@ -24,6 +27,9 @@ public Duke(String filePath) { } } + /** + * Runs the program. + */ public void run() { Ui.welcomeMessage(); Scanner userInput = new Scanner(System.in); diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 3b9f2b95f6..c700c2f9fa 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -11,13 +11,22 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * Manages storage of the task list. + */ public class Storage { - private final String filePath; + private String filePath; public Storage(String filePath) { this.filePath = filePath; } + /** + * Loads the task list with file. + * + * @return The task list. + * @throws IOException If unable to gain input from file. + */ public ArrayList load() throws IOException { File dir = new File("./data"); if (!dir.exists()) { @@ -28,12 +37,18 @@ public ArrayList load() throws IOException { Scanner s = new Scanner(f); ArrayList tasks = new ArrayList<>(); while (s.hasNext()) { - tasks.add(addTask(s.nextLine())); + tasks.add(inputToTask(s.nextLine())); } return tasks; } - private Task addTask(String input) { + /** + * Converts the String input to Task. + + * @param input The String input. + * @return The corresponding Task. + */ + private Task inputToTask(String input) { String taskType = input.split(" \\| ")[0]; boolean isComplete = input.split(" \\| ")[1].equals("1"); String description = input.split(" \\| ")[2]; @@ -43,35 +58,47 @@ private Task addTask(String input) { LocalDate d = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); return new Deadline(description, isComplete, d); } else { - LocalDate start = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); - LocalDate end = LocalDate.parse(input.split(" \\| ")[4], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); + LocalDate start = LocalDate.parse(input.split(" \\| ")[3], + DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); + LocalDate end = LocalDate.parse(input.split(" \\| ")[4], + DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); return new Event(description, isComplete, start, end); } } - public void addOneLineToFile(ArrayList list) throws DukeException { + /** + * Adds the last task in the task list to file. + * + * @param list The task list. + * @throws DukeException If unable to write to file. + */ + public void addTheLastTaskToFile(ArrayList list) throws DukeException { try { FileWriter fw = new FileWriter(filePath, true); - if (list.size() == 1) { - fw.write(list.get(0).toTxt()); - } else { - fw.write("\n" + list.get(list.size() - 1).toTxt()); + if (list.size() != 1) { + fw.write("\n"); } + fw.write(list.get(list.size() - 1).toTxt()); fw.close(); } catch (IOException e) { throw new DukeException("Unable to write to file."); } } + /** + * Rewrites the whole file with the task list. + * + * @param list The task list. + * @throws DukeException If unable to write to file. + */ public void rewriteFile(ArrayList list) throws DukeException { try { FileWriter fw = new FileWriter(filePath); for (int i = 0; i < list.size(); i++) { - if (i == list.size() - 1) { - fw.write(list.get(i).toTxt()); - } else { - fw.write(list.get(i).toTxt() + "\n"); + fw.write(list.get(i).toTxt()); + if (i != list.size() - 1) { + fw.write("\n"); } } fw.close(); diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 57a33c6f4f..94bbaba096 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -2,6 +2,9 @@ import java.util.ArrayList; +/** + * Encapsulates task list in the chat bot. + */ public class TaskList { protected ArrayList tasks; private Storage storage; @@ -16,22 +19,40 @@ public TaskList(ArrayList tasks, Storage storage) { this.storage = storage; } + /** + * Lists all tasks in the task list. + */ public void listTasks() { Ui.listTasks(this.tasks); } + /** + * Adds one task to the task list. + * + * @param t The task to be added. + */ public void addTask(Task t) { this.tasks.add(t); Ui.addTask(t, this.tasks); - this.storage.addOneLineToFile(this.tasks); + this.storage.addTheLastTaskToFile(this.tasks); } + /** + * Deletes the task at the specified position in the task list. + * + * @param num The position starts from 0. + */ public void deleteTask(int num) { Task re = tasks.remove(num); this.storage.rewriteFile(tasks); Ui.deleteTask(re, tasks); } + /** + * Marks the task at the specified position in the task list. + * + * @param num The position starts from 0. + */ public void markTask(int num) { Task t = tasks.get(num); t.markAsDone(); diff --git a/src/test/java/duke/TaskListTest.java b/src/test/java/duke/TaskListTest.java index 283788e544..08573d6120 100644 --- a/src/test/java/duke/TaskListTest.java +++ b/src/test/java/duke/TaskListTest.java @@ -6,7 +6,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +/** + * Tests TaskList class. + */ public class TaskListTest { + + /** + * Tests addTask function. + */ @Test public void addTask() { Task t = new Task("test"); diff --git a/src/test/java/duke/TaskTest.java b/src/test/java/duke/TaskTest.java index befb1c2423..061cdaf907 100644 --- a/src/test/java/duke/TaskTest.java +++ b/src/test/java/duke/TaskTest.java @@ -4,7 +4,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +/** + * Tests task class. + */ public class TaskTest { + + /** + * Tests markAsDone function. + */ @Test public void markAsDone() { Task t = new Task("test", false); From b47f6501e167a19c1c0a765be781eafc3420ca81 Mon Sep 17 00:00:00 2001 From: QianChangru Date: Sun, 10 Sep 2023 10:43:48 +0800 Subject: [PATCH 4/5] Update JavaDoc --- src/main/java/duke/DukeException.java | 3 +++ src/main/java/duke/Ui.java | 36 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java index e20067cfb8..9ab095d0d0 100644 --- a/src/main/java/duke/DukeException.java +++ b/src/main/java/duke/DukeException.java @@ -1,5 +1,8 @@ package duke; +/** + * Encapsulates exception for the chat bot. + */ public class DukeException extends RuntimeException{ public DukeException(String message) { super(message); diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index a336d91e8c..4e5521399a 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -2,11 +2,17 @@ import java.util.ArrayList; +/** + * Encapsulates ui for chat bot. + */ public class Ui { private static void printSeparationLine() { System.out.println("____________________________________________________________"); } + /** + * Prints welcome message. + */ public static void welcomeMessage() { Ui.printSeparationLine(); System.out.println(" Hello! I'm Jarvis"); @@ -14,6 +20,11 @@ public static void welcomeMessage() { Ui.printSeparationLine(); } + /** + * Prints all tasks in task list. + * + * @param tasks The task list. + */ public static void listTasks(ArrayList tasks) { Ui.printSeparationLine(); System.out.println(" Here are the tasks in your list:"); @@ -25,6 +36,11 @@ public static void listTasks(ArrayList tasks) { Ui.printSeparationLine(); } + /** + * Prints message for marking a task. + * + * @param t The task marked. + */ public static void markTask(Task t) { Ui.printSeparationLine(); System.out.println(" Nice! I've marked this task as done:"); @@ -32,6 +48,12 @@ public static void markTask(Task t) { Ui.printSeparationLine(); } + /** + * Prints the message for adding a task. + * + * @param t The task added. + * @param taskList The task list of the task. + */ public static void addTask(Task t, ArrayList taskList) { Ui.printSeparationLine(); System.out.println(" Got it. I've added this task:"); @@ -40,6 +62,12 @@ public static void addTask(Task t, ArrayList taskList) { Ui.printSeparationLine(); } + /** + * Prints the message for deleting a task. + * + * @param t The task deleted. + * @param taskList The task list of the task. + */ public static void deleteTask(Task t, ArrayList taskList) { Ui.printSeparationLine(); System.out.println(" Noted. I've removed this task:"); @@ -48,12 +76,20 @@ public static void deleteTask(Task t, ArrayList taskList) { Ui.printSeparationLine(); } + /** + * Prints the exception message. + * + * @param e The exception printed. + */ public static void printException(DukeException e) { Ui.printSeparationLine(); System.out.println(" " + e.getMessage()); Ui.printSeparationLine(); } + /** + * Prints farewell message. + */ public static void farewellMessage() { Ui.printSeparationLine(); System.out.println(" Bye. Hope to see you again soon!"); From c529dc9daf400749bcdb8e78040504ce545525c8 Mon Sep 17 00:00:00 2001 From: QianChangru Date: Sun, 10 Sep 2023 11:24:09 +0800 Subject: [PATCH 5/5] Start Level 9. Find --- src/main/java/duke/Parser.java | 2 ++ src/main/java/duke/TaskList.java | 15 +++++++++++++++ src/main/java/duke/Ui.java | 20 +++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index c7106c7b7f..ce83a1a25e 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -49,6 +49,8 @@ public void parse() { } else if (command.startsWith("bye")) { this.isEnd = true; Ui.farewellMessage(); + } else if (command.startsWith("find")) { + String keyword = command.split(" ", 2)[1]; } else { throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-("); } diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 94bbaba096..f127cfe6eb 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -59,4 +59,19 @@ public void markTask(int num) { Ui.markTask(t); this.storage.rewriteFile(tasks); } + + /** + * Searches Tasks using keyword. + * + * @param keyword The keyword. + */ + public void searchTask(String keyword) { + ArrayList result = new ArrayList<>(); + for (int i = 0; i < this.tasks.size(); i++) { + if (tasks.get(i).toString().contains(keyword)) { + result.add(tasks.get(i)); + } + } + Ui.listMatchingTasks(result); + } } diff --git a/src/main/java/duke/Ui.java b/src/main/java/duke/Ui.java index 4e5521399a..e43d10ca9f 100644 --- a/src/main/java/duke/Ui.java +++ b/src/main/java/duke/Ui.java @@ -31,7 +31,25 @@ public static void listTasks(ArrayList tasks) { int counter = 0; while (counter != tasks.size()) { counter++; - System.out.println(" " + counter + "." + tasks.get(counter - 1).toString()); + System.out.println(" " + counter + "." + + tasks.get(counter - 1).toString()); + } + Ui.printSeparationLine(); + } + + /** + * Prints all tasks in task list. + * + * @param tasks The task list. + */ + public static void listMatchingTasks(ArrayList tasks) { + Ui.printSeparationLine(); + System.out.println(" Here are the matching tasks in your list:"); + int counter = 0; + while (counter != tasks.size()) { + counter++; + System.out.println(" " + counter + "." + + tasks.get(counter - 1).toString()); } Ui.printSeparationLine(); }