From f54347d6b32dd690999f3f86c3a9a3d864282913 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Thu, 8 Feb 2024 18:03:54 +0800 Subject: [PATCH 01/14] Complete Level-0 by adding skeleton code --- src/main/java/Duke.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334c..1e81ac9e9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -5,6 +5,12 @@ public static void main(String[] args) { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + + System.out.println("____________________________________________________________"); + System.out.println("Hello! I'm Brennan!"); + System.out.println("What can I do for you?\n"); + System.out.println("____________________________________________________________"); + System.out.println("Bye. Hope to see you again soon!"); + System.out.println("____________________________________________________________"); } } From d74699ab3dce99cba7ddc524e875b3346fb18bf7 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Thu, 8 Feb 2024 18:06:31 +0800 Subject: [PATCH 02/14] Add echo functionality for Level-1 --- src/main/java/Duke.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 1e81ac9e9..af0d3c955 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,5 @@ +import java.util.Scanner; + public class Duke { public static void main(String[] args) { String logo = " ____ _ \n" @@ -9,8 +11,28 @@ public static void main(String[] args) { System.out.println("____________________________________________________________"); System.out.println("Hello! I'm Brennan!"); System.out.println("What can I do for you?\n"); + + //String that stores the input entered by the user + String input; + + Scanner in = new Scanner(System.in); + while (true) { + input = in.nextLine(); + + if (input.equals("bye")) { + break; + } + + else { + System.out.println("____________________________________________________________"); + System.out.println(input); + System.out.println("____________________________________________________________"); + } + } + System.out.println("____________________________________________________________"); System.out.println("Bye. Hope to see you again soon!"); System.out.println("____________________________________________________________"); + } } From 27e84ebc927c12402da54bfac86987a02158f5ff Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Thu, 8 Feb 2024 18:11:04 +0800 Subject: [PATCH 03/14] Add the ability to store whatever text entered by the user and display them back to the user when requested. --- src/main/java/Duke.java | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index af0d3c955..d102f030d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -8,6 +8,8 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; + String[] listOfItems = new String[100]; + System.out.println("____________________________________________________________"); System.out.println("Hello! I'm Brennan!"); System.out.println("What can I do for you?\n"); @@ -15,18 +17,31 @@ public static void main(String[] args) { //String that stores the input entered by the user String input; + //variable stores the number of tasks being added + int sizeOfAddedItems = 0; + Scanner in = new Scanner(System.in); while (true) { input = in.nextLine(); if (input.equals("bye")) { break; - } - else { + } else if (input.equals("list")) { + System.out.println("____________________________________________________________"); + for (int i = 0; i < sizeOfAddedItems; i++) { + System.out.println((i + 1) + ". " + listOfItems[i]); + } + System.out.println("____________________________________________________________"); + } else { + listOfItems[sizeOfAddedItems] = input; + System.out.println("____________________________________________________________"); - System.out.println(input); + System.out.println(" added: " + input); System.out.println("____________________________________________________________"); + + sizeOfAddedItems++; + } } From 8085bde26236dfb51620830ac76cb1991b7b825f Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Thu, 8 Feb 2024 18:15:32 +0800 Subject: [PATCH 04/14] Add the ability to mark tasks as done, refactor code to use Task class, add the ability to change the status back to not done. --- src/main/java/Duke.java | 38 ++++++++++++++++++++++++++++++++++---- src/main/java/Task.java | 22 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d102f030d..853354cdc 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,5 @@ import java.util.Scanner; +import java.util.Arrays; public class Duke { public static void main(String[] args) { @@ -8,7 +9,7 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - String[] listOfItems = new String[100]; + Task[] listOfItems = new Task[100]; System.out.println("____________________________________________________________"); System.out.println("Hello! I'm Brennan!"); @@ -28,14 +29,43 @@ public static void main(String[] args) { break; } else if (input.equals("list")) { + System.out.println("Here are the tasks in your list: "); System.out.println("____________________________________________________________"); for (int i = 0; i < sizeOfAddedItems; i++) { - System.out.println((i + 1) + ". " + listOfItems[i]); + + System.out.println((i + 1) + ". " + "[" + listOfItems[i].getStatusIcon() + "]" + listOfItems[i].description); } System.out.println("____________________________________________________________"); - } else { - listOfItems[sizeOfAddedItems] = input; + } + + else if (Arrays.asList(input.split( " ")).contains("mark")) { + + //Finding the index of the task that the user wants to mark + String[] splitInput = input.split(" "); + int indexTask = Integer.parseInt(splitInput[1]); + + listOfItems[indexTask - 1].markAsCompleted(); + + System.out.println("____________________________________________________________"); + System.out.println("Nice! I've marked this task as done:"); + System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); + System.out.println("____________________________________________________________"); + + } else if (Arrays.asList(input.split( " ")).contains("unmark")) { + + //Finding the index of the task that the user wants to mark + String[] splitInput = input.split(" "); + int indexTask = Integer.parseInt(splitInput[1]); + + listOfItems[indexTask - 1].markAsNotCompleted(); + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); + System.out.println("____________________________________________________________"); + + } else { + listOfItems[sizeOfAddedItems] = new Task(input); System.out.println("____________________________________________________________"); System.out.println(" added: " + input); System.out.println("____________________________________________________________"); diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..7031e749f --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,22 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "X" : " "); // mark done task with X + } + + public void markAsCompleted() { + isDone = true; + } + + public void markAsNotCompleted() { + isDone = false; + } + +} From 6fd91752e7cc5516908944b4f7acd2d663afcf3a Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 9 Feb 2024 16:45:50 +0800 Subject: [PATCH 05/14] Add Todo, deadline, event, types of task --- src/main/java/Deadline.java | 31 +++++++++++++++++++++++ src/main/java/Duke.java | 49 ++++++++++++++++++++++++++++++++++--- src/main/java/Event.java | 44 +++++++++++++++++++++++++++++++++ src/main/java/Task.java | 5 +++- src/main/java/ToDo.java | 11 +++++++++ 5 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Deadline.java create mode 100644 src/main/java/Event.java create mode 100644 src/main/java/ToDo.java diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java new file mode 100644 index 000000000..6031e5d53 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,31 @@ +public class Deadline extends Task { + + protected String dateOfDeadline; + + public Deadline(String description) { + super(description); + typeOfTask = "D"; + int dividerIndex = description.indexOf("/"); + + if (dividerIndex == -1) { + this.description = description; + setDateOfDeadline(null); + } + + else { + String endDate = description.substring(dividerIndex + 4); + String descriptionWithoutDate = description.substring(0, (dividerIndex - 1)).replace("deadline", ""); + setDateOfDeadline(endDate); + this.description = descriptionWithoutDate + " (by: " + getDateOfDeadline() + ")"; + } + } + + public String getDateOfDeadline() { + return dateOfDeadline; + } + + public void setDateOfDeadline(String dateOfDeadline) { + this.dateOfDeadline = dateOfDeadline; + } +} + diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 853354cdc..bdece0dfc 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -28,12 +28,15 @@ public static void main(String[] args) { if (input.equals("bye")) { break; - } else if (input.equals("list")) { + } + + else if (input.equals("list")) { + System.out.println("Here are the tasks in your list: "); System.out.println("____________________________________________________________"); for (int i = 0; i < sizeOfAddedItems; i++) { - System.out.println((i + 1) + ". " + "[" + listOfItems[i].getStatusIcon() + "]" + listOfItems[i].description); + System.out.println((i + 1) + ". " + " "+ "[" + listOfItems[i].typeOfTask + "]" + "[" + listOfItems[i].getStatusIcon() + "]" + listOfItems[i].description); } System.out.println("____________________________________________________________"); } @@ -51,7 +54,9 @@ else if (Arrays.asList(input.split( " ")).contains("mark")) { System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); System.out.println("____________________________________________________________"); - } else if (Arrays.asList(input.split( " ")).contains("unmark")) { + } + + else if (Arrays.asList(input.split( " ")).contains("unmark")) { //Finding the index of the task that the user wants to mark String[] splitInput = input.split(" "); @@ -64,7 +69,35 @@ else if (Arrays.asList(input.split( " ")).contains("mark")) { System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); System.out.println("____________________________________________________________"); - } else { + } + + else if (Arrays.asList(input.split( " ")).contains("todo")) { + + + listOfItems[sizeOfAddedItems] = new ToDo(input); + sizeOfAddedItems += 1; + + indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + } + + else if (Arrays.asList(input.split( " ")).contains("deadline")) { + + listOfItems[sizeOfAddedItems] = new Deadline(input); + sizeOfAddedItems += 1; + + indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + } + + else if (Arrays.asList(input.split( " ")).contains("event")) { + + listOfItems[sizeOfAddedItems] = new Event(input); + sizeOfAddedItems += 1; + + indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + + } + + else { listOfItems[sizeOfAddedItems] = new Task(input); System.out.println("____________________________________________________________"); System.out.println(" added: " + input); @@ -80,4 +113,12 @@ else if (Arrays.asList(input.split( " ")).contains("mark")) { System.out.println("____________________________________________________________"); } + public static void indicateNewTask(Task newTask, int currentNumberOfTasks) { + System.out.println("____________________________________________________________"); + System.out.println("Well done, you've added a new task: "); + System.out.println("[" + newTask.typeOfTask + "]" + "[" + newTask.getStatusIcon() + "]" + newTask.description); + System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); + System.out.println("____________________________________________________________"); + } } + diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..bdf053c35 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,44 @@ +public class Event extends Task { + + protected String startingDate; + protected String endingDate; + + public Event(String description) { + super(description); + typeOfTask = "E"; + + int dividerIndexFrom = description.indexOf("/"); + int dividerIndexTo = description.lastIndexOf("/"); + + setStartingDate(description.substring(dividerIndexFrom + 6, dividerIndexTo - 1)); + setEndingDate(description.substring(dividerIndexTo + 4)); + + if (dividerIndexFrom == -1) { + setStartingDate(null); + } + + else if (dividerIndexTo == -1) { + setEndingDate(null); + } + + String descriptionWithoutDate = description.substring(0, (dividerIndexFrom - 1)).replace("event", ""); + + this.description = descriptionWithoutDate + " (from: " + getStartingDate() + " to: " + getEndingDate() + ")"; + } + + public String getStartingDate() { + return startingDate; + } + + public void setStartingDate(String startingDate) { + this.startingDate = startingDate; + } + + public String getEndingDate() { + return endingDate; + } + + public void setEndingDate(String endingDate) { + this.endingDate = endingDate; + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 7031e749f..99d8b495f 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -2,13 +2,16 @@ public class Task { protected String description; protected boolean isDone; + protected String typeOfTask; + public Task(String description) { this.description = description; this.isDone = false; + typeOfTask = ""; } public String getStatusIcon() { - return (isDone ? "X" : " "); // mark done task with X + return (isDone ? "X" : " "); } public void markAsCompleted() { diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 000000000..438c57e56 --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,11 @@ +public class ToDo extends Task{ + + protected String dateOfDeadline; + + public ToDo(String description) { + super(description); + typeOfTask = "T"; + this.description = description.replace("todo", ""); + } + +} From a150b93deb46cf574122098a6bd9c70be0a70add Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 9 Feb 2024 17:35:19 +0800 Subject: [PATCH 06/14] Improve quality of code --- src/main/java/Deadline.java | 1 - src/main/java/Duke.java | 14 ++------------ src/main/java/Task.java | 1 - src/main/java/ToDo.java | 1 - 4 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 6031e5d53..aa89e0ca0 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,5 +1,4 @@ public class Deadline extends Task { - protected String dateOfDeadline; public Deadline(String description) { diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index bdece0dfc..52d63ed76 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -22,16 +22,15 @@ public static void main(String[] args) { int sizeOfAddedItems = 0; Scanner in = new Scanner(System.in); + while (true) { input = in.nextLine(); if (input.equals("bye")) { break; - } else if (input.equals("list")) { - System.out.println("Here are the tasks in your list: "); System.out.println("____________________________________________________________"); for (int i = 0; i < sizeOfAddedItems; i++) { @@ -42,7 +41,6 @@ else if (input.equals("list")) { } else if (Arrays.asList(input.split( " ")).contains("mark")) { - //Finding the index of the task that the user wants to mark String[] splitInput = input.split(" "); int indexTask = Integer.parseInt(splitInput[1]); @@ -53,11 +51,9 @@ else if (Arrays.asList(input.split( " ")).contains("mark")) { System.out.println("Nice! I've marked this task as done:"); System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); System.out.println("____________________________________________________________"); - } else if (Arrays.asList(input.split( " ")).contains("unmark")) { - //Finding the index of the task that the user wants to mark String[] splitInput = input.split(" "); int indexTask = Integer.parseInt(splitInput[1]); @@ -68,12 +64,9 @@ else if (Arrays.asList(input.split( " ")).contains("unmark")) { System.out.println(" OK, I've marked this task as not done yet:"); System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); System.out.println("____________________________________________________________"); - } else if (Arrays.asList(input.split( " ")).contains("todo")) { - - listOfItems[sizeOfAddedItems] = new ToDo(input); sizeOfAddedItems += 1; @@ -81,7 +74,6 @@ else if (Arrays.asList(input.split( " ")).contains("todo")) { } else if (Arrays.asList(input.split( " ")).contains("deadline")) { - listOfItems[sizeOfAddedItems] = new Deadline(input); sizeOfAddedItems += 1; @@ -89,12 +81,10 @@ else if (Arrays.asList(input.split( " ")).contains("deadline")) { } else if (Arrays.asList(input.split( " ")).contains("event")) { - listOfItems[sizeOfAddedItems] = new Event(input); sizeOfAddedItems += 1; indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); - } else { @@ -104,7 +94,6 @@ else if (Arrays.asList(input.split( " ")).contains("event")) { System.out.println("____________________________________________________________"); sizeOfAddedItems++; - } } @@ -113,6 +102,7 @@ else if (Arrays.asList(input.split( " ")).contains("event")) { System.out.println("____________________________________________________________"); } + public static void indicateNewTask(Task newTask, int currentNumberOfTasks) { System.out.println("____________________________________________________________"); System.out.println("Well done, you've added a new task: "); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 99d8b495f..184d7a769 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,7 +1,6 @@ public class Task { protected String description; protected boolean isDone; - protected String typeOfTask; public Task(String description) { diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java index 438c57e56..742ab107a 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/ToDo.java @@ -1,5 +1,4 @@ public class ToDo extends Task{ - protected String dateOfDeadline; public ToDo(String description) { From 8df137b002f8abf699a92b6848d5b12730e80e98 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Thu, 15 Feb 2024 18:36:17 +0800 Subject: [PATCH 07/14] Add error handling for typical cases --- src/main/java/Duke.java | 159 +++++++++++++++++-------------- src/main/java/DukeException.java | 33 +++++++ 2 files changed, 123 insertions(+), 69 deletions(-) create mode 100644 src/main/java/DukeException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 52d63ed76..5927ce553 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,6 +11,7 @@ public static void main(String[] args) { Task[] listOfItems = new Task[100]; + System.out.println(logo); System.out.println("____________________________________________________________"); System.out.println("Hello! I'm Brennan!"); System.out.println("What can I do for you?\n"); @@ -26,83 +27,96 @@ public static void main(String[] args) { while (true) { input = in.nextLine(); - if (input.equals("bye")) { - break; - } - - else if (input.equals("list")) { - System.out.println("Here are the tasks in your list: "); - System.out.println("____________________________________________________________"); - for (int i = 0; i < sizeOfAddedItems; i++) { - - System.out.println((i + 1) + ". " + " "+ "[" + listOfItems[i].typeOfTask + "]" + "[" + listOfItems[i].getStatusIcon() + "]" + listOfItems[i].description); + try { + + if (input.equals("bye")) { + break; + } else if (input.equals("list")) { + System.out.println("Here are the tasks in your list: "); + System.out.println("____________________________________________________________"); + for (int i = 0; i < sizeOfAddedItems; i++) { + + System.out.println((i + 1) + ". " + " " + "[" + listOfItems[i].typeOfTask + "]" + "[" + listOfItems[i].getStatusIcon() + "]" + listOfItems[i].description); + } + System.out.println("____________________________________________________________"); + } else if (Arrays.asList(input.split(" ")).contains("mark")) { + String[] splitInput = input.split(" "); + + if (splitInput.length < 2 || splitInput[1].isEmpty()) { + throw new StringIndexOutOfBoundsException(); + } + //Finding the index of the task that the user wants to mark + + int indexTask = Integer.parseInt(splitInput[1]); + + listOfItems[indexTask - 1].markAsCompleted(); + + System.out.println("____________________________________________________________"); + System.out.println("Nice! I've marked this task as done:"); + System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask - 1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); + System.out.println("____________________________________________________________"); + } else if (Arrays.asList(input.split(" ")).contains("unmark")) { + String[] splitInput = input.split(" "); + + if (splitInput.length < 2 || splitInput[1].isEmpty()) { + throw new StringIndexOutOfBoundsException(); + } + //Finding the index of the task that the user wants to mark + + int indexTask = Integer.parseInt(splitInput[1]); + + listOfItems[indexTask - 1].markAsNotCompleted(); + + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask - 1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); + System.out.println("____________________________________________________________"); + } else if (Arrays.asList(input.split(" ")).contains("todo")) { + String[] splitInput = input.split(" "); + if (splitInput.length < 2 || splitInput[1].isEmpty()) { + throw new StringIndexOutOfBoundsException(); + } else if (sizeOfAddedItems >= 100) { + throw new ArrayIndexOutOfBoundsException(); + } + listOfItems[sizeOfAddedItems] = new ToDo(input); + sizeOfAddedItems += 1; + + indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + } else if (Arrays.asList(input.split(" ")).contains("deadline")) { + String[] splitInput = input.split(" "); + if (splitInput.length < 2 || splitInput[1].isEmpty()) { + throw new StringIndexOutOfBoundsException(); + } else if (sizeOfAddedItems >= 100) { + throw new ArrayIndexOutOfBoundsException(); + } + listOfItems[sizeOfAddedItems] = new Deadline(input); + sizeOfAddedItems += 1; + + indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + } else if (Arrays.asList(input.split(" ")).contains("event")) { + String[] splitInput = input.split(" "); + if (splitInput.length < 2 || splitInput[1].isEmpty()) { + throw new StringIndexOutOfBoundsException(); + } else if (sizeOfAddedItems >= 100) { + throw new ArrayIndexOutOfBoundsException(); + } + listOfItems[sizeOfAddedItems] = new Event(input); + sizeOfAddedItems += 1; + + indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + } else { + throw new IllegalArgumentException(); } - System.out.println("____________________________________________________________"); - } - - else if (Arrays.asList(input.split( " ")).contains("mark")) { - //Finding the index of the task that the user wants to mark - String[] splitInput = input.split(" "); - int indexTask = Integer.parseInt(splitInput[1]); - - listOfItems[indexTask - 1].markAsCompleted(); - - System.out.println("____________________________________________________________"); - System.out.println("Nice! I've marked this task as done:"); - System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); - System.out.println("____________________________________________________________"); - } - - else if (Arrays.asList(input.split( " ")).contains("unmark")) { - //Finding the index of the task that the user wants to mark - String[] splitInput = input.split(" "); - int indexTask = Integer.parseInt(splitInput[1]); - - listOfItems[indexTask - 1].markAsNotCompleted(); - - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask-1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); - System.out.println("____________________________________________________________"); - } - - else if (Arrays.asList(input.split( " ")).contains("todo")) { - listOfItems[sizeOfAddedItems] = new ToDo(input); - sizeOfAddedItems += 1; - - indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); - } - - else if (Arrays.asList(input.split( " ")).contains("deadline")) { - listOfItems[sizeOfAddedItems] = new Deadline(input); - sizeOfAddedItems += 1; - - indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); - } - - else if (Arrays.asList(input.split( " ")).contains("event")) { - listOfItems[sizeOfAddedItems] = new Event(input); - sizeOfAddedItems += 1; - - indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); - } - - else { - listOfItems[sizeOfAddedItems] = new Task(input); - System.out.println("____________________________________________________________"); - System.out.println(" added: " + input); - System.out.println("____________________________________________________________"); - - sizeOfAddedItems++; + } catch (Exception e) { + DukeException.handleException(e, input); } } - System.out.println("____________________________________________________________"); System.out.println("Bye. Hope to see you again soon!"); System.out.println("____________________________________________________________"); - } + public static void indicateNewTask(Task newTask, int currentNumberOfTasks) { System.out.println("____________________________________________________________"); System.out.println("Well done, you've added a new task: "); @@ -112,3 +126,10 @@ public static void indicateNewTask(Task newTask, int currentNumberOfTasks) { } } + + + + + + + diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java new file mode 100644 index 000000000..e7e7a524a --- /dev/null +++ b/src/main/java/DukeException.java @@ -0,0 +1,33 @@ +public class DukeException extends Exception { + + public static void handleException(Exception exception, String input) { + final String SEPARATOR = "====================================================================================================================="; + + String[] splitInput = input.split(" "); + + System.out.println(SEPARATOR); + + if (exception instanceof ArrayIndexOutOfBoundsException) { + System.out.println("You have exceeded the task limit of 100 tasks."); + + } else if (exception instanceof StringIndexOutOfBoundsException) { + + if (input.startsWith("mark") || input.startsWith("unmark")) { + System.out.println("The task to marked or unmarked is not stated clearly."); + } + else if (input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { + System.out.println("Incomplete " + splitInput[0] + " detected. " + + "Your statement is not clear. Please fix this."); + } + + } else if (exception instanceof IllegalArgumentException) { + System.out.println("Unfamiliar commands cannot be accepted by the system."); + + } else { + System.out.println("Unknown error detected. Try to fix this immediately."); + } + + System.out.println(SEPARATOR); + } +} + From bca0bcfe26f9fd8daa003be621683e8946458363 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 23 Feb 2024 17:24:16 +0800 Subject: [PATCH 08/14] add delete functionality --- src/main/java/Duke.java | 44 ++++++++++++++++++++---------- src/main/java/META-INF/MANIFEST.MF | 3 ++ 2 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5927ce553..d6e06ee52 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,6 @@ -import java.util.Scanner; import java.util.Arrays; +import java.util.Scanner; +import java.util.ArrayList; public class Duke { public static void main(String[] args) { @@ -9,7 +10,7 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - Task[] listOfItems = new Task[100]; + ArrayList listOfItems = new ArrayList<>(); System.out.println(logo); System.out.println("____________________________________________________________"); @@ -36,7 +37,7 @@ public static void main(String[] args) { System.out.println("____________________________________________________________"); for (int i = 0; i < sizeOfAddedItems; i++) { - System.out.println((i + 1) + ". " + " " + "[" + listOfItems[i].typeOfTask + "]" + "[" + listOfItems[i].getStatusIcon() + "]" + listOfItems[i].description); + System.out.println((i + 1) + ". " + " " + "[" + listOfItems.get(i).typeOfTask + "]" + "[" + listOfItems.get(i).getStatusIcon() + "]" + listOfItems.get(i).description); } System.out.println("____________________________________________________________"); } else if (Arrays.asList(input.split(" ")).contains("mark")) { @@ -49,11 +50,11 @@ public static void main(String[] args) { int indexTask = Integer.parseInt(splitInput[1]); - listOfItems[indexTask - 1].markAsCompleted(); + listOfItems.get(indexTask - 1).markAsCompleted(); System.out.println("____________________________________________________________"); System.out.println("Nice! I've marked this task as done:"); - System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask - 1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); + System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); System.out.println("____________________________________________________________"); } else if (Arrays.asList(input.split(" ")).contains("unmark")) { String[] splitInput = input.split(" "); @@ -65,11 +66,11 @@ public static void main(String[] args) { int indexTask = Integer.parseInt(splitInput[1]); - listOfItems[indexTask - 1].markAsNotCompleted(); + listOfItems.get(indexTask - 1).markAsNotCompleted(); System.out.println("____________________________________________________________"); System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println((indexTask) + ". " + "[" + listOfItems[indexTask - 1].getStatusIcon() + "]" + listOfItems[indexTask - 1].description); + System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); System.out.println("____________________________________________________________"); } else if (Arrays.asList(input.split(" ")).contains("todo")) { String[] splitInput = input.split(" "); @@ -78,10 +79,10 @@ public static void main(String[] args) { } else if (sizeOfAddedItems >= 100) { throw new ArrayIndexOutOfBoundsException(); } - listOfItems[sizeOfAddedItems] = new ToDo(input); + listOfItems.add(sizeOfAddedItems, new ToDo(input)); sizeOfAddedItems += 1; - indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); } else if (Arrays.asList(input.split(" ")).contains("deadline")) { String[] splitInput = input.split(" "); if (splitInput.length < 2 || splitInput[1].isEmpty()) { @@ -89,10 +90,10 @@ public static void main(String[] args) { } else if (sizeOfAddedItems >= 100) { throw new ArrayIndexOutOfBoundsException(); } - listOfItems[sizeOfAddedItems] = new Deadline(input); + listOfItems.add(sizeOfAddedItems, new Deadline(input)); sizeOfAddedItems += 1; - indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); + indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); } else if (Arrays.asList(input.split(" ")).contains("event")) { String[] splitInput = input.split(" "); if (splitInput.length < 2 || splitInput[1].isEmpty()) { @@ -100,11 +101,16 @@ public static void main(String[] args) { } else if (sizeOfAddedItems >= 100) { throw new ArrayIndexOutOfBoundsException(); } - listOfItems[sizeOfAddedItems] = new Event(input); + listOfItems.add(sizeOfAddedItems, new Event(input)); sizeOfAddedItems += 1; - indicateNewTask(listOfItems[sizeOfAddedItems - 1], sizeOfAddedItems); - } else { + indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); + } else if (Arrays.asList(input.split(" ")).contains("delete")) { + String[] splitInput = input.split(" "); + deleteTask(listOfItems,splitInput); + sizeOfAddedItems--; + } + else { throw new IllegalArgumentException(); } } catch (Exception e) { @@ -124,6 +130,16 @@ public static void indicateNewTask(Task newTask, int currentNumberOfTasks) { System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); System.out.println("____________________________________________________________"); } + + private static void deleteTask(ArrayList listOfItems, String[] splitInput) { + int indexTask = Integer.parseInt(splitInput[1]) ; + System.out.println("This task has been deleted:"); + System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); + System.out.println("Your roster now contains " + (listOfItems.size() - 1) + " endeavors."); + + listOfItems.remove(indexTask-1); + } + } diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..9f37e4e0a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke + From 51704c7214393e94c096bc3cb99ac5217ce62d22 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 23 Feb 2024 23:52:56 +0800 Subject: [PATCH 09/14] Add save functionality --- data/taskCategory.txt | 3 ++ src/main/java/Deadline.java | 9 ++++-- src/main/java/Duke.java | 63 ++++++++++++++++++++++++++++++++++--- src/main/java/Event.java | 13 +++++--- src/main/java/Task.java | 34 ++++++++++++++++++++ src/main/java/ToDo.java | 5 +++ 6 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 data/taskCategory.txt diff --git a/data/taskCategory.txt b/data/taskCategory.txt new file mode 100644 index 000000000..25388455e --- /dev/null +++ b/data/taskCategory.txt @@ -0,0 +1,3 @@ +T| laundry|0 +E| project meeting (from: 5pm to: 8pm)))|0| 5pm | 8pm)) +D| assignment (by: today)))|0| today)) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index aa89e0ca0..8ab3bbce1 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -4,7 +4,7 @@ public class Deadline extends Task { public Deadline(String description) { super(description); typeOfTask = "D"; - int dividerIndex = description.indexOf("/"); + int dividerIndex = description.indexOf("by"); if (dividerIndex == -1) { this.description = description; @@ -12,7 +12,7 @@ public Deadline(String description) { } else { - String endDate = description.substring(dividerIndex + 4); + String endDate = description.substring(dividerIndex + 3); String descriptionWithoutDate = description.substring(0, (dividerIndex - 1)).replace("deadline", ""); setDateOfDeadline(endDate); this.description = descriptionWithoutDate + " (by: " + getDateOfDeadline() + ")"; @@ -26,5 +26,10 @@ public String getDateOfDeadline() { public void setDateOfDeadline(String dateOfDeadline) { this.dateOfDeadline = dateOfDeadline; } + + @Override + public String toFileString() { + return "D|" + super.toFileString() + "|" + dateOfDeadline; // Prefix with "D" to indicate Deadline + } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index d6e06ee52..b6b9814d2 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,8 +1,16 @@ +import java.io.*; import java.util.Arrays; import java.util.Scanner; import java.util.ArrayList; public class Duke { + private static final String FILE_PATH = "./data/taskCategory.txt"; + + static ArrayList listOfItems = new ArrayList<>(); + + //variable stores the number of tasks being added + static int sizeOfAddedItems = 0; + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -10,18 +18,17 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - ArrayList listOfItems = new ArrayList<>(); System.out.println(logo); System.out.println("____________________________________________________________"); System.out.println("Hello! I'm Brennan!"); System.out.println("What can I do for you?\n"); + loadTasksFromFile(); + //String that stores the input entered by the user String input; - //variable stores the number of tasks being added - int sizeOfAddedItems = 0; Scanner in = new Scanner(System.in); @@ -113,9 +120,12 @@ public static void main(String[] args) { else { throw new IllegalArgumentException(); } + } catch (Exception e) { DukeException.handleException(e, input); } + + saveTasksToFile(); } System.out.println("____________________________________________________________"); System.out.println("Bye. Hope to see you again soon!"); @@ -139,7 +149,52 @@ private static void deleteTask(ArrayList listOfItems, String[] splitInput) listOfItems.remove(indexTask-1); } - + + public static void saveTasksToFile() { + try { + File file = new File(FILE_PATH); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); // Create directories if they don't exist + } + file.createNewFile(); // Create the file if it doesn't exist + try (FileWriter writer = new FileWriter(file)) { + for (int i = 0; i < sizeOfAddedItems; i++) { + writer.write(listOfItems.get(i).toFileString() + "\n"); + } + } + } catch (IOException e) { + System.err.println("Error saving tasks to file: " + e.getMessage()); + } + } + + // Method to load tasks from a file + public static void loadTasksFromFile() { + try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) { + String line; + while ((line = reader.readLine()) != null) { + Task task = Task.fromString(line); + if (task != null) { + listOfItems.add(sizeOfAddedItems++, task); + } + } + } catch (FileNotFoundException e) { + // Handle the case where the file doesn't exist + System.err.println("File not found. Creating a new file..."); + System.err.println("File created. Please enter your commands."); + File file = new File(FILE_PATH); + try { + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); // Create directories if they don't exist + } + file.createNewFile(); + } catch (IOException ioException) { + System.err.println("Error creating a new file: " + ioException.getMessage()); + } + } catch (IOException e) { + System.err.println("Error loading tasks from file: " + e.getMessage()); + } + } + } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index bdf053c35..5cb293b27 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -7,11 +7,11 @@ public Event(String description) { super(description); typeOfTask = "E"; - int dividerIndexFrom = description.indexOf("/"); - int dividerIndexTo = description.lastIndexOf("/"); + int dividerIndexFrom = description.indexOf("from"); + int dividerIndexTo = description.lastIndexOf("to"); - setStartingDate(description.substring(dividerIndexFrom + 6, dividerIndexTo - 1)); - setEndingDate(description.substring(dividerIndexTo + 4)); + setStartingDate(description.substring(dividerIndexFrom + 5, dividerIndexTo - 1)); + setEndingDate(description.substring(dividerIndexTo + 3)); if (dividerIndexFrom == -1) { setStartingDate(null); @@ -41,4 +41,9 @@ public String getEndingDate() { public void setEndingDate(String endingDate) { this.endingDate = endingDate; } + + @Override + public String toFileString() { + return "E|" + super.toFileString() + "|" + startingDate + "|" + endingDate; // Prefix with "E" to indicate Event + } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 184d7a769..3a791bc77 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -21,4 +21,38 @@ public void markAsNotCompleted() { isDone = false; } + public String toFileString() { + return description + "|" + (isDone ? "1" : "0"); // Example format: "description|status" + } + + public static Task fromString(String fileString) { + String[] parts = fileString.split("\\|"); + String taskType = parts[0]; // Extract task type + String description = parts[1]; + boolean isDone = parts[2].equals("1"); // Extract status + + Task task; + switch (taskType) { + case "D": + task = new Deadline(description); // Create Deadline task + break; + case "E": + task = new Event(description); // Create Event task + break; + case "T": + task = new ToDo(description); // Create Todo task + break; + default: + // Handle unknown task type + task = null; + break; + } + + if (task != null && isDone) { + task.markAsCompleted(); // Mark task as done if status is '1' + } + + return task; + } + } diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java index 742ab107a..d246f85fe 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/ToDo.java @@ -7,4 +7,9 @@ public ToDo(String description) { this.description = description.replace("todo", ""); } + @Override + public String toFileString() { + return "T|" + super.toFileString(); // Prefix with "T" to indicate Todo + } + } From 6ee239089951fe294c43f82612e2e5c0b4c58db9 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Thu, 7 Mar 2024 19:03:06 +0800 Subject: [PATCH 10/14] Refactor code to be more OOP --- data/taskCategory.txt | 6 +- src/main/java/Deadline.java | 4 +- src/main/java/Duke.java | 225 ++++--------------------------- src/main/java/DukeException.java | 3 +- src/main/java/Parser.java | 47 +++++++ src/main/java/Storage.java | 36 +++++ src/main/java/Task.java | 7 +- src/main/java/TaskList.java | 71 ++++++++++ src/main/java/Ui.java | 31 +++++ 9 files changed, 221 insertions(+), 209 deletions(-) create mode 100644 src/main/java/Parser.java create mode 100644 src/main/java/Storage.java create mode 100644 src/main/java/TaskList.java create mode 100644 src/main/java/Ui.java diff --git a/data/taskCategory.txt b/data/taskCategory.txt index 25388455e..27eaec113 100644 --- a/data/taskCategory.txt +++ b/data/taskCategory.txt @@ -1,3 +1,3 @@ -T| laundry|0 -E| project meeting (from: 5pm to: 8pm)))|0| 5pm | 8pm)) -D| assignment (by: today)))|0| today)) +D| assignment (by: today))))))))))))|0| today))))))))))) +E| presentation (from: 2pm to: 4pm)))))))))|0| 2pm | 4pm)))))))) +D|deadline coding|1|null diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 8ab3bbce1..207438847 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -9,9 +9,7 @@ public Deadline(String description) { if (dividerIndex == -1) { this.description = description; setDateOfDeadline(null); - } - - else { + } else { String endDate = description.substring(dividerIndex + 3); String descriptionWithoutDate = description.substring(0, (dividerIndex - 1)).replace("deadline", ""); setDateOfDeadline(endDate); diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b6b9814d2..9a022477d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,206 +1,37 @@ -import java.io.*; -import java.util.Arrays; -import java.util.Scanner; -import java.util.ArrayList; - public class Duke { - private static final String FILE_PATH = "./data/taskCategory.txt"; - - static ArrayList listOfItems = new ArrayList<>(); - - //variable stores the number of tasks being added - static int sizeOfAddedItems = 0; - - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - - - System.out.println(logo); - System.out.println("____________________________________________________________"); - System.out.println("Hello! I'm Brennan!"); - System.out.println("What can I do for you?\n"); - - loadTasksFromFile(); - - //String that stores the input entered by the user - String input; - - - Scanner in = new Scanner(System.in); - - while (true) { - input = in.nextLine(); - - try { - - if (input.equals("bye")) { - break; - } else if (input.equals("list")) { - System.out.println("Here are the tasks in your list: "); - System.out.println("____________________________________________________________"); - for (int i = 0; i < sizeOfAddedItems; i++) { - - System.out.println((i + 1) + ". " + " " + "[" + listOfItems.get(i).typeOfTask + "]" + "[" + listOfItems.get(i).getStatusIcon() + "]" + listOfItems.get(i).description); - } - System.out.println("____________________________________________________________"); - } else if (Arrays.asList(input.split(" ")).contains("mark")) { - String[] splitInput = input.split(" "); - - if (splitInput.length < 2 || splitInput[1].isEmpty()) { - throw new StringIndexOutOfBoundsException(); - } - //Finding the index of the task that the user wants to mark - - int indexTask = Integer.parseInt(splitInput[1]); - - listOfItems.get(indexTask - 1).markAsCompleted(); - - System.out.println("____________________________________________________________"); - System.out.println("Nice! I've marked this task as done:"); - System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); - System.out.println("____________________________________________________________"); - } else if (Arrays.asList(input.split(" ")).contains("unmark")) { - String[] splitInput = input.split(" "); - - if (splitInput.length < 2 || splitInput[1].isEmpty()) { - throw new StringIndexOutOfBoundsException(); - } - //Finding the index of the task that the user wants to mark - - int indexTask = Integer.parseInt(splitInput[1]); - - listOfItems.get(indexTask - 1).markAsNotCompleted(); - - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); - System.out.println("____________________________________________________________"); - } else if (Arrays.asList(input.split(" ")).contains("todo")) { - String[] splitInput = input.split(" "); - if (splitInput.length < 2 || splitInput[1].isEmpty()) { - throw new StringIndexOutOfBoundsException(); - } else if (sizeOfAddedItems >= 100) { - throw new ArrayIndexOutOfBoundsException(); - } - listOfItems.add(sizeOfAddedItems, new ToDo(input)); - sizeOfAddedItems += 1; - - indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); - } else if (Arrays.asList(input.split(" ")).contains("deadline")) { - String[] splitInput = input.split(" "); - if (splitInput.length < 2 || splitInput[1].isEmpty()) { - throw new StringIndexOutOfBoundsException(); - } else if (sizeOfAddedItems >= 100) { - throw new ArrayIndexOutOfBoundsException(); - } - listOfItems.add(sizeOfAddedItems, new Deadline(input)); - sizeOfAddedItems += 1; - - indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); - } else if (Arrays.asList(input.split(" ")).contains("event")) { - String[] splitInput = input.split(" "); - if (splitInput.length < 2 || splitInput[1].isEmpty()) { - throw new StringIndexOutOfBoundsException(); - } else if (sizeOfAddedItems >= 100) { - throw new ArrayIndexOutOfBoundsException(); - } - listOfItems.add(sizeOfAddedItems, new Event(input)); - sizeOfAddedItems += 1; - - indicateNewTask(listOfItems.get(sizeOfAddedItems - 1), sizeOfAddedItems); - } else if (Arrays.asList(input.split(" ")).contains("delete")) { - String[] splitInput = input.split(" "); - deleteTask(listOfItems,splitInput); - sizeOfAddedItems--; - } - else { - throw new IllegalArgumentException(); - } - - } catch (Exception e) { - DukeException.handleException(e, input); - } - - saveTasksToFile(); - } - System.out.println("____________________________________________________________"); - System.out.println("Bye. Hope to see you again soon!"); - System.out.println("____________________________________________________________"); - } - - - public static void indicateNewTask(Task newTask, int currentNumberOfTasks) { - System.out.println("____________________________________________________________"); - System.out.println("Well done, you've added a new task: "); - System.out.println("[" + newTask.typeOfTask + "]" + "[" + newTask.getStatusIcon() + "]" + newTask.description); - System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); - System.out.println("____________________________________________________________"); - } - - private static void deleteTask(ArrayList listOfItems, String[] splitInput) { - int indexTask = Integer.parseInt(splitInput[1]) ; - System.out.println("This task has been deleted:"); - System.out.println((indexTask) + ". " + "[" + listOfItems.get(indexTask - 1).getStatusIcon() + "]" + listOfItems.get(indexTask - 1).description); - System.out.println("Your roster now contains " + (listOfItems.size() - 1) + " endeavors."); - - listOfItems.remove(indexTask-1); - } - - public static void saveTasksToFile() { + private Storage storage; + private TaskList tasks; + private Ui ui; + private Parser parser; + + public Duke(String filePath) { + ui = new Ui(); + storage = new Storage(filePath); + parser = new Parser(); try { - File file = new File(FILE_PATH); - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); // Create directories if they don't exist - } - file.createNewFile(); // Create the file if it doesn't exist - try (FileWriter writer = new FileWriter(file)) { - for (int i = 0; i < sizeOfAddedItems; i++) { - writer.write(listOfItems.get(i).toFileString() + "\n"); - } - } - } catch (IOException e) { - System.err.println("Error saving tasks to file: " + e.getMessage()); + tasks = new TaskList(storage.load()); + } catch (DukeException e) { + ui.showLoadingError(); + tasks = new TaskList(); } } - - // Method to load tasks from a file - public static void loadTasksFromFile() { - try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) { - String line; - while ((line = reader.readLine()) != null) { - Task task = Task.fromString(line); - if (task != null) { - listOfItems.add(sizeOfAddedItems++, task); - } - } - } catch (FileNotFoundException e) { - // Handle the case where the file doesn't exist - System.err.println("File not found. Creating a new file..."); - System.err.println("File created. Please enter your commands."); - File file = new File(FILE_PATH); + public void run() { + ui.showWelcome(); + boolean isExit = false; + while (!isExit) { try { - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); // Create directories if they don't exist - } - file.createNewFile(); - } catch (IOException ioException) { - System.err.println("Error creating a new file: " + ioException.getMessage()); + String fullCommand = ui.getUserInput(); + ui.showLine(); // show the divider line ("_______") + isExit = parser.parseCommand(fullCommand, tasks); + // Assuming DukeException is a custom exception class that you've defined elsewhere + } catch (DukeException e) { + ui.printMessage("Error: " + e.getMessage()); + } finally { + ui.showLine(); } - } catch (IOException e) { - System.err.println("Error loading tasks from file: " + e.getMessage()); } } - + public static void main(String[] args) { + new Duke("data/taskCategory.txt").run(); + } } - - - - - - - - diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java index e7e7a524a..3bf13ede5 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/DukeException.java @@ -14,8 +14,7 @@ public static void handleException(Exception exception, String input) { if (input.startsWith("mark") || input.startsWith("unmark")) { System.out.println("The task to marked or unmarked is not stated clearly."); - } - else if (input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { + } else if (input.startsWith("todo") || input.startsWith("deadline") || input.startsWith("event")) { System.out.println("Incomplete " + splitInput[0] + " detected. " + "Your statement is not clear. Please fix this."); } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 000000000..6ad40dc0b --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,47 @@ +public class Parser { + public boolean parseCommand(String command, TaskList tasks) throws DukeException { + String[] splitCommand = command.split(" "); + try { + if (splitCommand[0].equals("list")) { + tasks.listTasks(); + } else if (splitCommand[0].equals("mark")) { + checkCommandArguments(splitCommand, 2, tasks); + tasks.markTaskAsCompleted(Integer.parseInt(splitCommand[1])); + } else if (splitCommand[0].equals("unmark")) { + checkCommandArguments(splitCommand, 2, tasks); + tasks.markTaskAsNotCompleted(Integer.parseInt(splitCommand[1])); + } else if (splitCommand[0].equals("todo")) { + checkCommandArguments(splitCommand, 2, tasks); + tasks.addTask(new ToDo(command)); + } else if (splitCommand[0].equals("deadline")) { + checkCommandArguments(splitCommand, 2, tasks); + tasks.addTask(new Deadline(command)); + } else if (splitCommand[0].equals("event")) { + checkCommandArguments(splitCommand, 2, tasks); + tasks.addTask(new Event(command)); + } else if (splitCommand[0].equals("delete")) { + checkCommandArguments(splitCommand, 2, tasks); + tasks.deleteTask(Integer.parseInt(splitCommand[1])); + } else if (splitCommand[0].equals("bye")) { + return true; // Indicate that the program should exit + } else { + throw new IllegalArgumentException(); + } + } catch (Exception e) { + DukeException.handleException(e, command); + } + finally { + Storage.save(tasks.getTasks()); + } + return false; // Default return value if the command does not require exiting + } + + private void checkCommandArguments(String[] splitCommand, int expectedArgs, TaskList tasks) { + if (tasks.size() >= 100) { + throw new ArrayIndexOutOfBoundsException(); + } + if (splitCommand.length < expectedArgs) { + throw new StringIndexOutOfBoundsException(); + } + } +} diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 000000000..2b3721275 --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,36 @@ +import java.io.*; +import java.util.ArrayList; + +public class Storage { + private static String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public ArrayList load() throws DukeException { + ArrayList tasks = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { + String line; + while ((line = reader.readLine()) != null) { + Task task = Task.fromString(line); + if (task != null) { + tasks.add(task); + } + } + } catch (IOException e) { + DukeException.handleException(e, "null"); + } + return tasks; + } + + public static void save(ArrayList tasks) { + try (FileWriter writer = new FileWriter(filePath)) { + for (Task task : tasks) { + writer.write(task.toFileString() + "\n"); + } + } catch (IOException e) { + System.err.println("Error saving tasks to file: " + e.getMessage()); + } + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 3a791bc77..bd27d5aca 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -34,16 +34,15 @@ public static Task fromString(String fileString) { Task task; switch (taskType) { case "D": - task = new Deadline(description); // Create Deadline task + task = new Deadline(description); break; case "E": - task = new Event(description); // Create Event task + task = new Event(description); break; case "T": - task = new ToDo(description); // Create Todo task + task = new ToDo(description); break; default: - // Handle unknown task type task = null; break; } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 000000000..7418582b8 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,71 @@ +import java.util.ArrayList; +public class TaskList { + private ArrayList tasks; + + public TaskList() { + this.tasks = new ArrayList<>(); + } + public TaskList(ArrayList tasks) { + this.tasks = tasks; + } + public void addTask(Task task) { + tasks.add(task); + indicateNewTask(tasks.get(tasks.size() - 1), tasks.size()); + } + public void deleteTask(int indexTask) { + Task removedTask = tasks.get(indexTask - 1); + tasks.remove(removedTask); + System.out.println("Command received. I've removed this task:"); + System.out.println((indexTask) + ". " + "[" + removedTask.getStatusIcon() + "]" + removedTask.description); + System.out.println("Currently you have " + tasks.size() + " tasks in the list below."); + } + + + public void markTaskAsCompleted(int indexTask) { + + tasks.get(indexTask - 1).markAsCompleted(); + + System.out.println("____________________________________________________________"); + System.out.println("Nice! I've marked this task as done:"); + System.out.println((indexTask) + ". " + "[" + tasks.get(indexTask - 1).getStatusIcon() + "]" + tasks.get(indexTask - 1).description); + System.out.println("____________________________________________________________"); + } + + public void listTasks() { + System.out.println("Here are the tasks in your list: "); + System.out.println("____________________________________________________________"); + for (int i = 0; i < tasks.size(); i++) { + + System.out.println((i + 1) + ". " + " " + "[" + tasks.get(i).typeOfTask + "]" + "[" + tasks.get(i).getStatusIcon() + "]" + tasks.get(i).description); + } + System.out.println("____________________________________________________________"); + } + + public void markTaskAsNotCompleted(int indexTask) { + + tasks.get(indexTask - 1).markAsNotCompleted(); + + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println((indexTask) + ". " + "[" + tasks.get(indexTask - 1).getStatusIcon() + "]" + tasks.get(indexTask - 1).description); + System.out.println("____________________________________________________________"); + } + public void indicateNewTask(Task newTask, int currentNumberOfTasks) { + System.out.println("____________________________________________________________"); + System.out.println("Well done, you've added a new task: "); + System.out.println("[" + newTask.typeOfTask + "]" + "[" + newTask.getStatusIcon() + "]" + newTask.description); + System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); + System.out.println("____________________________________________________________"); + } + + public ArrayList getTasks() { + return tasks; + } + + public Task get(int index) { + return tasks.get(index); + } + public int size() { + return tasks.size(); + } +} diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 000000000..66bd841a3 --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class Ui { + private Scanner in; + + public Ui() { + in = new Scanner(System.in); + } + + public String getUserInput() { + return in.nextLine(); + } + + public void printMessage(String message) { + System.out.println(message); + } + + public void showLoadingError() { + System.err.println("Error loading tasks from file."); + } + + public void showWelcome() { + showLine(); + System.out.println("Hello! I'm Brennan!"); + System.out.println("What can I do for you?\n"); + } + + public void showLine() { + System.out.println("____________________________________________________________"); + } +} From 8aa350c36425d545033ca7cb8efce2aff2404489 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 8 Mar 2024 18:44:30 +0800 Subject: [PATCH 11/14] Add find functionality --- data/taskCategory.txt | 9 +++-- src/main/java/Duke.java | 2 ++ src/main/java/DukeException.java | 2 -- src/main/java/Parser.java | 3 ++ src/main/java/Storage.java | 2 -- src/main/java/Task.java | 6 ---- src/main/java/TaskList.java | 55 ++++++++++++++---------------- src/main/java/ToDo.java | 2 -- src/main/java/Ui.java | 58 +++++++++++++++++++++++++++++--- 9 files changed, 89 insertions(+), 50 deletions(-) diff --git a/data/taskCategory.txt b/data/taskCategory.txt index 27eaec113..a97e3c300 100644 --- a/data/taskCategory.txt +++ b/data/taskCategory.txt @@ -1,3 +1,6 @@ -D| assignment (by: today))))))))))))|0| today))))))))))) -E| presentation (from: 2pm to: 4pm)))))))))|0| 2pm | 4pm)))))))) -D|deadline coding|1|null +D| assignment (by: today)))))))))))))))))|0| today)))))))))))))))) +E| presentation (from: 2pm to: 4pm))))))))))))))|0| 2pm | 4pm))))))))))))) +D|deadline coding|0|null +T| analyse book|1 +D|deadline return book in library|1|null +T| cleaning|1 diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 9a022477d..06df08873 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -15,6 +15,7 @@ public Duke(String filePath) { tasks = new TaskList(); } } + public void run() { ui.showWelcome(); boolean isExit = false; @@ -31,6 +32,7 @@ public void run() { } } } + public static void main(String[] args) { new Duke("data/taskCategory.txt").run(); } diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java index 3bf13ede5..7f21aba95 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/DukeException.java @@ -18,14 +18,12 @@ public static void handleException(Exception exception, String input) { System.out.println("Incomplete " + splitInput[0] + " detected. " + "Your statement is not clear. Please fix this."); } - } else if (exception instanceof IllegalArgumentException) { System.out.println("Unfamiliar commands cannot be accepted by the system."); } else { System.out.println("Unknown error detected. Try to fix this immediately."); } - System.out.println(SEPARATOR); } } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 6ad40dc0b..7d998e805 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -22,6 +22,9 @@ public boolean parseCommand(String command, TaskList tasks) throws DukeException } else if (splitCommand[0].equals("delete")) { checkCommandArguments(splitCommand, 2, tasks); tasks.deleteTask(Integer.parseInt(splitCommand[1])); + } else if (splitCommand[0].equals("find")) { + checkCommandArguments(splitCommand, 2, tasks); + tasks.findTasks(splitCommand[1], tasks.getTasks()); } else if (splitCommand[0].equals("bye")) { return true; // Indicate that the program should exit } else { diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 2b3721275..fb0e8300f 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -3,11 +3,9 @@ public class Storage { private static String filePath; - public Storage(String filePath) { this.filePath = filePath; } - public ArrayList load() throws DukeException { ArrayList tasks = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { diff --git a/src/main/java/Task.java b/src/main/java/Task.java index bd27d5aca..675bcfb6e 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -2,17 +2,14 @@ public class Task { protected String description; protected boolean isDone; protected String typeOfTask; - public Task(String description) { this.description = description; this.isDone = false; typeOfTask = ""; } - public String getStatusIcon() { return (isDone ? "X" : " "); } - public void markAsCompleted() { isDone = true; } @@ -24,7 +21,6 @@ public void markAsNotCompleted() { public String toFileString() { return description + "|" + (isDone ? "1" : "0"); // Example format: "description|status" } - public static Task fromString(String fileString) { String[] parts = fileString.split("\\|"); String taskType = parts[0]; // Extract task type @@ -50,8 +46,6 @@ public static Task fromString(String fileString) { if (task != null && isDone) { task.markAsCompleted(); // Mark task as done if status is '1' } - return task; } - } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 7418582b8..93c895947 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,6 +1,6 @@ import java.util.ArrayList; public class TaskList { - private ArrayList tasks; + public static ArrayList tasks; public TaskList() { this.tasks = new ArrayList<>(); @@ -8,54 +8,48 @@ public TaskList() { public TaskList(ArrayList tasks) { this.tasks = tasks; } + public void addTask(Task task) { tasks.add(task); indicateNewTask(tasks.get(tasks.size() - 1), tasks.size()); } + public void deleteTask(int indexTask) { Task removedTask = tasks.get(indexTask - 1); tasks.remove(removedTask); - System.out.println("Command received. I've removed this task:"); - System.out.println((indexTask) + ". " + "[" + removedTask.getStatusIcon() + "]" + removedTask.description); - System.out.println("Currently you have " + tasks.size() + " tasks in the list below."); + Ui.deleteTaskDisplay(indexTask, removedTask); } - public void markTaskAsCompleted(int indexTask) { - tasks.get(indexTask - 1).markAsCompleted(); - - System.out.println("____________________________________________________________"); - System.out.println("Nice! I've marked this task as done:"); - System.out.println((indexTask) + ". " + "[" + tasks.get(indexTask - 1).getStatusIcon() + "]" + tasks.get(indexTask - 1).description); - System.out.println("____________________________________________________________"); + Ui.markedTasksAsCompletedDisplay(indexTask); } public void listTasks() { - System.out.println("Here are the tasks in your list: "); - System.out.println("____________________________________________________________"); - for (int i = 0; i < tasks.size(); i++) { - - System.out.println((i + 1) + ". " + " " + "[" + tasks.get(i).typeOfTask + "]" + "[" + tasks.get(i).getStatusIcon() + "]" + tasks.get(i).description); - } - System.out.println("____________________________________________________________"); + Ui.listTasksDisplay(); } public void markTaskAsNotCompleted(int indexTask) { - tasks.get(indexTask - 1).markAsNotCompleted(); - - System.out.println("____________________________________________________________"); - System.out.println(" OK, I've marked this task as not done yet:"); - System.out.println((indexTask) + ". " + "[" + tasks.get(indexTask - 1).getStatusIcon() + "]" + tasks.get(indexTask - 1).description); - System.out.println("____________________________________________________________"); + Ui.markedTasksAsNotCompletedDisplay(indexTask); } + public void indicateNewTask(Task newTask, int currentNumberOfTasks) { - System.out.println("____________________________________________________________"); - System.out.println("Well done, you've added a new task: "); - System.out.println("[" + newTask.typeOfTask + "]" + "[" + newTask.getStatusIcon() + "]" + newTask.description); - System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); - System.out.println("____________________________________________________________"); + Ui.indicateNewTaskDisplay(newTask, currentNumberOfTasks); + } + + public void findTasks(String keyword, ArrayList tasksList) { + ArrayList matchingTasks = new ArrayList<>(); + for (Task task : tasksList) { + if (task.description.toLowerCase().contains(keyword.toLowerCase())) { + matchingTasks.add(task); + } + } + if (matchingTasks.isEmpty()) { + Ui.noTasksFoundDisplay(); + } else { + Ui.showTasksFoundDisplay(matchingTasks); + } } public ArrayList getTasks() { @@ -65,7 +59,8 @@ public ArrayList getTasks() { public Task get(int index) { return tasks.get(index); } + public int size() { return tasks.size(); } -} +} \ No newline at end of file diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java index d246f85fe..130c70fd0 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/ToDo.java @@ -1,12 +1,10 @@ public class ToDo extends Task{ protected String dateOfDeadline; - public ToDo(String description) { super(description); typeOfTask = "T"; this.description = description.replace("todo", ""); } - @Override public String toFileString() { return "T|" + super.toFileString(); // Prefix with "T" to indicate Todo diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 66bd841a3..08ffa3146 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,30 +1,78 @@ +import java.util.ArrayList; import java.util.Scanner; public class Ui { private Scanner in; - public Ui() { in = new Scanner(System.in); } - public String getUserInput() { return in.nextLine(); } - public void printMessage(String message) { System.out.println(message); } - public void showLoadingError() { System.err.println("Error loading tasks from file."); } - public void showWelcome() { showLine(); System.out.println("Hello! I'm Brennan!"); System.out.println("What can I do for you?\n"); } + public static void deleteTaskDisplay(int indexTask, Task removedTask) { + System.out.println("Command received. I've removed this task:"); + System.out.println(indexTask + ". " + "[" + removedTask.getStatusIcon() + "]" + removedTask.description); + System.out.println("Currently you have " + TaskList.tasks.size() + " tasks in the list below."); + } + + public static void noTasksFoundDisplay() { + System.out.println("____________________________________________________________"); + System.out.println("No tasks found."); + System.out.println("____________________________________________________________"); + } + + public static void showTasksFoundDisplay(ArrayList matchingTasks) { + System.out.println("Here are all the tasks that match your search keyword: "); + System.out.println("____________________________________________________________"); + for (int i = 0; i < matchingTasks.size(); i++) { + System.out.println((i + 1) + ". " + " " + "[" + matchingTasks.get(i).typeOfTask + "]" + "[" + matchingTasks.get(i).getStatusIcon() + "]" + matchingTasks.get(i).description); + } + System.out.println("____________________________________________________________"); + } + + public static void markedTasksAsCompletedDisplay(int indexTask) { + System.out.println("____________________________________________________________"); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(indexTask + ". " + "[" + TaskList.tasks.get(indexTask - 1).getStatusIcon() + "]" + TaskList.tasks.get(indexTask - 1).description); + System.out.println("____________________________________________________________"); + } + + public static void listTasksDisplay() { + System.out.println("Here are the tasks in your list: "); + System.out.println("____________________________________________________________"); + for (int i = 0; i < TaskList.tasks.size(); i++) { + System.out.println((i + 1) + ". " + " " + "[" + TaskList.tasks.get(i).typeOfTask + "]" + "[" + TaskList.tasks.get(i).getStatusIcon() + "]" + TaskList.tasks.get(i).description); + } + System.out.println("____________________________________________________________"); + } + + public static void markedTasksAsNotCompletedDisplay(int indexTask) { + System.out.println("____________________________________________________________"); + System.out.println(" OK, I've marked this task as not done yet:"); + System.out.println((indexTask) + ". " + "[" + TaskList.tasks.get(indexTask - 1).getStatusIcon() + "]" + TaskList.tasks.get(indexTask - 1).description); + System.out.println("____________________________________________________________"); + } + + public static void indicateNewTaskDisplay(Task newTask, int currentNumberOfTasks) { + System.out.println("____________________________________________________________"); + System.out.println("Well done, you've added a new task: "); + System.out.println("[" + newTask.typeOfTask + "]" + "[" + newTask.getStatusIcon() + "]" + newTask.description); + System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); + System.out.println("____________________________________________________________"); + } + public void showLine() { System.out.println("____________________________________________________________"); } From 8a04325943861f31da9227697fd9589cf35fc462 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 8 Mar 2024 19:18:04 +0800 Subject: [PATCH 12/14] Add Javadoc comments --- src/main/java/Deadline.java | 32 ++++++++++++++- src/main/java/Duke.java | 26 ++++++++++++- src/main/java/DukeException.java | 19 +++++++-- src/main/java/Event.java | 47 ++++++++++++++++++++-- src/main/java/Parser.java | 25 +++++++++++- src/main/java/Storage.java | 21 +++++++++- src/main/java/TaskList.java | 67 +++++++++++++++++++++++++++----- src/main/java/ToDo.java | 21 +++++++++- src/main/java/Ui.java | 30 +++++++++++++- 9 files changed, 262 insertions(+), 26 deletions(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 207438847..620b47234 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,6 +1,20 @@ +/** + * Represents a task with a deadline. + */ public class Deadline extends Task { + + /** + * Date of the deadline. + */ protected String dateOfDeadline; + /** + * Constructs a Deadline task with the given description. + * If the description contains a deadline date, it is extracted and stored. + * Otherwise, the description is used directly. + * + * @param description The description of the task. + */ public Deadline(String description) { super(description); typeOfTask = "D"; @@ -17,17 +31,31 @@ public Deadline(String description) { } } + /** + * Gets the date of the deadline. + * + * @return The date of the deadline. + */ public String getDateOfDeadline() { return dateOfDeadline; } + /** + * Sets the date of the deadline. + * + * @param dateOfDeadline The date of the deadline. + */ public void setDateOfDeadline(String dateOfDeadline) { this.dateOfDeadline = dateOfDeadline; } + /** + * Converts the Deadline task to a string suitable for storage in a file. + * + * @return A string representation of the Deadline task for file storage. + */ @Override public String toFileString() { return "D|" + super.toFileString() + "|" + dateOfDeadline; // Prefix with "D" to indicate Deadline } -} - +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 9a022477d..f312f4fd9 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,9 +1,20 @@ + +/** + * Duke is a task management application that helps users manage their tasks. + * It allows users to add, delete, mark as done, and list tasks. + * Duke stores tasks in a file and loads them upon startup. + */ public class Duke { private Storage storage; private TaskList tasks; private Ui ui; private Parser parser; + /** + * Constructs a Duke instance with the specified file path. + * + * @param filePath The file path where tasks are stored. + */ public Duke(String filePath) { ui = new Ui(); storage = new Storage(filePath); @@ -15,6 +26,11 @@ public Duke(String filePath) { tasks = new TaskList(); } } + + /** + * Runs the Duke application. + * Displays welcome message, handles user commands until exit command is received. + */ public void run() { ui.showWelcome(); boolean isExit = false; @@ -23,7 +39,6 @@ public void run() { String fullCommand = ui.getUserInput(); ui.showLine(); // show the divider line ("_______") isExit = parser.parseCommand(fullCommand, tasks); - // Assuming DukeException is a custom exception class that you've defined elsewhere } catch (DukeException e) { ui.printMessage("Error: " + e.getMessage()); } finally { @@ -31,7 +46,14 @@ public void run() { } } } + + /** + * The main method to start the Duke application. + * Creates a new instance of Duke and runs it. + * + * @param args Command line arguments (not used in this implementation). + */ public static void main(String[] args) { new Duke("data/taskCategory.txt").run(); } -} +} \ No newline at end of file diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java index 3bf13ede5..3629d9a82 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/DukeException.java @@ -1,5 +1,19 @@ public class DukeException extends Exception { - + /** + * Handles exceptions that occur during Duke application operations. + * It prints appropriate error messages based on the type of exception. + * If the exception is related to array index out of bounds, + * it notifies the user about exceeding the task limit. + * If the exception is related to string index out of bounds, + * it identifies whether the input is incomplete or unclear, + * depending on the operation being performed. + * If the exception is an illegal argument exception, + * it indicates that the command provided is unfamiliar. + * For any other type of exception, it displays a generic unknown error message. + * + * @param exception The exception that occurred. + * @param input The input string where the exception occurred. + */ public static void handleException(Exception exception, String input) { final String SEPARATOR = "====================================================================================================================="; @@ -28,5 +42,4 @@ public static void handleException(Exception exception, String input) { System.out.println(SEPARATOR); } -} - +} \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 5cb293b27..58fa7e9fc 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,8 +1,24 @@ +/** + * Represents an event task with starting and ending dates. + */ public class Event extends Task { + /** + * Starting date of the event. + */ protected String startingDate; + + /** + * Ending date of the event. + */ protected String endingDate; + /** + * Constructs an Event object with the given description. + * The description format should include "from" and "to" to specify starting and ending dates respectively. + * + * @param description Description of the event task. + */ public Event(String description) { super(description); typeOfTask = "E"; @@ -15,9 +31,7 @@ public Event(String description) { if (dividerIndexFrom == -1) { setStartingDate(null); - } - - else if (dividerIndexTo == -1) { + } else if (dividerIndexTo == -1) { setEndingDate(null); } @@ -26,24 +40,49 @@ else if (dividerIndexTo == -1) { this.description = descriptionWithoutDate + " (from: " + getStartingDate() + " to: " + getEndingDate() + ")"; } + /** + * Gets the starting date of the event. + * + * @return The starting date of the event. + */ public String getStartingDate() { return startingDate; } + /** + * Sets the starting date of the event. + * + * @param startingDate The starting date to be set. + */ public void setStartingDate(String startingDate) { this.startingDate = startingDate; } + /** + * Gets the ending date of the event. + * + * @return The ending date of the event. + */ public String getEndingDate() { return endingDate; } + /** + * Sets the ending date of the event. + * + * @param endingDate The ending date to be set. + */ public void setEndingDate(String endingDate) { this.endingDate = endingDate; } + /** + * Converts the Event object to a string suitable for writing to a file. + * + * @return A string representation of the Event object in file format. + */ @Override public String toFileString() { return "E|" + super.toFileString() + "|" + startingDate + "|" + endingDate; // Prefix with "E" to indicate Event } -} +} \ No newline at end of file diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 6ad40dc0b..5729d1420 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -1,4 +1,18 @@ +/** + * The Parser class is responsible for interpreting user commands and executing corresponding actions on the TaskList. + * It parses user input strings, identifies the command type, and delegates the execution to the appropriate methods + * in the TaskList class. + */ public class Parser { + + /** + * Parses the given command and executes corresponding actions on the TaskList. + * + * @param command The command to be parsed and executed. + * @param tasks The TaskList object on which actions will be executed. + * @return True if the program should exit after executing the command, false otherwise. + * @throws DukeException If an error occurs during command execution. + */ public boolean parseCommand(String command, TaskList tasks) throws DukeException { String[] splitCommand = command.split(" "); try { @@ -36,6 +50,15 @@ public boolean parseCommand(String command, TaskList tasks) throws DukeException return false; // Default return value if the command does not require exiting } + /** + * Checks if the number of command arguments is valid. + * + * @param splitCommand The array containing the split command. + * @param expectedArgs The expected number of arguments for the command. + * @param tasks The TaskList object on which actions are being performed. + * @throws ArrayIndexOutOfBoundsException If the number of tasks exceeds 100. + * @throws StringIndexOutOfBoundsException If the number of arguments is less than expected. + */ private void checkCommandArguments(String[] splitCommand, int expectedArgs, TaskList tasks) { if (tasks.size() >= 100) { throw new ArrayIndexOutOfBoundsException(); @@ -44,4 +67,4 @@ private void checkCommandArguments(String[] splitCommand, int expectedArgs, Task throw new StringIndexOutOfBoundsException(); } } -} +} \ No newline at end of file diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 2b3721275..ab0f8aa64 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -1,13 +1,27 @@ import java.io.*; import java.util.ArrayList; +/** + * The Storage class manages the loading and saving of tasks to a file. + */ public class Storage { private static String filePath; + /** + * Constructs a Storage object with the specified file path. + * + * @param filePath The file path where tasks will be stored. + */ public Storage(String filePath) { this.filePath = filePath; } + /** + * Loads tasks from the file specified by the file path. + * + * @return An ArrayList containing the loaded tasks. + * @throws DukeException If an error occurs while loading tasks. + */ public ArrayList load() throws DukeException { ArrayList tasks = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { @@ -24,6 +38,11 @@ public ArrayList load() throws DukeException { return tasks; } + /** + * Saves the provided tasks to the file specified by the file path. + * + * @param tasks The ArrayList of tasks to be saved. + */ public static void save(ArrayList tasks) { try (FileWriter writer = new FileWriter(filePath)) { for (Task task : tasks) { @@ -33,4 +52,4 @@ public static void save(ArrayList tasks) { System.err.println("Error saving tasks to file: " + e.getMessage()); } } -} +} \ No newline at end of file diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index 7418582b8..8641afe83 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,17 +1,43 @@ import java.util.ArrayList; + +/** + * Represents a list of tasks. + */ public class TaskList { + private ArrayList tasks; + /** + * Constructs an empty TaskList. + */ public TaskList() { this.tasks = new ArrayList<>(); } + + /** + * Constructs a TaskList with the given list of tasks. + * + * @param tasks The list of tasks to initialize the TaskList. + */ public TaskList(ArrayList tasks) { this.tasks = tasks; } + + /** + * Adds a task to the task list. + * + * @param task The task to be added. + */ public void addTask(Task task) { tasks.add(task); indicateNewTask(tasks.get(tasks.size() - 1), tasks.size()); } + + /** + * Deletes the task at the specified index from the task list. + * + * @param indexTask The index of the task to be deleted. + */ public void deleteTask(int indexTask) { Task removedTask = tasks.get(indexTask - 1); tasks.remove(removedTask); @@ -20,36 +46,50 @@ public void deleteTask(int indexTask) { System.out.println("Currently you have " + tasks.size() + " tasks in the list below."); } - + /** + * Marks the task at the specified index as completed. + * + * @param indexTask The index of the task to be marked as completed. + */ public void markTaskAsCompleted(int indexTask) { - tasks.get(indexTask - 1).markAsCompleted(); - System.out.println("____________________________________________________________"); System.out.println("Nice! I've marked this task as done:"); System.out.println((indexTask) + ". " + "[" + tasks.get(indexTask - 1).getStatusIcon() + "]" + tasks.get(indexTask - 1).description); System.out.println("____________________________________________________________"); } + /** + * Lists all the tasks in the task list. + */ public void listTasks() { System.out.println("Here are the tasks in your list: "); System.out.println("____________________________________________________________"); for (int i = 0; i < tasks.size(); i++) { - System.out.println((i + 1) + ". " + " " + "[" + tasks.get(i).typeOfTask + "]" + "[" + tasks.get(i).getStatusIcon() + "]" + tasks.get(i).description); } System.out.println("____________________________________________________________"); } + /** + * Marks the task at the specified index as not completed. + * + * @param indexTask The index of the task to be marked as not completed. + */ public void markTaskAsNotCompleted(int indexTask) { - tasks.get(indexTask - 1).markAsNotCompleted(); - System.out.println("____________________________________________________________"); System.out.println(" OK, I've marked this task as not done yet:"); System.out.println((indexTask) + ". " + "[" + tasks.get(indexTask - 1).getStatusIcon() + "]" + tasks.get(indexTask - 1).description); System.out.println("____________________________________________________________"); } + + /** + * Indicates the addition of a new task. + * + * @param newTask The new task added. + * @param currentNumberOfTasks The current number of tasks after addition. + */ public void indicateNewTask(Task newTask, int currentNumberOfTasks) { System.out.println("____________________________________________________________"); System.out.println("Well done, you've added a new task: "); @@ -58,14 +98,21 @@ public void indicateNewTask(Task newTask, int currentNumberOfTasks) { System.out.println("____________________________________________________________"); } + /** + * Gets the list of tasks. + * + * @return The list of tasks. + */ public ArrayList getTasks() { return tasks; } - public Task get(int index) { - return tasks.get(index); - } + /** + * Gets the number of tasks in the task list. + * + * @return The number of tasks in the task list. + */ public int size() { return tasks.size(); } -} +} \ No newline at end of file diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java index d246f85fe..a1b43c2a4 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/ToDo.java @@ -1,15 +1,32 @@ -public class ToDo extends Task{ +/** + * Represents a ToDo task, a type of Task with no specific deadline. + */ +public class ToDo extends Task { + + /** + * The dateOfDeadline for the ToDo task. + */ protected String dateOfDeadline; + /** + * Constructs a ToDo task with the specified description. + * + * @param description The description of the ToDo task. + */ public ToDo(String description) { super(description); typeOfTask = "T"; this.description = description.replace("todo", ""); } + /** + * Converts the ToDo task to a string format suitable for saving to a file. + * + * @return A string representing the ToDo task in file format. + */ @Override public String toFileString() { return "T|" + super.toFileString(); // Prefix with "T" to indicate Todo } -} +} \ No newline at end of file diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 66bd841a3..16ef336af 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,31 +1,59 @@ import java.util.Scanner; +/** + * Provides a user interface for interacting with the program. + */ public class Ui { + /** + * Scanner object for user input. + */ private Scanner in; + /** + * Constructs a new Ui object with a Scanner for input. + */ public Ui() { in = new Scanner(System.in); } + /** + * Retrieves user input from the console. + * + * @return The user input as a String. + */ public String getUserInput() { return in.nextLine(); } + /** + * Prints a message to the console. + * + * @param message The message to be printed. + */ public void printMessage(String message) { System.out.println(message); } + /** + * Prints an error message indicating failure to load tasks from a file. + */ public void showLoadingError() { System.err.println("Error loading tasks from file."); } + /** + * Prints a welcome message to the console. + */ public void showWelcome() { showLine(); System.out.println("Hello! I'm Brennan!"); System.out.println("What can I do for you?\n"); } + /** + * Prints a line of dashes to the console for visual separation. + */ public void showLine() { System.out.println("____________________________________________________________"); } -} +} \ No newline at end of file From 224c57dbba451f72b02f5127d32487c7fd8b9a8a Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 8 Mar 2024 20:31:52 +0800 Subject: [PATCH 13/14] Update README.md, to contain user guide for the whole application --- README.md | 4 +- data/taskCategory.txt | 7 +- docs/README.md | 223 +++++++++++++++++- src/main/java/{Duke.java => Brennan.java} | 10 +- ...keException.java => BrennanException.java} | 2 +- src/main/java/META-INF/MANIFEST.MF | 2 +- src/main/java/Parser.java | 7 +- src/main/java/Storage.java | 6 +- src/main/java/TaskList.java | 2 +- src/main/java/Ui.java | 2 +- text-ui-test/runtest.bat | 2 +- 11 files changed, 233 insertions(+), 34 deletions(-) rename src/main/java/{Duke.java => Brennan.java} (88%) rename src/main/java/{DukeException.java => BrennanException.java} (97%) diff --git a/README.md b/README.md index 8715d4d91..e1125b847 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Brennan 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 +13,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/Brennan.java` file, right-click it, and choose `Run Brennan.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 ____ _ diff --git a/data/taskCategory.txt b/data/taskCategory.txt index b92a78fd9..bc35dbd52 100644 --- a/data/taskCategory.txt +++ b/data/taskCategory.txt @@ -1,5 +1,2 @@ -D| assignment (by: today))))))))))))))))))))|0| today))))))))))))))))))) -D|deadline coding|0|null -T| analyse book|0 -T| haircut|0 -E| show (from: 5pm to: 8pm)))|0| 5pm | 8pm)) +D| Submit essay (by: 2024-03-17))|0| 2024-03-17) +E| Marriage (from: 2pm to: 5pm))|0| 2pm | 5pm) diff --git a/docs/README.md b/docs/README.md index 8077118eb..746b6419d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,230 @@ +# Brennan-Task Management Application + # User Guide -## Features +## Features + +### Adding Tasks + +This feature allows users to add different types of tasks such as Todo, Deadline, and Event to the task list. + +### Listing Tasks + +This feature enables users to view all the tasks currently stored in the task list. + +### Marking Tasks as Completed -### Feature-ABC +With this feature, users can mark tasks as completed, indicating that they have been finished. -Description of the feature. +### Unmarking Completed tasks -### Feature-XYZ +With this feature, users can unmark tasks which were completed, indicating that they have not been finished. -Description of the feature. +### Deleting Tasks + +Users can delete tasks from the task list using this feature. + +### Finding Tasks + +This feature allows users to search for tasks containing specific keywords. + +### Exiting the Application + +This feature allows users to gracefully exit the application. ## Usage -### `Keyword` - Describe action +### `todo` - Add a Todo task + +Adds a Todo task to the task list. + +Example of usage: + +`todo Buy meat` + +Expected outcome: + +A new Todo task "Buy meat" is added to the task list. + +``` +____________________________________________________________ +____________________________________________________________ +Well done, you've added a new task: +[T][ ] Buy meat +Currently you have 6 task(s) in your list! +____________________________________________________________ +____________________________________________________________ + +``` + +### `deadline` - Add a Deadline task + +Adds a Deadline task to the task list. + +Example of usage: + +`deadline Submit essay /by: 2024-03-17` + +Expected outcome: + +A new Deadline task "Submit essay" with the deadline "2024-03-17" is added to the task list. + +``` +____________________________________________________________ +____________________________________________________________ +Well done, you've added a new task: +[D][ ] Submit essay (by: 2024-03-17) +Currently you have 7 task(s) in your list! +____________________________________________________________ +____________________________________________________________ +``` + +### `event` - Add an Event task + +Adds an Event task to the task list. + +Example of usage: + +`event Marriage /from: 2pm /to:5pm` + +Expected outcome: + +A new Event task "Marriage" from 2p to 5pm +``` +____________________________________________________________ +____________________________________________________________ +Well done, you've added a new task: +[E][ ] Marriage (from: 2pm to: 5pm) +Currently you have 8 task(s) in your list! +____________________________________________________________ +____________________________________________________________ +``` + +### `list` - List all tasks + +Displays all tasks currently stored in the task list. + +Example of usage: + +`list` + +Expected outcome: + +A list of all tasks currently stored in the task list is displayed. -Describe the action and its outcome. +``` +____________________________________________________________ +Here are the tasks in your list: +____________________________________________________________ +1. [T][ ] analyse book +2. [T][ ] haircut +3. [D][ ] Submit essay (by: 2024-03-17) +4. [E][ ] Marriage (from: 2pm to: 5pm) +____________________________________________________________ +____________________________________________________________ + +``` + +### `mark` - Mark a task as completed + +Marks a task as completed based on its index in the task list. + +Example of usage: + +`mark 2` + +Expected outcome: + +The task at index 2 is marked as completed. + +``` +____________________________________________________________ +____________________________________________________________ +Nice! I've marked this task as done: +2. [X] haircut +____________________________________________________________ +____________________________________________________________ -Example of usage: +``` + +### `unmark` - Unmark a task + +Unmarks a previously completed task, setting it back to an incomplete status. -`keyword (optional arguments)` +Example of usage: + +`unmark 2` Expected outcome: -Description of the outcome. +The task at index 2 is marked as incomplete. ``` -expected output +____________________________________________________________ +____________________________________________________________ + OK, I've marked this task as not done yet: +2. [ ] Marriage (from: 2pm to: 5pm) +____________________________________________________________ +____________________________________________________________ ``` + +### `delete` - Delete a task + +Deletes a task from the task list based on its index. + +Example of usage: + +`delete 1` + +Expected outcome: + +The task at index 1 is deleted from the task list. + +``` +____________________________________________________________ +Command received. I've removed this task: +1. [ ] analyse book +Currently you have 2 tasks in the list below. +____________________________________________________________ + +``` + +### `find` - Find tasks + +Searches for tasks containing a specific keyword. + +Example of usage: + +`find essay` + +Expected outcome: + +Tasks containing the keyword "essay" are displayed. + +``` +____________________________________________________________ +Here are all the tasks that match your search keyword: +____________________________________________________________ +1. [D][ ] Submit essay (by: 2024-03-17) +____________________________________________________________ +____________________________________________________________ + +``` +### `bye` - Exit the application + +Terminates the application and closes the program. + +Example of usage: + +`bye` + +Expected outcome: + +The application terminates and the program closes. + +``` +____________________________________________________________ +Goodbye! Have a nice day! +____________________________________________________________ + +``` \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Brennan.java similarity index 88% rename from src/main/java/Duke.java rename to src/main/java/Brennan.java index f312f4fd9..410bf26a7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Brennan.java @@ -4,7 +4,7 @@ * It allows users to add, delete, mark as done, and list tasks. * Duke stores tasks in a file and loads them upon startup. */ -public class Duke { +public class Brennan { private Storage storage; private TaskList tasks; private Ui ui; @@ -15,13 +15,13 @@ public class Duke { * * @param filePath The file path where tasks are stored. */ - public Duke(String filePath) { + public Brennan(String filePath) { ui = new Ui(); storage = new Storage(filePath); parser = new Parser(); try { tasks = new TaskList(storage.load()); - } catch (DukeException e) { + } catch (BrennanException e) { ui.showLoadingError(); tasks = new TaskList(); } @@ -39,7 +39,7 @@ public void run() { String fullCommand = ui.getUserInput(); ui.showLine(); // show the divider line ("_______") isExit = parser.parseCommand(fullCommand, tasks); - } catch (DukeException e) { + } catch (BrennanException e) { ui.printMessage("Error: " + e.getMessage()); } finally { ui.showLine(); @@ -54,6 +54,6 @@ public void run() { * @param args Command line arguments (not used in this implementation). */ public static void main(String[] args) { - new Duke("data/taskCategory.txt").run(); + new Brennan("data/taskCategory.txt").run(); } } \ No newline at end of file diff --git a/src/main/java/DukeException.java b/src/main/java/BrennanException.java similarity index 97% rename from src/main/java/DukeException.java rename to src/main/java/BrennanException.java index ae52c3c82..abf9338ef 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/BrennanException.java @@ -1,4 +1,4 @@ -public class DukeException extends Exception { +public class BrennanException extends Exception { /** * Handles exceptions that occur during Duke application operations. * It prints appropriate error messages based on the type of exception. diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF index 9f37e4e0a..ea5a6e50e 100644 --- a/src/main/java/META-INF/MANIFEST.MF +++ b/src/main/java/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ Manifest-Version: 1.0 -Main-Class: Duke +Main-Class: Brennan diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index f9fa3d6eb..c872d301a 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -11,9 +11,9 @@ public class Parser { * @param command The command to be parsed and executed. * @param tasks The TaskList object on which actions will be executed. * @return True if the program should exit after executing the command, false otherwise. - * @throws DukeException If an error occurs during command execution. + * @throws BrennanException If an error occurs during command execution. */ - public boolean parseCommand(String command, TaskList tasks) throws DukeException { + public boolean parseCommand(String command, TaskList tasks) throws BrennanException { String[] splitCommand = command.split(" "); try { if (splitCommand[0].equals("list")) { @@ -40,12 +40,13 @@ public boolean parseCommand(String command, TaskList tasks) throws DukeException checkCommandArguments(splitCommand, 2, tasks); tasks.findTasks(splitCommand[1], tasks.getTasks()); } else if (splitCommand[0].equals("bye")) { + System.out.println("Goodbye! Have a nice day!"); return true; // Indicate that the program should exit } else { throw new IllegalArgumentException(); } } catch (Exception e) { - DukeException.handleException(e, command); + BrennanException.handleException(e, command); } finally { Storage.save(tasks.getTasks()); diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index ab0f8aa64..4d5329591 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -20,9 +20,9 @@ public Storage(String filePath) { * Loads tasks from the file specified by the file path. * * @return An ArrayList containing the loaded tasks. - * @throws DukeException If an error occurs while loading tasks. + * @throws BrennanException If an error occurs while loading tasks. */ - public ArrayList load() throws DukeException { + public ArrayList load() throws BrennanException { ArrayList tasks = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; @@ -33,7 +33,7 @@ public ArrayList load() throws DukeException { } } } catch (IOException e) { - DukeException.handleException(e, "null"); + BrennanException.handleException(e, "null"); } return tasks; } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index bfbf6e558..359c5017f 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -118,4 +118,4 @@ public ArrayList getTasks() { public int size() { return tasks.size(); } -} +} \ No newline at end of file diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index ccfbd9f36..76a2b383d 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -141,4 +141,4 @@ public static void indicateNewTaskDisplay(Task newTask, int currentNumberOfTasks System.out.println("Currently you have " + currentNumberOfTasks + " task(s) in your list!"); System.out.println("____________________________________________________________"); } -} +} \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..0287d12d2 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -15,7 +15,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 Brennan < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From 6a351f994ab3ed1cbea6855b48c5d2326e50d5d4 Mon Sep 17 00:00:00 2001 From: Praneet-25 Date: Fri, 8 Mar 2024 21:14:54 +0800 Subject: [PATCH 14/14] Added relative path --- data/taskCategory.txt | 2 - src/main/java/Brennan.java | 2 +- src/main/java/Storage.java | 83 ++++++++++++++++++++++++++------------ 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/data/taskCategory.txt b/data/taskCategory.txt index bc35dbd52..e69de29bb 100644 --- a/data/taskCategory.txt +++ b/data/taskCategory.txt @@ -1,2 +0,0 @@ -D| Submit essay (by: 2024-03-17))|0| 2024-03-17) -E| Marriage (from: 2pm to: 5pm))|0| 2pm | 5pm) diff --git a/src/main/java/Brennan.java b/src/main/java/Brennan.java index 410bf26a7..ca37ab733 100644 --- a/src/main/java/Brennan.java +++ b/src/main/java/Brennan.java @@ -54,6 +54,6 @@ public void run() { * @param args Command line arguments (not used in this implementation). */ public static void main(String[] args) { - new Brennan("data/taskCategory.txt").run(); + new Brennan("./data/taskCategory.txt").run(); } } \ No newline at end of file diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 4d5329591..c5dccd785 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -15,35 +15,46 @@ public class Storage { public Storage(String filePath) { this.filePath = filePath; } - - /** - * Loads tasks from the file specified by the file path. - * - * @return An ArrayList containing the loaded tasks. - * @throws BrennanException If an error occurs while loading tasks. - */ - public ArrayList load() throws BrennanException { - ArrayList tasks = new ArrayList<>(); - try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { - String line; - while ((line = reader.readLine()) != null) { - Task task = Task.fromString(line); - if (task != null) { - tasks.add(task); - } +/** + * Loads tasks from the file specified by the file path. + * + * @return An ArrayList containing the loaded tasks. + * @throws BrennanException If an error occurs while loading tasks. + */ +public ArrayList load() throws BrennanException { + ArrayList tasks = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { + String line; + while ((line = reader.readLine()) != null) { + Task task = Task.fromString(line); + if (task != null) { + tasks.add(task); } - } catch (IOException e) { - BrennanException.handleException(e, "null"); } - return tasks; + } catch (FileNotFoundException e) { + // Handle the case where the file doesn't exist + System.err.println("File not found. Created a new file!"); + createNewFile(); + } catch (IOException e) { + BrennanException.handleException(e, "null"); } + return tasks; +} - /** - * Saves the provided tasks to the file specified by the file path. - * - * @param tasks The ArrayList of tasks to be saved. - */ - public static void save(ArrayList tasks) { +/** + * Saves the provided tasks to the file specified by the file path. + * + * @param tasks The ArrayList of tasks to be saved. + */ +public static void save(ArrayList tasks) { + try { + File file = new File(filePath); + if (!file.getParentFile().exists()) { + //If the folder does not exist, create it + file.getParentFile().mkdirs(); + } + //If the file doesn't exist, create it + file.createNewFile(); try (FileWriter writer = new FileWriter(filePath)) { for (Task task : tasks) { writer.write(task.toFileString() + "\n"); @@ -51,5 +62,25 @@ public static void save(ArrayList tasks) { } catch (IOException e) { System.err.println("Error saving tasks to file: " + e.getMessage()); } + } catch (Exception e) { + throw new RuntimeException(e); + } +} + +/** + * Creates a new file and folder if they don't exist. + */ +private static void createNewFile() { + try { + File file = new File(filePath); + if (!file.getParentFile().exists()) { + // If the folder does not exist, create it + file.getParentFile().mkdirs(); + } + // Create the file if it doesn't exist + file.createNewFile(); + } catch (IOException ioException) { + System.err.println("Creation of a new file has failed: " + ioException.getMessage()); } -} \ No newline at end of file +} +}