From 25a09cf7d1100723055251dc45deccd0e2177528 Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Tue, 24 Aug 2021 20:01:58 +0800 Subject: [PATCH 01/10] level-1 --- src/main/java/Duke.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334..ea361ef5 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,6 @@ +import java.util.Arrays; +import java.util.Scanner; + public class Duke { public static void main(String[] args) { String logo = " ____ _ \n" @@ -6,5 +9,28 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + + System.out.println("____________________________________________________________"); + System.out.println("Hello! I'm Duke "); + System.out.println("What can I do for you?"); + System.out.println("____________________________________________________________"); + String line = ""; + + while(true) { + + Scanner in = new Scanner(System.in); + line = in.nextLine(); + if(line.equalsIgnoreCase("bye")) + break; + System.out.println("____________________________________________________________"); + System.out.println(line); + System.out.println("____________________________________________________________"); + + } + System.out.println("____________________________________________________________"); + System.out.println("Bye. Hope to see you again soon!"); + System.out.println("____________________________________________________________"); } + + } From 34072cb3d289e4955da7b2cf18959babe016fcab Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Tue, 24 Aug 2021 20:29:33 +0800 Subject: [PATCH 02/10] Level-2 --- src/main/java/Duke.java | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ea361ef5..e190543e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,3 @@ -import java.util.Arrays; import java.util.Scanner; public class Duke { @@ -15,17 +14,27 @@ public static void main(String[] args) { System.out.println("What can I do for you?"); System.out.println("____________________________________________________________"); String line = ""; - + String[] lists = new String[100]; + int counter=0; while(true) { Scanner in = new Scanner(System.in); line = in.nextLine(); if(line.equalsIgnoreCase("bye")) break; - System.out.println("____________________________________________________________"); - System.out.println(line); - System.out.println("____________________________________________________________"); - + if(line.equalsIgnoreCase("list")){ + System.out.println("____________________________________________________________"); + for (int i = 0; i < counter; i++) { + System.out.println(i+1 + ". " + lists[i]); + } + System.out.println("____________________________________________________________"); + }else { + System.out.println("____________________________________________________________"); + lists[counter]=line; + counter++; + System.out.println("added: " + line); + System.out.println("____________________________________________________________"); + } } System.out.println("____________________________________________________________"); System.out.println("Bye. Hope to see you again soon!"); From c32a0861e72d7b6d32b1370edc505823367e2ed0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Tue, 7 Sep 2021 19:19:44 +0800 Subject: [PATCH 03/10] level-3 --- src/main/java/Duke.java | 19 ++++++++++++++----- src/main/java/Task.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e190543e..5d7bc82f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -14,8 +14,8 @@ public static void main(String[] args) { System.out.println("What can I do for you?"); System.out.println("____________________________________________________________"); String line = ""; - String[] lists = new String[100]; - int counter=0; + Task[] lists = new Task[100]; + int counter = 0; while(true) { Scanner in = new Scanner(System.in); @@ -25,12 +25,21 @@ public static void main(String[] args) { if(line.equalsIgnoreCase("list")){ System.out.println("____________________________________________________________"); for (int i = 0; i < counter; i++) { - System.out.println(i+1 + ". " + lists[i]); + System.out.println(i+1 + "." + lists[i].print()); } System.out.println("____________________________________________________________"); - }else { + }else if (line.substring( 0 , 4 ).equalsIgnoreCase("done")) { System.out.println("____________________________________________________________"); - lists[counter]=line; + String[] input = line.split(" ",2); + String desc = input[0]; + int x = Integer.parseInt(input[1]);; + lists[x-1].setDone(true); + System.out.println("Nice! I've marked this task as done: "); + System.out.println(lists[x-1].print()); + System.out.println("____________________________________________________________"); + } else { + System.out.println("____________________________________________________________"); + lists[counter]= new Task(line); counter++; System.out.println("added: " + line); System.out.println("____________________________________________________________"); diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 00000000..90276516 --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,29 @@ +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 setDone(boolean done){ + this.isDone = done; + } + + public String getDescription(){ + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String print(){ + String print = " [" + getStatusIcon()+"] "+getDescription(); + return print; + } +} From 2a389bf311d9826e663461248d48b7ea6869ef90 Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Tue, 7 Sep 2021 20:18:42 +0800 Subject: [PATCH 04/10] level-4 --- src/main/java/Deadline.java | 14 +++++ src/main/java/Duke.java | 110 ++++++++++++++++++++++++++---------- src/main/java/Event.java | 14 +++++ src/main/java/Task.java | 4 +- src/main/java/ToDo.java | 12 ++++ 5 files changed, 122 insertions(+), 32 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 00000000..106b5f6f --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,14 @@ +public class Deadline extends Task{ + protected String by; + + public Deadline(String description, String by) { + super(description); + this.by = by; + } + + @Override + public String toString() { + return "[D]" + super.toString() + " (by: " + by + ")"; + } + +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d7bc82f..55c2a5ed 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,53 +1,103 @@ import java.util.Scanner; public class Duke { + public static void printbreak() { + System.out.println("____________________________________________________________"); + } + + public static void printbye() { + System.out.println("Bye. Hope to see you again soon!"); + } + + public static void printtotaltask(int counter) { + System.out.println("Now you have " + counter + " tasks in the list."); + } + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - System.out.println("____________________________________________________________"); + System.out.println("Hello from\n" + logo); + printbreak(); System.out.println("Hello! I'm Duke "); System.out.println("What can I do for you?"); - System.out.println("____________________________________________________________"); - String line = ""; + printbreak(); + String line; Task[] lists = new Task[100]; int counter = 0; - while(true) { + String desc; + String desc2 = ""; + String s1, s2; + while (true) { Scanner in = new Scanner(System.in); line = in.nextLine(); - if(line.equalsIgnoreCase("bye")) - break; - if(line.equalsIgnoreCase("list")){ - System.out.println("____________________________________________________________"); - for (int i = 0; i < counter; i++) { - System.out.println(i+1 + "." + lists[i].print()); + if (!line.isEmpty()) { + if (line.contains(" ")) { + String[] input = line.split(" ", 2); + desc = input[0]; + desc2 = input[1]; + } else { + desc = line; + } + if (line.equalsIgnoreCase("bye")) + break; + if (line.equalsIgnoreCase("list")) { + printbreak(); + for (int i = 0; i < counter; i++) { + System.out.println(i + 1 + ". " + lists[i].toString()); + } + printbreak(); + } else if (desc.equalsIgnoreCase("done")) { + printbreak(); + int x = Integer.parseInt(desc2); + lists[x - 1].setDone(true); + System.out.println("Nice! I've marked this task as done: "); + System.out.println(lists[x - 1].toString()); + printbreak(); + } else if (desc.equalsIgnoreCase("deadline")) { + printbreak(); + String[] input = desc2.split("/by ", 2); + s1 = input[0]; + s2 = input[1]; + lists[counter] = new Deadline(s1, s2); + System.out.println("Got it. I've added this task: "); + System.out.println(lists[counter].toString()); + counter++; + printtotaltask(counter); + printbreak(); + } else if (desc.equalsIgnoreCase("event")) { + printbreak(); + String[] input = desc2.split("/at ", 2); + s1 = input[0]; + s2 = input[1]; + lists[counter] = new Event(s1, s2); + System.out.println("Got it. I've added this task: "); + System.out.println(lists[counter].toString()); + counter++; + printtotaltask(counter); + printbreak(); + } else { + printbreak(); + if (desc.equalsIgnoreCase("todo")){ + lists[counter] = new ToDo(desc2); + }else { + lists[counter] = new ToDo(line); + } + System.out.println("Got it. I've added this task: "); + System.out.println(lists[counter].toString()); + counter++; + printtotaltask(counter); + printbreak(); } - System.out.println("____________________________________________________________"); - }else if (line.substring( 0 , 4 ).equalsIgnoreCase("done")) { - System.out.println("____________________________________________________________"); - String[] input = line.split(" ",2); - String desc = input[0]; - int x = Integer.parseInt(input[1]);; - lists[x-1].setDone(true); - System.out.println("Nice! I've marked this task as done: "); - System.out.println(lists[x-1].print()); - System.out.println("____________________________________________________________"); - } else { - System.out.println("____________________________________________________________"); - lists[counter]= new Task(line); - counter++; - System.out.println("added: " + line); - System.out.println("____________________________________________________________"); } } - System.out.println("____________________________________________________________"); - System.out.println("Bye. Hope to see you again soon!"); - System.out.println("____________________________________________________________"); + printbreak(); + printbye(); + printbreak(); } diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 00000000..9b03d2d3 --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,14 @@ +public class Event extends Task{ + protected String at; + + public Event(String description, String at) { + super(description); + this.at = at; + } + + @Override + public String toString() { + return "[E]" + super.toString() + " (at: " + at + ")"; + } + +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 90276516..1d5b807f 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -22,8 +22,8 @@ public void setDescription(String description) { this.description = description; } - public String print(){ - String print = " [" + getStatusIcon()+"] "+getDescription(); + public String toString(){ + String print = "[" + getStatusIcon()+"] "+getDescription(); return print; } } diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 00000000..60c6d6c2 --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,12 @@ +public class ToDo extends Task{ + + public ToDo(String description) { + super(description); + } + + @Override + public String toString() { + return "[T]" + super.toString(); + } + +} \ No newline at end of file From 3ac81969c5b92a501597a9fd4e7c8ddb65885958 Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Sat, 13 Nov 2021 21:33:16 +0800 Subject: [PATCH 05/10] Level-5 --- src/main/java/CommandNotFoundException.java | 3 + src/main/java/Duke.java | 145 +++++++++++--------- src/main/java/toDoNotFoundException.java | 3 + 3 files changed, 84 insertions(+), 67 deletions(-) create mode 100644 src/main/java/CommandNotFoundException.java create mode 100644 src/main/java/toDoNotFoundException.java diff --git a/src/main/java/CommandNotFoundException.java b/src/main/java/CommandNotFoundException.java new file mode 100644 index 00000000..0b9ebf6f --- /dev/null +++ b/src/main/java/CommandNotFoundException.java @@ -0,0 +1,3 @@ +public class CommandNotFoundException extends Exception{ + //no other code needed +} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 55c2a5ed..2a4ea228 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,19 +1,20 @@ import java.util.Scanner; public class Duke { - public static void printbreak() { + public static void printBreak() { System.out.println("____________________________________________________________"); } - public static void printbye() { + public static void printBye() { System.out.println("Bye. Hope to see you again soon!"); } - public static void printtotaltask(int counter) { + public static void printTotalTask(int counter) { System.out.println("Now you have " + counter + " tasks in the list."); } - public static void main(String[] args) { + + public static void main(String[] args) throws CommandNotFoundException { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" @@ -21,10 +22,10 @@ public static void main(String[] args) { + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - printbreak(); + printBreak(); System.out.println("Hello! I'm Duke "); System.out.println("What can I do for you?"); - printbreak(); + printBreak(); String line; Task[] lists = new Task[100]; int counter = 0; @@ -32,73 +33,83 @@ public static void main(String[] args) { String desc2 = ""; String s1, s2; while (true) { - Scanner in = new Scanner(System.in); line = in.nextLine(); - if (!line.isEmpty()) { - if (line.contains(" ")) { - String[] input = line.split(" ", 2); - desc = input[0]; - desc2 = input[1]; - } else { - desc = line; - } - if (line.equalsIgnoreCase("bye")) - break; - if (line.equalsIgnoreCase("list")) { - printbreak(); - for (int i = 0; i < counter; i++) { - System.out.println(i + 1 + ". " + lists[i].toString()); + try { + if (!line.isEmpty()) { + if (line.contains(" ")) { + String[] input = line.split(" ", 2); + desc = input[0]; + desc2 = input[1]; + } else { + desc = line; } - printbreak(); - } else if (desc.equalsIgnoreCase("done")) { - printbreak(); - int x = Integer.parseInt(desc2); - lists[x - 1].setDone(true); - System.out.println("Nice! I've marked this task as done: "); - System.out.println(lists[x - 1].toString()); - printbreak(); - } else if (desc.equalsIgnoreCase("deadline")) { - printbreak(); - String[] input = desc2.split("/by ", 2); - s1 = input[0]; - s2 = input[1]; - lists[counter] = new Deadline(s1, s2); - System.out.println("Got it. I've added this task: "); - System.out.println(lists[counter].toString()); - counter++; - printtotaltask(counter); - printbreak(); - } else if (desc.equalsIgnoreCase("event")) { - printbreak(); - String[] input = desc2.split("/at ", 2); - s1 = input[0]; - s2 = input[1]; - lists[counter] = new Event(s1, s2); - System.out.println("Got it. I've added this task: "); - System.out.println(lists[counter].toString()); - counter++; - printtotaltask(counter); - printbreak(); - } else { - printbreak(); - if (desc.equalsIgnoreCase("todo")){ + if (line.equalsIgnoreCase("bye")) + break; + if (line.equalsIgnoreCase("list")) { + printBreak(); + for (int i = 0; i < counter; i++) { + System.out.println(i + 1 + ". " + lists[i].toString()); + } + printBreak(); + } else if (desc.equalsIgnoreCase("done")) { + printBreak(); + int x = Integer.parseInt(desc2); + lists[x - 1].setDone(true); + System.out.println("Nice! I've marked this task as done: "); + System.out.println(lists[x - 1].toString()); + printBreak(); + } else if (desc.equalsIgnoreCase("deadline")) { + printBreak(); + String[] input = desc2.split("/by ", 2); + s1 = input[0]; + s2 = input[1]; + lists[counter] = new Deadline(s1, s2); + System.out.println("Got it. I've added this task: "); + System.out.println(lists[counter].toString()); + counter++; + printTotalTask(counter); + printBreak(); + } else if (desc.equalsIgnoreCase("event")) { + printBreak(); + String[] input = desc2.split("/at ", 2); + s1 = input[0]; + s2 = input[1]; + lists[counter] = new Event(s1, s2); + System.out.println("Got it. I've added this task: "); + System.out.println(lists[counter].toString()); + counter++; + printTotalTask(counter); + printBreak(); + } else if (desc.equalsIgnoreCase("todo")) { + if (desc2.isEmpty()){ + throw new toDoNotFoundException(); + } + printBreak(); lists[counter] = new ToDo(desc2); - }else { - lists[counter] = new ToDo(line); + System.out.println("Got it. I've added this task: "); + System.out.println(lists[counter].toString()); + counter++; + printTotalTask(counter); + printBreak(); + } else { + throw new CommandNotFoundException(); } - System.out.println("Got it. I've added this task: "); - System.out.println(lists[counter].toString()); - counter++; - printtotaltask(counter); - printbreak(); } - } + } catch (toDoNotFoundException e ) { + printBreak(); + System.out.println("Sorry, there cannot be any empty todo task."); + printBreak(); + continue; + } catch (CommandNotFoundException e){ + printBreak(); + System.out.println("Sorry, I am unable to handle your request."); + printBreak(); + continue; } - printbreak(); - printbye(); - printbreak(); } - - + printBreak(); + printBye(); + printBreak(); + } } diff --git a/src/main/java/toDoNotFoundException.java b/src/main/java/toDoNotFoundException.java new file mode 100644 index 00000000..ebbbbead --- /dev/null +++ b/src/main/java/toDoNotFoundException.java @@ -0,0 +1,3 @@ +public class toDoNotFoundException extends Exception{ + //no other code needed +} From 9fb69525c3fc1d09732a3c2bbccc5cc9020bf146 Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Sun, 14 Nov 2021 12:16:23 +0800 Subject: [PATCH 06/10] Level - 6 --- src/main/java/Duke.java | 138 ++++++++++++++----- src/main/java/NoListFoundException.java | 3 + src/main/java/deadlineNotFoundException.java | 3 + src/main/java/eventNotFoundException.java | 3 + src/main/java/taskToDo.java | 3 + 5 files changed, 119 insertions(+), 31 deletions(-) create mode 100644 src/main/java/NoListFoundException.java create mode 100644 src/main/java/deadlineNotFoundException.java create mode 100644 src/main/java/eventNotFoundException.java create mode 100644 src/main/java/taskToDo.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 2a4ea228..ccbf3e8b 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,6 +1,17 @@ +import java.util.ArrayList; +import java.util.Iterator; import java.util.Scanner; public class Duke { + + public static String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + + public static String logoMessage = logo + "\nHello! I'm Duke. \nWhat can I do for you?"; + public static void printBreak() { System.out.println("____________________________________________________________"); } @@ -13,22 +24,13 @@ public static void printTotalTask(int counter) { System.out.println("Now you have " + counter + " tasks in the list."); } - - public static void main(String[] args) throws CommandNotFoundException { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - - System.out.println("Hello from\n" + logo); - printBreak(); - System.out.println("Hello! I'm Duke "); - System.out.println("What can I do for you?"); + public static void run(){ + System.out.println(logoMessage); printBreak(); String line; - Task[] lists = new Task[100]; - int counter = 0; + ArrayList taskLists = new ArrayList(); + //Task[] lists = new Task[100]; + //int counter = 0; String desc; String desc2 = ""; String s1, s2; @@ -44,53 +46,96 @@ public static void main(String[] args) throws CommandNotFoundException { } else { desc = line; } + + if (line.equalsIgnoreCase("bye")) break; if (line.equalsIgnoreCase("list")) { printBreak(); - for (int i = 0; i < counter; i++) { - System.out.println(i + 1 + ". " + lists[i].toString()); + if(taskLists.size()==0) { + printTotalTask(taskLists.size()); + System.out.println("The list is empty."); + printBreak(); + continue; + } + //for (int i = 0; i < counter; i++) { + // System.out.println(i + 1 + ". " + lists[i].toString()); + //} + Iterator iterator = taskLists.iterator(); + + while(iterator.hasNext()){ + System.out.println(iterator.next()); } printBreak(); } else if (desc.equalsIgnoreCase("done")) { printBreak(); + if (desc2.isEmpty()|| Integer.parseInt(desc2) > taskLists.size() || Integer.parseInt(desc2) <= 0){ + throw new CommandNotFoundException(); + } int x = Integer.parseInt(desc2); - lists[x - 1].setDone(true); + taskLists.get(x - 1).setDone(true); + //lists[x - 1].setDone(true); System.out.println("Nice! I've marked this task as done: "); - System.out.println(lists[x - 1].toString()); + System.out.println(taskLists.get(x - 1).toString()); + //System.out.println(lists[x - 1].toString()); + printBreak(); + } else if (desc.equalsIgnoreCase("delete")) { + if (taskLists.size()==0){ + throw new NoListFoundException(); + } + if (desc2.isEmpty()|| Integer.parseInt(desc2) > taskLists.size() || Integer.parseInt(desc2) <= 0){ + throw new CommandNotFoundException(); + } + printBreak(); + int x = Integer.parseInt(desc2); + System.out.println("I've deleted this task entered: "); + System.out.println(taskLists.get(x - 1).toString()); + taskLists.remove(x - 1); printBreak(); } else if (desc.equalsIgnoreCase("deadline")) { + if (desc2.isEmpty()){ + throw new deadlineNotFoundException(); + } printBreak(); String[] input = desc2.split("/by ", 2); s1 = input[0]; s2 = input[1]; - lists[counter] = new Deadline(s1, s2); + taskLists.add(new Deadline(s1, s2)); + //lists[counter] = new Deadline(s1, s2); System.out.println("Got it. I've added this task: "); - System.out.println(lists[counter].toString()); - counter++; - printTotalTask(counter); + System.out.println(taskLists.get(taskLists.size()-1).toString()); + //System.out.println(lists[x - 1].toString()); + //counter++; + printTotalTask(taskLists.size()); printBreak(); } else if (desc.equalsIgnoreCase("event")) { + if (desc2.isEmpty()){ + throw new eventNotFoundException(); + } printBreak(); String[] input = desc2.split("/at ", 2); s1 = input[0]; s2 = input[1]; - lists[counter] = new Event(s1, s2); + taskLists.add(new Event(s1,s2)); + //lists[counter] = new Event(s1, s2); System.out.println("Got it. I've added this task: "); - System.out.println(lists[counter].toString()); - counter++; - printTotalTask(counter); + System.out.println(taskLists.get(taskLists.size()-1).toString()); + //System.out.println(lists[counter].toString()); + //counter++; + printTotalTask(taskLists.size()); printBreak(); } else if (desc.equalsIgnoreCase("todo")) { if (desc2.isEmpty()){ throw new toDoNotFoundException(); } printBreak(); - lists[counter] = new ToDo(desc2); + taskLists.add(new ToDo(desc2)); + //lists[counter] = new ToDo(desc2); System.out.println("Got it. I've added this task: "); - System.out.println(lists[counter].toString()); - counter++; - printTotalTask(counter); + System.out.println(taskLists.get(taskLists.size()-1).toString()); + //System.out.println(lists[counter].toString()); + //counter++; + printTotalTask(taskLists.size()); printBreak(); } else { throw new CommandNotFoundException(); @@ -106,10 +151,41 @@ public static void main(String[] args) throws CommandNotFoundException { System.out.println("Sorry, I am unable to handle your request."); printBreak(); continue; + } catch (NoListFoundException e){ + printBreak(); + System.out.println("Sorry, I am unable to delete any task because no task is found."); + printBreak(); + continue; + }catch (deadlineNotFoundException e){ + printBreak(); + System.out.println("Sorry, there cannot be any empty deadline task."); + printBreak(); + continue; + }catch (eventNotFoundException e){ + printBreak(); + System.out.println("Sorry, there cannot be any empty event task."); + printBreak(); + continue; + } } - } printBreak(); printBye(); printBreak(); } + + public static void main(String[] args) throws CommandNotFoundException { + //String logo = " ____ _ \n" + // + "| _ \\ _ _| | _____ \n" + // + "| | | | | | | |/ / _ \\\n" + // + "| |_| | |_| | < __/\n" + // + "|____/ \\__,_|_|\\_\\___|\n"; + // + "|____/ \\__,_|_|\\_\\___|\n"; + + //System.out.println("Hello from\n" + logo); + //printBreak(); + //System.out.println("Hello! I'm Duke "); + //System.out.println("What can I do for you?"); + //printBreak(); + run(); + } } diff --git a/src/main/java/NoListFoundException.java b/src/main/java/NoListFoundException.java new file mode 100644 index 00000000..53dc8a38 --- /dev/null +++ b/src/main/java/NoListFoundException.java @@ -0,0 +1,3 @@ +public class NoListFoundException extends Exception{ + //no other code needed +} diff --git a/src/main/java/deadlineNotFoundException.java b/src/main/java/deadlineNotFoundException.java new file mode 100644 index 00000000..a542a3b9 --- /dev/null +++ b/src/main/java/deadlineNotFoundException.java @@ -0,0 +1,3 @@ +public class deadlineNotFoundException extends Exception{ + //no other code needed +} diff --git a/src/main/java/eventNotFoundException.java b/src/main/java/eventNotFoundException.java new file mode 100644 index 00000000..ba299bbd --- /dev/null +++ b/src/main/java/eventNotFoundException.java @@ -0,0 +1,3 @@ +public class eventNotFoundException extends Exception{ + //no other code needed +} diff --git a/src/main/java/taskToDo.java b/src/main/java/taskToDo.java new file mode 100644 index 00000000..7b54d7c8 --- /dev/null +++ b/src/main/java/taskToDo.java @@ -0,0 +1,3 @@ +public enum taskToDo { + todo,deadline,list,event,done,delete +} From afaabe7395fd2fecdf89b07b863ac1420d1af2c9 Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Sun, 14 Nov 2021 20:08:58 +0800 Subject: [PATCH 07/10] Level 7, More OOP, A-Packages --- README.md | 4 +- src/main/java/CommandNotFoundException.java | 3 - src/main/java/Duke.java | 191 ------------------ src/main/java/Duke/Duke.java | 160 +++++++++++++++ .../CommandNotFoundException.java | 11 + .../Duke/ExceptionList/DukeException.java | 10 + .../ExceptionList/NoListFoundException.java | 12 ++ .../deadlineNotFoundException.java | 11 + .../ExceptionList/eventNotFoundException.java | 10 + .../ExceptionList/toDoNotFoundException.java | 11 + src/main/java/Duke/Storage/Storage.java | 74 +++++++ .../java/{ => Duke/TaskList}/Deadline.java | 6 +- src/main/java/{ => Duke/TaskList}/Event.java | 5 + src/main/java/{ => Duke/TaskList}/Task.java | 17 ++ src/main/java/Duke/TaskList/TaskList.java | 136 +++++++++++++ src/main/java/{ => Duke/TaskList}/ToDo.java | 4 + .../java/{ => Duke/TaskList}/taskToDo.java | 2 + src/main/java/Duke/Ui/Ui.java | 37 ++++ src/main/java/NoListFoundException.java | 3 - src/main/java/deadlineNotFoundException.java | 3 - src/main/java/eventNotFoundException.java | 3 - src/main/java/toDoNotFoundException.java | 3 - text-ui-test/runtest.bat | 2 +- 23 files changed, 508 insertions(+), 210 deletions(-) delete mode 100644 src/main/java/CommandNotFoundException.java delete mode 100644 src/main/java/Duke.java create mode 100644 src/main/java/Duke/Duke.java create mode 100644 src/main/java/Duke/ExceptionList/CommandNotFoundException.java create mode 100644 src/main/java/Duke/ExceptionList/DukeException.java create mode 100644 src/main/java/Duke/ExceptionList/NoListFoundException.java create mode 100644 src/main/java/Duke/ExceptionList/deadlineNotFoundException.java create mode 100644 src/main/java/Duke/ExceptionList/eventNotFoundException.java create mode 100644 src/main/java/Duke/ExceptionList/toDoNotFoundException.java create mode 100644 src/main/java/Duke/Storage/Storage.java rename src/main/java/{ => Duke/TaskList}/Deadline.java (62%) rename src/main/java/{ => Duke/TaskList}/Event.java (71%) rename src/main/java/{ => Duke/TaskList}/Task.java (64%) create mode 100644 src/main/java/Duke/TaskList/TaskList.java rename src/main/java/{ => Duke/TaskList}/ToDo.java (66%) rename src/main/java/{ => Duke/TaskList}/taskToDo.java (73%) create mode 100644 src/main/java/Duke/Ui/Ui.java delete mode 100644 src/main/java/NoListFoundException.java delete mode 100644 src/main/java/deadlineNotFoundException.java delete mode 100644 src/main/java/eventNotFoundException.java delete mode 100644 src/main/java/toDoNotFoundException.java diff --git a/README.md b/README.md index 8715d4d9..63d0ad6b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# Duke.Duke 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/Duke.Duke.java` file, right-click it, and choose `Run Duke.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: ``` Hello from ____ _ diff --git a/src/main/java/CommandNotFoundException.java b/src/main/java/CommandNotFoundException.java deleted file mode 100644 index 0b9ebf6f..00000000 --- a/src/main/java/CommandNotFoundException.java +++ /dev/null @@ -1,3 +0,0 @@ -public class CommandNotFoundException extends Exception{ - //no other code needed -} diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index ccbf3e8b..00000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,191 +0,0 @@ -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Scanner; - -public class Duke { - - public static String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - - public static String logoMessage = logo + "\nHello! I'm Duke. \nWhat can I do for you?"; - - public static void printBreak() { - System.out.println("____________________________________________________________"); - } - - public static void printBye() { - System.out.println("Bye. Hope to see you again soon!"); - } - - public static void printTotalTask(int counter) { - System.out.println("Now you have " + counter + " tasks in the list."); - } - - public static void run(){ - System.out.println(logoMessage); - printBreak(); - String line; - ArrayList taskLists = new ArrayList(); - //Task[] lists = new Task[100]; - //int counter = 0; - String desc; - String desc2 = ""; - String s1, s2; - while (true) { - Scanner in = new Scanner(System.in); - line = in.nextLine(); - try { - if (!line.isEmpty()) { - if (line.contains(" ")) { - String[] input = line.split(" ", 2); - desc = input[0]; - desc2 = input[1]; - } else { - desc = line; - } - - - if (line.equalsIgnoreCase("bye")) - break; - if (line.equalsIgnoreCase("list")) { - printBreak(); - if(taskLists.size()==0) { - printTotalTask(taskLists.size()); - System.out.println("The list is empty."); - printBreak(); - continue; - } - //for (int i = 0; i < counter; i++) { - // System.out.println(i + 1 + ". " + lists[i].toString()); - //} - Iterator iterator = taskLists.iterator(); - - while(iterator.hasNext()){ - System.out.println(iterator.next()); - } - printBreak(); - } else if (desc.equalsIgnoreCase("done")) { - printBreak(); - if (desc2.isEmpty()|| Integer.parseInt(desc2) > taskLists.size() || Integer.parseInt(desc2) <= 0){ - throw new CommandNotFoundException(); - } - int x = Integer.parseInt(desc2); - taskLists.get(x - 1).setDone(true); - //lists[x - 1].setDone(true); - System.out.println("Nice! I've marked this task as done: "); - System.out.println(taskLists.get(x - 1).toString()); - //System.out.println(lists[x - 1].toString()); - printBreak(); - } else if (desc.equalsIgnoreCase("delete")) { - if (taskLists.size()==0){ - throw new NoListFoundException(); - } - if (desc2.isEmpty()|| Integer.parseInt(desc2) > taskLists.size() || Integer.parseInt(desc2) <= 0){ - throw new CommandNotFoundException(); - } - printBreak(); - int x = Integer.parseInt(desc2); - System.out.println("I've deleted this task entered: "); - System.out.println(taskLists.get(x - 1).toString()); - taskLists.remove(x - 1); - printBreak(); - } else if (desc.equalsIgnoreCase("deadline")) { - if (desc2.isEmpty()){ - throw new deadlineNotFoundException(); - } - printBreak(); - String[] input = desc2.split("/by ", 2); - s1 = input[0]; - s2 = input[1]; - taskLists.add(new Deadline(s1, s2)); - //lists[counter] = new Deadline(s1, s2); - System.out.println("Got it. I've added this task: "); - System.out.println(taskLists.get(taskLists.size()-1).toString()); - //System.out.println(lists[x - 1].toString()); - //counter++; - printTotalTask(taskLists.size()); - printBreak(); - } else if (desc.equalsIgnoreCase("event")) { - if (desc2.isEmpty()){ - throw new eventNotFoundException(); - } - printBreak(); - String[] input = desc2.split("/at ", 2); - s1 = input[0]; - s2 = input[1]; - taskLists.add(new Event(s1,s2)); - //lists[counter] = new Event(s1, s2); - System.out.println("Got it. I've added this task: "); - System.out.println(taskLists.get(taskLists.size()-1).toString()); - //System.out.println(lists[counter].toString()); - //counter++; - printTotalTask(taskLists.size()); - printBreak(); - } else if (desc.equalsIgnoreCase("todo")) { - if (desc2.isEmpty()){ - throw new toDoNotFoundException(); - } - printBreak(); - taskLists.add(new ToDo(desc2)); - //lists[counter] = new ToDo(desc2); - System.out.println("Got it. I've added this task: "); - System.out.println(taskLists.get(taskLists.size()-1).toString()); - //System.out.println(lists[counter].toString()); - //counter++; - printTotalTask(taskLists.size()); - printBreak(); - } else { - throw new CommandNotFoundException(); - } - } - } catch (toDoNotFoundException e ) { - printBreak(); - System.out.println("Sorry, there cannot be any empty todo task."); - printBreak(); - continue; - } catch (CommandNotFoundException e){ - printBreak(); - System.out.println("Sorry, I am unable to handle your request."); - printBreak(); - continue; - } catch (NoListFoundException e){ - printBreak(); - System.out.println("Sorry, I am unable to delete any task because no task is found."); - printBreak(); - continue; - }catch (deadlineNotFoundException e){ - printBreak(); - System.out.println("Sorry, there cannot be any empty deadline task."); - printBreak(); - continue; - }catch (eventNotFoundException e){ - printBreak(); - System.out.println("Sorry, there cannot be any empty event task."); - printBreak(); - continue; - } - } - printBreak(); - printBye(); - printBreak(); - } - - public static void main(String[] args) throws CommandNotFoundException { - //String logo = " ____ _ \n" - // + "| _ \\ _ _| | _____ \n" - // + "| | | | | | | |/ / _ \\\n" - // + "| |_| | |_| | < __/\n" - // + "|____/ \\__,_|_|\\_\\___|\n"; - // + "|____/ \\__,_|_|\\_\\___|\n"; - - //System.out.println("Hello from\n" + logo); - //printBreak(); - //System.out.println("Hello! I'm Duke "); - //System.out.println("What can I do for you?"); - //printBreak(); - run(); - } -} diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java new file mode 100644 index 00000000..ed198f96 --- /dev/null +++ b/src/main/java/Duke/Duke.java @@ -0,0 +1,160 @@ +package Duke; + +import Duke.ExceptionList.*; +import Duke.Storage.Storage; +import Duke.TaskList.*; +import Duke.Ui.Ui; + +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Scanner; +import java.io.FileWriter; // Import the FileWriter class +import java.io.IOException; // Import the IOException class to handle errors +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +public class Duke { + + private Storage storage; + private TaskList tl; + private Ui ui; + public static final String DEFAULT_STORAGE_FILEPATH = "duke.txt"; + + public Duke(){ + run(); + } + + + public void run() { + ui = new Ui(); + ui.showStartMessage(); + ui.printBreak(); + String filePath = ""; + //TaskList tl = new TaskList(); + storage = new Storage(); + try { + tl = new TaskList(storage.load()); + //if (filePath.isEmpty()){ + // throw new DukeException(); + //} + } + catch (DukeException | IOException e) { + ui.showLoadingError(); + tl = new TaskList(); + } + + String line; + String desc; + String desc2 = ""; + String s1, s2; + while (true) { + Scanner in = new Scanner(System.in); + line = in.nextLine(); + try { + if (!line.isEmpty()) { + desc = ""; + desc2 = ""; + if (line.contains(" ")) { + + String[] input = line.split(" ", 2); + desc = input[0]; + desc2 = input[1]; + } else { + desc = line; + } + if (line.equalsIgnoreCase("bye")) { + storage.saveTask(tl); + break; + } + if (line.equalsIgnoreCase("list")) { + ui.printBreak(); + tl.printTaskList(); + ui.printBreak(); + } else if (desc.equalsIgnoreCase("done")) { + ui.printBreak(); + if (desc2.isEmpty()){ + throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + }else if (Integer.parseInt(desc2) > tl.getSize() || Integer.parseInt(desc2) <= 0){ + throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + } + int x = Integer.parseInt(desc2); + tl.setDoneStatus(x); + ui.printBreak(); + } else if (desc.equalsIgnoreCase("delete")) { + if (tl.getSize()==0){ + throw new NoListFoundException("Sorry, I am unable to delete any task because no task is found."); + } + if (desc2.isEmpty()){ + throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + }else if (Integer.parseInt(desc2) > tl.getSize() || Integer.parseInt(desc2) <= 0){ + + } + ui.printBreak(); + int x = Integer.parseInt(desc2); + System.out.println("I've deleted this task entered: "); + tl.deleteTask(x); + ui.printBreak(); + } else if (desc.equalsIgnoreCase("deadline")) { + if (desc2.isEmpty()){ + throw new deadlineNotFoundException("Sorry, there cannot be any empty deadline task."); + } + ui.printBreak(); + tl.addTask(desc,desc2,false); + System.out.println("Got it. I've added this task: "); + System.out.println(tl.getLastAddedTask()); + ui.printTotalTask(tl.getSize()); + ui.printBreak(); + } else if (desc.equalsIgnoreCase("event")) { + if (desc2.isEmpty()){ + throw new eventNotFoundException("Sorry, there cannot be any empty event task."); + } + ui.printBreak(); + tl.addTask(desc,desc2,false); + System.out.println("Got it. I've added this task: "); + System.out.println(tl.getLastAddedTask()); + ui.printTotalTask(tl.getSize()); + ui.printBreak(); + } else if (desc.equalsIgnoreCase("todo")) { + if (desc2.isEmpty()){ + throw new toDoNotFoundException("Sorry, there cannot be any empty todo task."); + } + ui.printBreak(); + tl.addTask(desc,desc2,false); + System.out.println("Got it. I've added this task: "); + System.out.println(tl.getLastAddedTask()); + ui.printTotalTask(tl.getSize()); + ui.printBreak(); + } else { + throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + } + } + } catch (toDoNotFoundException | CommandNotFoundException | NoListFoundException | deadlineNotFoundException | eventNotFoundException e ) { + continue; + } + } + ui.printBreak(); + ui.printBye(); + ui.printBreak(); + } + + public static void main(String[] args) { + //String logo = " ____ _ \n" + // + "| _ \\ _ _| | _____ \n" + // + "| | | | | | | |/ / _ \\\n" + // + "| |_| | |_| | < __/\n" + // + "|____/ \\__,_|_|\\_\\___|\n"; + // + "|____/ \\__,_|_|\\_\\___|\n"; + + //System.out.println("Hello from\n" + logo); + //printBreak(); + //System.out.println("Hello! I'm Duke.Duke "); + //System.out.println("What can I do for you?"); + //printBreak(); + Duke d = new Duke(); + } +} diff --git a/src/main/java/Duke/ExceptionList/CommandNotFoundException.java b/src/main/java/Duke/ExceptionList/CommandNotFoundException.java new file mode 100644 index 00000000..7d4eb33a --- /dev/null +++ b/src/main/java/Duke/ExceptionList/CommandNotFoundException.java @@ -0,0 +1,11 @@ +package Duke.ExceptionList; + +public class CommandNotFoundException extends Exception{ + //no other code needed + public CommandNotFoundException(String message) { + super(message); + } + public CommandNotFoundException(){ + super(); + } +} diff --git a/src/main/java/Duke/ExceptionList/DukeException.java b/src/main/java/Duke/ExceptionList/DukeException.java new file mode 100644 index 00000000..09a0d5b1 --- /dev/null +++ b/src/main/java/Duke/ExceptionList/DukeException.java @@ -0,0 +1,10 @@ +package Duke.ExceptionList; + +public class DukeException extends Exception { + public DukeException(String message) { + super(message); + } + public DukeException(){ + super(); + } +} diff --git a/src/main/java/Duke/ExceptionList/NoListFoundException.java b/src/main/java/Duke/ExceptionList/NoListFoundException.java new file mode 100644 index 00000000..1d4be88d --- /dev/null +++ b/src/main/java/Duke/ExceptionList/NoListFoundException.java @@ -0,0 +1,12 @@ +package Duke.ExceptionList; + +public class NoListFoundException extends Exception { + //no other code needed + public NoListFoundException(String message) { + super(message); + } + + public NoListFoundException() { + super(); + } +} diff --git a/src/main/java/Duke/ExceptionList/deadlineNotFoundException.java b/src/main/java/Duke/ExceptionList/deadlineNotFoundException.java new file mode 100644 index 00000000..9cab566c --- /dev/null +++ b/src/main/java/Duke/ExceptionList/deadlineNotFoundException.java @@ -0,0 +1,11 @@ +package Duke.ExceptionList; + +public class deadlineNotFoundException extends Exception{ + //no other code needed + public deadlineNotFoundException(String message) { + super(message); + } + public deadlineNotFoundException(){ + super(); + } +} diff --git a/src/main/java/Duke/ExceptionList/eventNotFoundException.java b/src/main/java/Duke/ExceptionList/eventNotFoundException.java new file mode 100644 index 00000000..baf9a3fe --- /dev/null +++ b/src/main/java/Duke/ExceptionList/eventNotFoundException.java @@ -0,0 +1,10 @@ +package Duke.ExceptionList; + +public class eventNotFoundException extends Exception{ + public eventNotFoundException(String message) { + super(message); + } + public eventNotFoundException(){ + super(); + } +} diff --git a/src/main/java/Duke/ExceptionList/toDoNotFoundException.java b/src/main/java/Duke/ExceptionList/toDoNotFoundException.java new file mode 100644 index 00000000..73cc06ee --- /dev/null +++ b/src/main/java/Duke/ExceptionList/toDoNotFoundException.java @@ -0,0 +1,11 @@ +package Duke.ExceptionList; + +public class toDoNotFoundException extends Exception{ + //no other code needed + public toDoNotFoundException(String message) { + super(message); + } + public toDoNotFoundException(){ + super(); + } +} diff --git a/src/main/java/Duke/Storage/Storage.java b/src/main/java/Duke/Storage/Storage.java new file mode 100644 index 00000000..8439cbf8 --- /dev/null +++ b/src/main/java/Duke/Storage/Storage.java @@ -0,0 +1,74 @@ +package Duke.Storage; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import Duke.TaskList.*; +import java.util.ArrayList; + +public class Storage { + protected static final String DEFAULT_STORAGE_FILEPATH = "duke.txt"; + protected static final String home = System.getProperty("user.home"); + private static String filePath = DEFAULT_STORAGE_FILEPATH; + protected Path path; + + public Storage(){ + filePath = DEFAULT_STORAGE_FILEPATH; + path = Paths.get(home, filePath); + } + public Storage(String filepath) throws IOException { + this.filePath = filePath; + path = Paths.get(home, filePath); + if (!isValidPath(path)) { + //throw new InvalidStorageFilePathException("Storage file should end with '.txt'"); + } + //check if file exist + isPathExist(); + } + + private static boolean isValidPath(Path filePath) { + return filePath.toString().endsWith(".txt"); + } + + private void isPathExist() throws IOException { + Path path = Paths.get(home, filePath); + // check if the directory and file exists, else create + if (!Files.exists(path)) { + Files.createDirectory(Paths.get(home,"data")); + Files.createFile(path); + } + } + public void saveTask(TaskList tasks){ + path = Paths.get(home, "data",DEFAULT_STORAGE_FILEPATH); + + // start saving task + String content = ""; + ArrayList save = tasks.getTaskList(); + for (Task task : save) + content += task.saveTask() + System.lineSeparator(); + //System.out.println(content); + try{ + writeToFile(path+"",content); + }catch(IOException e){ + + } + } + + private void writeToFile(String filePath, String text) throws IOException { + FileWriter fw = new FileWriter(filePath); + fw.write(text); + fw.close(); + } + + public String load() throws IOException { + String path = home+"\\data\\"+filePath; + //return (File) Files.readAllLines(Path.of(path)); + return path; // create a File for the given file path + } +} diff --git a/src/main/java/Deadline.java b/src/main/java/Duke/TaskList/Deadline.java similarity index 62% rename from src/main/java/Deadline.java rename to src/main/java/Duke/TaskList/Deadline.java index 106b5f6f..4eb36ef1 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Duke/TaskList/Deadline.java @@ -1,4 +1,6 @@ -public class Deadline extends Task{ +package Duke.TaskList; + +public class Deadline extends Task { protected String by; public Deadline(String description, String by) { @@ -11,4 +13,6 @@ public String toString() { return "[D]" + super.toString() + " (by: " + by + ")"; } + @Override + public String saveTask(){ return "D|" + super.saveTask() + "|" + by;} } diff --git a/src/main/java/Event.java b/src/main/java/Duke/TaskList/Event.java similarity index 71% rename from src/main/java/Event.java rename to src/main/java/Duke/TaskList/Event.java index 9b03d2d3..75945739 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Duke/TaskList/Event.java @@ -1,3 +1,5 @@ +package Duke.TaskList; + public class Event extends Task{ protected String at; @@ -11,4 +13,7 @@ public String toString() { return "[E]" + super.toString() + " (at: " + at + ")"; } + @Override + public String saveTask(){ return "E|" + super.saveTask() + "|" + at;} + } \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Duke/TaskList/Task.java similarity index 64% rename from src/main/java/Task.java rename to src/main/java/Duke/TaskList/Task.java index 1d5b807f..36f1ac6b 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Duke/TaskList/Task.java @@ -1,3 +1,5 @@ +package Duke.TaskList; + public class Task { protected String description; protected boolean isDone; @@ -6,10 +8,19 @@ public Task(String description) { this.description = description; this.isDone = false; } + public Task(String description,boolean isDone){ + this.description = description; + this.isDone = isDone; + } public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + + public String getStatusDone(){ + return (isDone ? "1" : "0"); // mark done task with 1 + } + public void setDone(boolean done){ this.isDone = done; } @@ -26,4 +37,10 @@ public String toString(){ String print = "[" + getStatusIcon()+"] "+getDescription(); return print; } + + public String saveTask(){ + String save = getStatusDone() + "|" + getDescription(); + return save; + } + } diff --git a/src/main/java/Duke/TaskList/TaskList.java b/src/main/java/Duke/TaskList/TaskList.java new file mode 100644 index 00000000..8cabe1bc --- /dev/null +++ b/src/main/java/Duke/TaskList/TaskList.java @@ -0,0 +1,136 @@ +package Duke.TaskList; + +import Duke.ExceptionList.*; + +import java.io.*; +import java.util.ArrayList; +import java.util.Scanner; + + +public class TaskList { + ArrayList taskLists = new ArrayList(); + + static int counter; + + public TaskList() { + + } + + public TaskList(ArrayList taskListsLoad) { + + //taskLists.add(); + + } + + public TaskList(String f) throws DukeException { + //parse the String from the text file then use it to inside into the DB + try (BufferedReader br = new BufferedReader(new FileReader(f))) { + String line; + while ((line = br.readLine()) != null) { + // process the line. + addFileTask(line); + } + } catch (FileNotFoundException e) { + throw new DukeException("File is not found. Please, make sure you entered the correct path."); + } catch (toDoNotFoundException | eventNotFoundException | deadlineNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void addFileTask(String str) throws toDoNotFoundException, eventNotFoundException, deadlineNotFoundException { + String[] input = str.split("|", 2); + String s1 = input[0]; + String s2 = input[1]; + addTask(s1, s2, true); + } + + public void addTask(String cmd, String description, boolean file) throws toDoNotFoundException, eventNotFoundException, deadlineNotFoundException { + if (cmd.equalsIgnoreCase("todo") || cmd.equalsIgnoreCase("T")) { + if (description.isEmpty()) { + throw new toDoNotFoundException("Sorry, there cannot be any empty todo task."); + } + if (file) { + String[] input = description.split("\\|", 3); + String s1 = input[1]; + String s2 = input[2]; + taskLists.add(new ToDo(s2)); + if (s1.equals("1")) { + taskLists.get(taskLists.size() - 1).setDone(true); + } + } else taskLists.add(new ToDo(description)); + + } else if (cmd.equalsIgnoreCase("deadline") || cmd.equalsIgnoreCase("D")) { + if (description.isEmpty()) { + throw new deadlineNotFoundException("Sorry, there cannot be any empty deadline task."); + } + if (file) { + String[] input = description.split("\\|", 4); + String s1 = input[1]; + String s2 = input[2]; + String s3 = input[3]; + taskLists.add(new Deadline(s2, s3)); + if (s1.equals("1")) { + taskLists.get(taskLists.size() - 1).setDone(true); + } + } else { + String[] input = description.split("/by ", 2); + String s1 = input[0]; + String s2 = input[1]; + taskLists.add(new Deadline(s1, s2)); + } + + } else if (cmd.equalsIgnoreCase("event") || cmd.equalsIgnoreCase("E")) { + if (description.isEmpty()) { + throw new eventNotFoundException("Sorry, there cannot be any empty event task."); + } + if (file) { + String[] input = description.split("\\|", 4); + String s1 = input[1]; + String s2 = input[2]; + String s3 = input[3]; + + taskLists.add(new Event(s2, s3)); + if (s1.equals("1")) { + taskLists.get(taskLists.size() - 1).setDone(true); + } + } else { + String[] input = description.split("/at ", 2); + String s1 = input[0]; + String s2 = input[1]; + taskLists.add(new Event(s1, s2)); + } + } + } + + public String getLastAddedTask() { + return taskLists.get(taskLists.size() - 1).toString(); + } + + public void deleteTask(int x) { + System.out.println(taskLists.get(x - 1).toString()); + taskLists.remove(x - 1); + } + + public void printTaskList() { + System.out.println("Here are the tasks in your list: "); + for (int i = 0; i < taskLists.size(); i++) { + System.out.println(taskLists.get(i).toString()); + } + } + + public void setDoneStatus(int x) { + taskLists.get(x - 1).setDone(true); + System.out.println("Nice! I've marked this task as done: "); + System.out.println(taskLists.get(x - 1).toString()); + } + + public int getSize() { + return taskLists.size(); + } + + public ArrayList getTaskList() { + return taskLists; + } +} diff --git a/src/main/java/ToDo.java b/src/main/java/Duke/TaskList/ToDo.java similarity index 66% rename from src/main/java/ToDo.java rename to src/main/java/Duke/TaskList/ToDo.java index 60c6d6c2..afdfe427 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/Duke/TaskList/ToDo.java @@ -1,3 +1,5 @@ +package Duke.TaskList; + public class ToDo extends Task{ public ToDo(String description) { @@ -9,4 +11,6 @@ public String toString() { return "[T]" + super.toString(); } + @Override + public String saveTask(){ return "T|" + super.saveTask();} } \ No newline at end of file diff --git a/src/main/java/taskToDo.java b/src/main/java/Duke/TaskList/taskToDo.java similarity index 73% rename from src/main/java/taskToDo.java rename to src/main/java/Duke/TaskList/taskToDo.java index 7b54d7c8..39b9fa82 100644 --- a/src/main/java/taskToDo.java +++ b/src/main/java/Duke/TaskList/taskToDo.java @@ -1,3 +1,5 @@ +package Duke.TaskList; + public enum taskToDo { todo,deadline,list,event,done,delete } diff --git a/src/main/java/Duke/Ui/Ui.java b/src/main/java/Duke/Ui/Ui.java new file mode 100644 index 00000000..99321116 --- /dev/null +++ b/src/main/java/Duke/Ui/Ui.java @@ -0,0 +1,37 @@ +package Duke.Ui; + +public class Ui { + private String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + + private String logoMessage = logo + "\nHello! I'm Duke.Duke. \nWhat can I do for you?"; + + private String breaklines = "____________________________________________________________"; + + public void printBreak() { + System.out.println(breaklines); + } + + public void printBye() { + System.out.println("Bye. Hope to see you again soon!"); + } + + public void printTotalTask(int counter) { + System.out.println("Now you have " + counter + " tasks in the list."); + } + + private String print(){ + return logoMessage; + } + + public void showStartMessage(){ + System.out.println(print()); + } + + public void showLoadingError() { + System.out.println("Sorry file is not found. We will save a new copy."); + } +} diff --git a/src/main/java/NoListFoundException.java b/src/main/java/NoListFoundException.java deleted file mode 100644 index 53dc8a38..00000000 --- a/src/main/java/NoListFoundException.java +++ /dev/null @@ -1,3 +0,0 @@ -public class NoListFoundException extends Exception{ - //no other code needed -} diff --git a/src/main/java/deadlineNotFoundException.java b/src/main/java/deadlineNotFoundException.java deleted file mode 100644 index a542a3b9..00000000 --- a/src/main/java/deadlineNotFoundException.java +++ /dev/null @@ -1,3 +0,0 @@ -public class deadlineNotFoundException extends Exception{ - //no other code needed -} diff --git a/src/main/java/eventNotFoundException.java b/src/main/java/eventNotFoundException.java deleted file mode 100644 index ba299bbd..00000000 --- a/src/main/java/eventNotFoundException.java +++ /dev/null @@ -1,3 +0,0 @@ -public class eventNotFoundException extends Exception{ - //no other code needed -} diff --git a/src/main/java/toDoNotFoundException.java b/src/main/java/toDoNotFoundException.java deleted file mode 100644 index ebbbbead..00000000 --- a/src/main/java/toDoNotFoundException.java +++ /dev/null @@ -1,3 +0,0 @@ -public class toDoNotFoundException extends Exception{ - //no other code needed -} diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 08737446..cecfdb11 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 Duke.Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From a7d1083432f1d729951c1b48b5c6eba37fba4386 Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Sun, 14 Nov 2021 20:25:56 +0800 Subject: [PATCH 08/10] Cleaning up codes. --- src/main/java/Duke/Duke.java | 39 +++--------------------------- src/main/java/META-INF/MANIFEST.MF | 3 +++ 2 files changed, 6 insertions(+), 36 deletions(-) create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index ed198f96..c9530e92 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -5,25 +5,14 @@ import Duke.TaskList.*; import Duke.Ui.Ui; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Iterator; import java.util.Scanner; -import java.io.FileWriter; // Import the FileWriter class import java.io.IOException; // Import the IOException class to handle errors -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; public class Duke { private Storage storage; private TaskList tl; private Ui ui; - public static final String DEFAULT_STORAGE_FILEPATH = "duke.txt"; public Duke(){ run(); @@ -34,30 +23,20 @@ public void run() { ui = new Ui(); ui.showStartMessage(); ui.printBreak(); - String filePath = ""; - //TaskList tl = new TaskList(); storage = new Storage(); try { tl = new TaskList(storage.load()); - //if (filePath.isEmpty()){ - // throw new DukeException(); - //} } catch (DukeException | IOException e) { ui.showLoadingError(); tl = new TaskList(); } - - String line; - String desc; - String desc2 = ""; - String s1, s2; + String line, desc, desc2, s1, s2; while (true) { Scanner in = new Scanner(System.in); line = in.nextLine(); try { if (!line.isEmpty()) { - desc = ""; desc2 = ""; if (line.contains(" ")) { @@ -92,7 +71,7 @@ public void run() { if (desc2.isEmpty()){ throw new CommandNotFoundException("Sorry, I am unable to handle your request."); }else if (Integer.parseInt(desc2) > tl.getSize() || Integer.parseInt(desc2) <= 0){ - + throw new CommandNotFoundException("Sorry, I am unable to handle your request."); } ui.printBreak(); int x = Integer.parseInt(desc2); @@ -143,18 +122,6 @@ public void run() { } public static void main(String[] args) { - //String logo = " ____ _ \n" - // + "| _ \\ _ _| | _____ \n" - // + "| | | | | | | |/ / _ \\\n" - // + "| |_| | |_| | < __/\n" - // + "|____/ \\__,_|_|\\_\\___|\n"; - // + "|____/ \\__,_|_|\\_\\___|\n"; - - //System.out.println("Hello from\n" + logo); - //printBreak(); - //System.out.println("Hello! I'm Duke.Duke "); - //System.out.println("What can I do for you?"); - //printBreak(); - Duke d = new Duke(); + new Duke(); } } diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 00000000..5699c741 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Duke.Duke + From ce07b14044e4495e7979ab1a6686989503e4fa2d Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Sun, 14 Nov 2021 22:14:56 +0800 Subject: [PATCH 09/10] Minor updates. --- src/main/java/Duke/Duke.java | 26 ++++++++--- src/main/java/Duke/Storage/Storage.java | 11 ++--- src/main/java/Duke/TaskList/Task.java | 2 +- src/main/java/Duke/TaskList/TaskList.java | 55 +++++++++++------------ src/main/java/Duke/Ui/Ui.java | 21 +++++---- 5 files changed, 63 insertions(+), 52 deletions(-) diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index c9530e92..3ae813b7 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -7,6 +7,7 @@ import java.util.Scanner; import java.io.IOException; // Import the IOException class to handle errors +import java.util.regex.Pattern; public class Duke { @@ -18,6 +19,14 @@ public Duke(){ run(); } + private Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?"); + + public boolean isNumeric(String strNum) { + if (strNum == null) { + return false; + } + return pattern.matcher(strNum).matches(); + } public void run() { ui = new Ui(); @@ -55,12 +64,12 @@ public void run() { tl.printTaskList(); ui.printBreak(); } else if (desc.equalsIgnoreCase("done")) { - ui.printBreak(); - if (desc2.isEmpty()){ - throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + if (desc2.isEmpty() || !isNumeric(desc2)){ + throw new CommandNotFoundException("Please enter a number."); }else if (Integer.parseInt(desc2) > tl.getSize() || Integer.parseInt(desc2) <= 0){ - throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + throw new CommandNotFoundException("Sorry, Please enter a legit number."); } + ui.printBreak(); int x = Integer.parseInt(desc2); tl.setDoneStatus(x); ui.printBreak(); @@ -68,10 +77,10 @@ public void run() { if (tl.getSize()==0){ throw new NoListFoundException("Sorry, I am unable to delete any task because no task is found."); } - if (desc2.isEmpty()){ - throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + if (desc2.isEmpty()|| !isNumeric(desc2)){ + throw new CommandNotFoundException("Please enter a number."); }else if (Integer.parseInt(desc2) > tl.getSize() || Integer.parseInt(desc2) <= 0){ - throw new CommandNotFoundException("Sorry, I am unable to handle your request."); + throw new CommandNotFoundException("Sorry, Please enter a legit number."); } ui.printBreak(); int x = Integer.parseInt(desc2); @@ -113,6 +122,9 @@ public void run() { } } } catch (toDoNotFoundException | CommandNotFoundException | NoListFoundException | deadlineNotFoundException | eventNotFoundException e ) { + ui.printBreak(); + System.out.println(e.getMessage()); + ui.printBreak(); continue; } } diff --git a/src/main/java/Duke/Storage/Storage.java b/src/main/java/Duke/Storage/Storage.java index 8439cbf8..88acad8b 100644 --- a/src/main/java/Duke/Storage/Storage.java +++ b/src/main/java/Duke/Storage/Storage.java @@ -1,21 +1,18 @@ package Duke.Storage; -import java.io.File; -import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; import Duke.TaskList.*; import java.util.ArrayList; public class Storage { - protected static final String DEFAULT_STORAGE_FILEPATH = "duke.txt"; - protected static final String home = System.getProperty("user.home"); - private static String filePath = DEFAULT_STORAGE_FILEPATH; + protected final String DEFAULT_STORAGE_FILEPATH = "duke.txt"; + protected final String home = System.getProperty("user.home"); + private String filePath = DEFAULT_STORAGE_FILEPATH; protected Path path; public Storage(){ @@ -56,7 +53,7 @@ public void saveTask(TaskList tasks){ try{ writeToFile(path+"",content); }catch(IOException e){ - + e.printStackTrace(); } } diff --git a/src/main/java/Duke/TaskList/Task.java b/src/main/java/Duke/TaskList/Task.java index 36f1ac6b..01fa4660 100644 --- a/src/main/java/Duke/TaskList/Task.java +++ b/src/main/java/Duke/TaskList/Task.java @@ -34,7 +34,7 @@ public void setDescription(String description) { } public String toString(){ - String print = "[" + getStatusIcon()+"] "+getDescription(); + String print = "[" + getStatusIcon()+"] " + getDescription(); return print; } diff --git a/src/main/java/Duke/TaskList/TaskList.java b/src/main/java/Duke/TaskList/TaskList.java index 8cabe1bc..099e6cb2 100644 --- a/src/main/java/Duke/TaskList/TaskList.java +++ b/src/main/java/Duke/TaskList/TaskList.java @@ -4,24 +4,12 @@ import java.io.*; import java.util.ArrayList; -import java.util.Scanner; public class TaskList { ArrayList taskLists = new ArrayList(); - - static int counter; - public TaskList() { - - } - - public TaskList(ArrayList taskListsLoad) { - - //taskLists.add(); - } - public TaskList(String f) throws DukeException { //parse the String from the text file then use it to inside into the DB try (BufferedReader br = new BufferedReader(new FileReader(f))) { @@ -32,29 +20,35 @@ public TaskList(String f) throws DukeException { } } catch (FileNotFoundException e) { throw new DukeException("File is not found. Please, make sure you entered the correct path."); - } catch (toDoNotFoundException | eventNotFoundException | deadlineNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (toDoNotFoundException | eventNotFoundException | deadlineNotFoundException |IOException e) { e.printStackTrace(); } } public void addFileTask(String str) throws toDoNotFoundException, eventNotFoundException, deadlineNotFoundException { - String[] input = str.split("|", 2); + String[] input = str.split("\\|", 2); String s1 = input[0]; String s2 = input[1]; addTask(s1, s2, true); } + /** + * Adds task into the ArrayList of Tasks object. The cmd has been passed through validation thus unwanted commands + * will not be written into the list. + * + * @param cmd the given command by the user + * @param description details on the task listed by user + * @param file checks if it is from a loaded file or an input string + * */ public void addTask(String cmd, String description, boolean file) throws toDoNotFoundException, eventNotFoundException, deadlineNotFoundException { if (cmd.equalsIgnoreCase("todo") || cmd.equalsIgnoreCase("T")) { if (description.isEmpty()) { throw new toDoNotFoundException("Sorry, there cannot be any empty todo task."); } if (file) { - String[] input = description.split("\\|", 3); - String s1 = input[1]; - String s2 = input[2]; + String[] input = description.split("\\|", 2); + String s1 = input[0]; + String s2 = input[1]; taskLists.add(new ToDo(s2)); if (s1.equals("1")) { taskLists.get(taskLists.size() - 1).setDone(true); @@ -66,10 +60,10 @@ public void addTask(String cmd, String description, boolean file) throws toDoNot throw new deadlineNotFoundException("Sorry, there cannot be any empty deadline task."); } if (file) { - String[] input = description.split("\\|", 4); - String s1 = input[1]; - String s2 = input[2]; - String s3 = input[3]; + String[] input = description.split("\\|", 3); + String s1 = input[0]; + String s2 = input[1]; + String s3 = input[2]; taskLists.add(new Deadline(s2, s3)); if (s1.equals("1")) { taskLists.get(taskLists.size() - 1).setDone(true); @@ -86,11 +80,10 @@ public void addTask(String cmd, String description, boolean file) throws toDoNot throw new eventNotFoundException("Sorry, there cannot be any empty event task."); } if (file) { - String[] input = description.split("\\|", 4); - String s1 = input[1]; - String s2 = input[2]; - String s3 = input[3]; - + String[] input = description.split("\\|", 3); + String s1 = input[0]; + String s2 = input[1]; + String s3 = input[2]; taskLists.add(new Event(s2, s3)); if (s1.equals("1")) { taskLists.get(taskLists.size() - 1).setDone(true); @@ -107,7 +100,11 @@ public void addTask(String cmd, String description, boolean file) throws toDoNot public String getLastAddedTask() { return taskLists.get(taskLists.size() - 1).toString(); } - + /** + * User input will perform a deleting of the specific task from the ArrayList of Tasks object. + * + * @param x the given position of the list given by the user + * */ public void deleteTask(int x) { System.out.println(taskLists.get(x - 1).toString()); taskLists.remove(x - 1); diff --git a/src/main/java/Duke/Ui/Ui.java b/src/main/java/Duke/Ui/Ui.java index 99321116..c480bcdb 100644 --- a/src/main/java/Duke/Ui/Ui.java +++ b/src/main/java/Duke/Ui/Ui.java @@ -1,36 +1,41 @@ package Duke.Ui; public class Ui { - private String logo = " ____ _ \n" + private final String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - private String logoMessage = logo + "\nHello! I'm Duke.Duke. \nWhat can I do for you?"; + private final String logoMessage = logo + "\nHello! I'm Duke.Duke. \nWhat can I do for you?"; - private String breaklines = "____________________________________________________________"; + private final String breakLines = "____________________________________________________________"; + private String print(){ + return logoMessage; + } + + //Printing divider public void printBreak() { - System.out.println(breaklines); + System.out.println(breakLines); } + //Printing goodbye message public void printBye() { System.out.println("Bye. Hope to see you again soon!"); } + //Printing the count of tasks in the list. public void printTotalTask(int counter) { System.out.println("Now you have " + counter + " tasks in the list."); } - private String print(){ - return logoMessage; - } - + //Printing start message public void showStartMessage(){ System.out.println(print()); } + //Printing Loading Error message public void showLoadingError() { System.out.println("Sorry file is not found. We will save a new copy."); } From d4f8df552a091da804710fb883c27c1749124c3d Mon Sep 17 00:00:00 2001 From: "DESKTOP-26G0JMD\\Soon Boon" Date: Sun, 14 Nov 2021 22:26:26 +0800 Subject: [PATCH 10/10] A-JavaDoc --- src/main/java/Duke/Storage/Storage.java | 16 ++++++++++++---- src/main/java/Duke/TaskList/Task.java | 12 ++++++++++-- src/main/java/Duke/Ui/Ui.java | 6 +++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main/java/Duke/Storage/Storage.java b/src/main/java/Duke/Storage/Storage.java index 88acad8b..5395bd7d 100644 --- a/src/main/java/Duke/Storage/Storage.java +++ b/src/main/java/Duke/Storage/Storage.java @@ -22,9 +22,7 @@ public Storage(){ public Storage(String filepath) throws IOException { this.filePath = filePath; path = Paths.get(home, filePath); - if (!isValidPath(path)) { - //throw new InvalidStorageFilePathException("Storage file should end with '.txt'"); - } + if (!isValidPath(path)) {} //check if file exist isPathExist(); } @@ -41,6 +39,11 @@ private void isPathExist() throws IOException { Files.createFile(path); } } + /** + * This method save all the tasks in the ArrayList of TaskLists. + * + * @param tasks ArrayList of Task in TaskLists. + * */ public void saveTask(TaskList tasks){ path = Paths.get(home, "data",DEFAULT_STORAGE_FILEPATH); @@ -56,7 +59,12 @@ public void saveTask(TaskList tasks){ e.printStackTrace(); } } - + /** + * This method writes all the text into the file. + * + * @param filePath Path of the file to be written. + * @param text String of the text to be stored in the file. + * */ private void writeToFile(String filePath, String text) throws IOException { FileWriter fw = new FileWriter(filePath); fw.write(text); diff --git a/src/main/java/Duke/TaskList/Task.java b/src/main/java/Duke/TaskList/Task.java index 01fa4660..02f398cf 100644 --- a/src/main/java/Duke/TaskList/Task.java +++ b/src/main/java/Duke/TaskList/Task.java @@ -32,12 +32,20 @@ public String getDescription(){ public void setDescription(String description) { this.description = description; } - + /** + * This method returns the object in String format. + * + * @return the object in string format + * */ public String toString(){ String print = "[" + getStatusIcon()+"] " + getDescription(); return print; } - + /** + * This method return the object in String format. + * + * @return the object in string format for the task to be saved. + * */ public String saveTask(){ String save = getStatusDone() + "|" + getDescription(); return save; diff --git a/src/main/java/Duke/Ui/Ui.java b/src/main/java/Duke/Ui/Ui.java index c480bcdb..243c2a69 100644 --- a/src/main/java/Duke/Ui/Ui.java +++ b/src/main/java/Duke/Ui/Ui.java @@ -25,7 +25,11 @@ public void printBye() { System.out.println("Bye. Hope to see you again soon!"); } - //Printing the count of tasks in the list. + /** + * Listing the total number of Task object inside the TaskLists. + * + * @param counter the number of task in the TaskLists + * */ public void printTotalTask(int counter) { System.out.println("Now you have " + counter + " tasks in the list."); }