From 8b1aacf32252959efc86caefc2b2a1a135139fa7 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Thu, 20 Jan 2022 23:43:28 +0800 Subject: [PATCH 01/23] add increment level-0 --- src/main/java/Greet.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/Greet.java diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java new file mode 100644 index 000000000..6e4fb5dd1 --- /dev/null +++ b/src/main/java/Greet.java @@ -0,0 +1,7 @@ +public class Greet { + public static void main(String[] args){ + System.out.println("Hello! I'm Duke"); + System.out.println("What can I do for you?"); + System.out.println("Bye. Hope to see you again soon!"); + } +} From bcfbc0de009467037e0f12775b763ccf3524e7b0 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Wed, 2 Feb 2022 10:10:43 +0800 Subject: [PATCH 02/23] Greet, echo, exit --- src/main/java/Greet.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java index 6e4fb5dd1..1523ca2c1 100644 --- a/src/main/java/Greet.java +++ b/src/main/java/Greet.java @@ -1,7 +1,17 @@ +import java.util.Scanner; + public class Greet { public static void main(String[] args){ System.out.println("Hello! I'm Duke"); System.out.println("What can I do for you?"); + Scanner sc = new Scanner(System.in); + + String input; + input = sc.nextLine(); + while(!input.equals("bye")){ + System.out.println(" "+ input); + input = sc.nextLine(); + } System.out.println("Bye. Hope to see you again soon!"); } } From cd624bece56007ca36e49fda4e3e5fa12bedee7c Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Wed, 2 Feb 2022 10:56:07 +0800 Subject: [PATCH 03/23] Add, List --- src/main/java/Greet.java | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java index 1523ca2c1..911514d52 100644 --- a/src/main/java/Greet.java +++ b/src/main/java/Greet.java @@ -6,12 +6,26 @@ public static void main(String[] args){ System.out.println("What can I do for you?"); Scanner sc = new Scanner(System.in); - String input; - input = sc.nextLine(); - while(!input.equals("bye")){ - System.out.println(" "+ input); - input = sc.nextLine(); + String[] input = new String[100]; + int idx = 0; + input[0] = sc.nextLine(); + while(!input[idx].equals("bye")){ + if(input[idx].equals("list")){ + idx--; + for(int i =0; i<=idx ;i++){ + System.out.println(" " + (i+1) + ". " + input[i]); + } + } + else { + if (idx >= 100) { + break; + } + System.out.println(" added: " + input[idx]); + } + idx++; + input[idx] = sc.nextLine(); } + System.out.println("Bye. Hope to see you again soon!"); } } From f953ae5279fee0774dcc6302fb3c87ae0c5c7bd4 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Wed, 2 Feb 2022 12:10:37 +0800 Subject: [PATCH 04/23] add Task, taskmanagement --- src/main/java/Greet.java | 3 +-- src/main/java/Task.java | 25 ++++++++++++++++++ src/main/java/TaskManagement.java | 44 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/main/java/Task.java create mode 100644 src/main/java/TaskManagement.java diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java index 911514d52..120646637 100644 --- a/src/main/java/Greet.java +++ b/src/main/java/Greet.java @@ -2,8 +2,7 @@ public class Greet { public static void main(String[] args){ - System.out.println("Hello! I'm Duke"); - System.out.println("What can I do for you?"); + Scanner sc = new Scanner(System.in); String[] input = new String[100]; diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 000000000..f230976aa --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,25 @@ +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" : " "); + } + + public void markAsDone(){ + this.isDone=true; + } + + public void markAsUndone(){ + this.isDone=false; + } + + public String toString(){ + return "[" + getStatusIcon() + "] " + description; + } +} diff --git a/src/main/java/TaskManagement.java b/src/main/java/TaskManagement.java new file mode 100644 index 000000000..fd0a10c9d --- /dev/null +++ b/src/main/java/TaskManagement.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +public class TaskManagement { + public static void main(String[] args) { + Task[] taskList = new Task[3]; + taskList[0] = new Task("read book"); + taskList[1] = new Task("return book"); + taskList[2] = new Task("buy bread"); + + taskList[0].markAsDone(); + + + Scanner sc = new Scanner(System.in); + String input = sc.nextLine(); + while(!input.equals("bye")){ + if(input.equals("list")){ + System.out.println("Here are the tasks in your list:"); + for(int i =0 ; i<3; i++){ + System.out.println((i+1) + ". " + taskList[i].toString()); + } + } + else if(input.contains("unmark")){ + System.out.println("Ok, I've marked this task as not done yet:"); + int idx = Integer.parseInt(input.replace("unmark ", "")); + taskList[idx-1].markAsUndone(); + System.out.println(taskList[idx-1].toString()); + + } + else if(input.contains("mark")){ + System.out.println("Nice! I have marked this task as done:"); + int idx = Integer.parseInt(input.replace("mark ", "")); + taskList[idx-1].markAsDone(); + System.out.println(taskList[idx-1].toString()); + + } + else{ + System.out.println(" " + input); + } + input = sc.nextLine(); + } + System.out.println(" Bye. Hope to see you again soon!"); + } + +} \ No newline at end of file From bae8eaff1954ae2e4f1f72a245f64ba2e76e0b35 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Wed, 2 Feb 2022 12:16:38 +0800 Subject: [PATCH 05/23] tweat code --- src/main/java/{TaskManagement.java => TaskManager.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/{TaskManagement.java => TaskManager.java} (98%) diff --git a/src/main/java/TaskManagement.java b/src/main/java/TaskManager.java similarity index 98% rename from src/main/java/TaskManagement.java rename to src/main/java/TaskManager.java index fd0a10c9d..1bb254317 100644 --- a/src/main/java/TaskManagement.java +++ b/src/main/java/TaskManager.java @@ -1,6 +1,6 @@ import java.util.Scanner; -public class TaskManagement { +public class TaskManager { public static void main(String[] args) { Task[] taskList = new Task[3]; taskList[0] = new Task("read book"); From 05271d4ee48183a532af688637e10d00eeabf70d Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Tue, 8 Feb 2022 21:55:32 +0800 Subject: [PATCH 06/23] add todo, deadline, event --- src/main/java/Deadline.java | 24 ++++++++++ src/main/java/Event.java | 15 ++++++ src/main/java/Task.java | 4 ++ src/main/java/TaskManager.java | 86 +++++++++++++++++++++++++--------- src/main/java/Todo.java | 10 ++++ 5 files changed, 117 insertions(+), 22 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..914813a88 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,24 @@ + + +public class Deadline extends Task{ + private String by; + + public Deadline(String description, String by){ + super(description); + this.by=by; + } + + public String getBy(){ + return by; + } + + public void setBy(String by){ + this.by = by; + } + + @Override + public String toString(){ + return "[D]" + super.toString() + "(by: " + by + ")"; + } + +} diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 000000000..d182100bd --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,15 @@ +public class Event extends Task{ + private String at; + + public Event(String description, String at) { + super(description); + this.at = at; + } + + public String toString(){ + return "[E]" + super.toString() + "(at: " + at + ")"; + } + + + +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index f230976aa..d2aae506c 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -22,4 +22,8 @@ public void markAsUndone(){ public String toString(){ return "[" + getStatusIcon() + "] " + description; } + + public String getSymbol(){ + return ""; + } } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 1bb254317..3d34f0a72 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -1,44 +1,86 @@ +import jdk.swing.interop.SwingInterOpUtils; + +import java.util.Locale; import java.util.Scanner; public class TaskManager { - public static void main(String[] args) { - Task[] taskList = new Task[3]; - taskList[0] = new Task("read book"); - taskList[1] = new Task("return book"); - taskList[2] = new Task("buy bread"); + private static final String INDENT = " "; + private static final String LINE="-------------------------------------------"; + private static int taskCount=0; + private static Task[] taskList = new Task[100] ; - taskList[0].markAsDone(); - + public static void main(String[] args) { + System.out.println(INDENT + "Hi, I am XiaoAi TongXue ;D"); + System.out.println(INDENT + LINE); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); - while(!input.equals("bye")){ - if(input.equals("list")){ - System.out.println("Here are the tasks in your list:"); - for(int i =0 ; i<3; i++){ - System.out.println((i+1) + ". " + taskList[i].toString()); + String action = input.split(" ")[0].toLowerCase(Locale.ROOT); + System.out.println(INDENT + LINE); + while(!action.equals("bye")){ + if(action.equals("list")){ + System.out.println(INDENT+"Here are the tasks in your list:"); + for(int i =0 ; i Date: Wed, 9 Feb 2022 13:11:45 +0800 Subject: [PATCH 07/23] add error handle --- src/main/java/Task.java | 3 - src/main/java/TaskManager.java | 184 ++++++++++++++++++++++----------- 2 files changed, 126 insertions(+), 61 deletions(-) diff --git a/src/main/java/Task.java b/src/main/java/Task.java index d2aae506c..816af48ed 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -23,7 +23,4 @@ public String toString(){ return "[" + getStatusIcon() + "] " + description; } - public String getSymbol(){ - return ""; - } } diff --git a/src/main/java/TaskManager.java b/src/main/java/TaskManager.java index 3d34f0a72..bb0111f65 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/TaskManager.java @@ -6,81 +6,149 @@ public class TaskManager { private static final String INDENT = " "; private static final String LINE="-------------------------------------------"; + public static final String SPACE = " "; + private static final int MAX_TASK_COUNT = 100; + + private static int taskCount=0; - private static Task[] taskList = new Task[100] ; + private static Task[] taskList = new Task[MAX_TASK_COUNT] ; + private static Scanner sc = new Scanner(System.in); - public static void main(String[] args) { - System.out.println(INDENT + "Hi, I am XiaoAi TongXue ;D"); - System.out.println(INDENT + LINE); - Scanner sc = new Scanner(System.in); - String input = sc.nextLine(); - String action = input.split(" ")[0].toLowerCase(Locale.ROOT); - System.out.println(INDENT + LINE); + public static void main(String[] args) { + greet(); + String input = getInput(); + String action = getAction(input); + printLine(); + while(!action.equals("bye")){ - if(action.equals("list")){ - System.out.println(INDENT+"Here are the tasks in your list:"); - for(int i =0 ; i Date: Wed, 9 Feb 2022 13:17:38 +0800 Subject: [PATCH 08/23] divide into packages --- src/main/java/{ => controller}/TaskManager.java | 8 ++++++-- src/main/java/{ => task}/Deadline.java | 4 ++-- src/main/java/{ => task}/Event.java | 2 ++ src/main/java/{ => task}/Task.java | 2 ++ src/main/java/{ => task}/Todo.java | 2 ++ text-ui-test/runtest.bat | 2 +- 6 files changed, 15 insertions(+), 5 deletions(-) rename src/main/java/{ => controller}/TaskManager.java (97%) rename src/main/java/{ => task}/Deadline.java (87%) rename src/main/java/{ => task}/Event.java (94%) rename src/main/java/{ => task}/Task.java (97%) rename src/main/java/{ => task}/Todo.java (92%) diff --git a/src/main/java/TaskManager.java b/src/main/java/controller/TaskManager.java similarity index 97% rename from src/main/java/TaskManager.java rename to src/main/java/controller/TaskManager.java index bb0111f65..2a9063f63 100644 --- a/src/main/java/TaskManager.java +++ b/src/main/java/controller/TaskManager.java @@ -1,6 +1,10 @@ -import jdk.swing.interop.SwingInterOpUtils; +package controller; + +import task.Deadline; +import task.Event; +import task.Task; +import task.Todo; -import java.util.Locale; import java.util.Scanner; public class TaskManager { diff --git a/src/main/java/Deadline.java b/src/main/java/task/Deadline.java similarity index 87% rename from src/main/java/Deadline.java rename to src/main/java/task/Deadline.java index 914813a88..e8b85bb85 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -1,6 +1,6 @@ +package task; - -public class Deadline extends Task{ +public class Deadline extends Task { private String by; public Deadline(String description, String by){ diff --git a/src/main/java/Event.java b/src/main/java/task/Event.java similarity index 94% rename from src/main/java/Event.java rename to src/main/java/task/Event.java index d182100bd..c817bad1a 100644 --- a/src/main/java/Event.java +++ b/src/main/java/task/Event.java @@ -1,3 +1,5 @@ +package task; + public class Event extends Task{ private String at; diff --git a/src/main/java/Task.java b/src/main/java/task/Task.java similarity index 97% rename from src/main/java/Task.java rename to src/main/java/task/Task.java index 816af48ed..76cd5b776 100644 --- a/src/main/java/Task.java +++ b/src/main/java/task/Task.java @@ -1,3 +1,5 @@ +package task; + public class Task { protected String description; protected boolean isDone; diff --git a/src/main/java/Todo.java b/src/main/java/task/Todo.java similarity index 92% rename from src/main/java/Todo.java rename to src/main/java/task/Todo.java index 1b87d35c1..869d894d3 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/task/Todo.java @@ -1,3 +1,5 @@ +package task; + public class Todo extends Task{ public Todo(String description){ diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 087374464..c8e451714 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 controller.TaskManager < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT From fabf6225735262866107c0e773822eb1d596370a Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Wed, 16 Feb 2022 18:56:31 +0800 Subject: [PATCH 09/23] code quality edit --- src/main/java/IllegalShapeException.java | 2 ++ src/main/java/controller/TaskManager.java | 21 +++++++++++---------- 2 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 src/main/java/IllegalShapeException.java diff --git a/src/main/java/IllegalShapeException.java b/src/main/java/IllegalShapeException.java new file mode 100644 index 000000000..957599a81 --- /dev/null +++ b/src/main/java/IllegalShapeException.java @@ -0,0 +1,2 @@ +package PACKAGE_NAME;public class IllegalShapeException { +} diff --git a/src/main/java/controller/TaskManager.java b/src/main/java/controller/TaskManager.java index 2a9063f63..1b7de5b2d 100644 --- a/src/main/java/controller/TaskManager.java +++ b/src/main/java/controller/TaskManager.java @@ -81,15 +81,6 @@ public static void addTask(Task t){ } - public static boolean checkDescription(String des){ - //怎么显示没有description? - if(des.equals("")){ - System.out.println(INDENT + "OOPS! The description of task cannot be empty!"); - return false; - } - else return true; - } - private static void greet(){ printLine(); System.out.println(INDENT + "Hi, I am XiaoAi TongXue ;D"); @@ -155,4 +146,14 @@ private static void addTaskByMessage(String action, String input){ addTask(newTask); } -} \ No newline at end of file +} +// +//public class Cup{ +// private T item; +// public void set(T item){ +// this.item = item; +// } +// public T get(){ +// return item; +// } +//} \ No newline at end of file From 1fb33c4ae36b157bcd8eb2f51d2b7231a705433a Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Wed, 16 Feb 2022 19:15:33 +0800 Subject: [PATCH 10/23] add delete funciton --- src/main/java/controller/TaskManager.java | 39 +++++++++++++++++------ 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main/java/controller/TaskManager.java b/src/main/java/controller/TaskManager.java index 1b7de5b2d..a23ef8c36 100644 --- a/src/main/java/controller/TaskManager.java +++ b/src/main/java/controller/TaskManager.java @@ -5,6 +5,7 @@ import task.Task; import task.Todo; +import java.util.ArrayList; import java.util.Scanner; public class TaskManager { @@ -15,7 +16,7 @@ public class TaskManager { private static int taskCount=0; - private static Task[] taskList = new Task[MAX_TASK_COUNT] ; + private static ArrayList taskList = new ArrayList<>(); private static Scanner sc = new Scanner(System.in); @@ -31,7 +32,7 @@ public static void main(String[] args) { listTask(); break; case "mark": - updateTask(Integer.parseInt(input.split(SPACE)[1])-1,true); + updateTask(Integer.parseInt(input.split(SPACE)[1])-1,true); //refractor get index break; case "unmark": updateTask(Integer.parseInt(input.split(SPACE)[1])-1,false); @@ -41,6 +42,9 @@ public static void main(String[] args) { case "event": addTaskByMessage(action, input); break; + case "delete": + deleteTask(Integer.parseInt(input.split(SPACE)[1])-1); + break; default: printErrorInputMessage(); } @@ -52,6 +56,17 @@ public static void main(String[] args) { bye(); } + private static void deleteTask(int idx){ + System.out.println(INDENT+"Noted. I've removed this task:"); + printTask(taskList.get(idx)); + taskList.remove(idx); + printTaskCount(); + } + + private static void updateTaskCount(){ + taskCount = taskList.size(); + } + private static void bye() { System.out.println(INDENT + "Bye. Hope to see you again soon!"); printLine(); @@ -59,8 +74,8 @@ private static void bye() { private static void listTask() { System.out.println(INDENT+"Here are the tasks in your list:"); - for(int i =0 ; i Date: Fri, 18 Feb 2022 15:23:27 +0800 Subject: [PATCH 11/23] add save function --- data/task-file | 1 + src/main/java/IllegalShapeException.java | 2 - src/main/java/controller/TaskManager.java | 154 +++++++++++++++++++++- src/main/java/task/Deadline.java | 6 + src/main/java/task/Event.java | 4 + src/main/java/task/Task.java | 9 ++ src/main/java/task/Todo.java | 3 + 7 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 data/task-file delete mode 100644 src/main/java/IllegalShapeException.java diff --git a/data/task-file b/data/task-file new file mode 100644 index 000000000..61a8f34a5 --- /dev/null +++ b/data/task-file @@ -0,0 +1 @@ +T | | read diff --git a/src/main/java/IllegalShapeException.java b/src/main/java/IllegalShapeException.java deleted file mode 100644 index 957599a81..000000000 --- a/src/main/java/IllegalShapeException.java +++ /dev/null @@ -1,2 +0,0 @@ -package PACKAGE_NAME;public class IllegalShapeException { -} diff --git a/src/main/java/controller/TaskManager.java b/src/main/java/controller/TaskManager.java index 1b7de5b2d..a53e81df8 100644 --- a/src/main/java/controller/TaskManager.java +++ b/src/main/java/controller/TaskManager.java @@ -5,13 +5,16 @@ import task.Task; import task.Todo; -import java.util.Scanner; +import java.io.*; +import java.util.*; public class TaskManager { private static final String INDENT = " "; private static final String LINE="-------------------------------------------"; public static final String SPACE = " "; private static final int MAX_TASK_COUNT = 100; + private static final String DIR = "data/task-file"; + private static final String FILE_SEPARATOR = " | "; private static int taskCount=0; @@ -19,7 +22,7 @@ public class TaskManager { private static Scanner sc = new Scanner(System.in); - public static void main(String[] args) { + public static void main(String[] args) { greet(); String input = getInput(); String action = getAction(input); @@ -53,6 +56,7 @@ public static void main(String[] args) { } private static void bye() { + save(DIR, new ArrayList<>(Arrays.asList(taskList))); System.out.println(INDENT + "Bye. Hope to see you again soon!"); printLine(); } @@ -145,8 +149,152 @@ private static void addTaskByMessage(String action, String input){ } addTask(newTask); } - + +// private static void retrieveData() { +// File f = new File(DIR); +// if(f.exists()){ +// taskList = load(f); +// }else{ +// f.getParentFile().mkdir(); +// f.createNewFile(); +// taskList = new ArrayList(); +// save(DIR, taskList); +// } +// } + + private static void save(String filename, ArrayList al){ + StringBuilder sb = new StringBuilder(); + List newList = new ArrayList(); + for(int i = 0; i < al.size(); i++){ + Task task = al.get(i); + if(task == null) break; + String taskType = task.getTypeIcon(); + String taskStatus = task.getStatusIcon(); + String taskDetails = task.getDescription(); + sb.append(taskType); + sb.append(FILE_SEPARATOR); + sb.append(taskStatus); + sb.append(FILE_SEPARATOR); + sb.append(taskDetails); + if(taskType.equals("D") || taskType.equals("E")){ + sb.append(FILE_SEPARATOR); + sb.append(task.getTime()); + } + sb.append(System.lineSeparator()); + } + try { + writeToFile(filename, sb.toString()); + } catch (IOException e) { + System.out.println("Something went wrong:" + e.getMessage()); + } + } + + public static void writeToFile(String fileName, String data) throws IOException { + FileWriter fw = new FileWriter(fileName); + fw.write(data); + fw.close(); + } + + +// +// public static List read(String filename) throws IOException{ +// File f = new File(filename); +// Scanner s = new Scanner(f); +// while(s.hasNext()) { +// String newLine = s.nextLine(); +// Task newTask; +// StringTokenizer st = new StringTokenizer(newLine, FILE_SEPARATOR); +// +// String taskType = st.nextToken(); +// String taskStatus = st.nextToken(); +// String description = st.nextToken(); +// if(taskType.equals("D")) { +// String time = st.nextToken(); +// newTask = new Deadline(description,time); +// newTask. +// } +// +// +// +// } +// try{ +// List data = new ArrayList(); +// Scanner scanner = new Scanner(new FileInputStream(filename)); +// try { +// while (scanner.hasNextLine()) { +// data.add(scanner.nextLine()); +// } +// } finally { +// scanner.close(); +// } +// return data; +// }catch(IOException e){ +// System.out.println("reading file unsuccessfully"); +// e.printStackTrace(); +// return null; +// } +// } + + + + +// try { +// File file = new File(dir); +// if (file.exists()) { +// orders = load(dir); +// } else { +// file.getParentFile().mkdir(); +// file.createNewFile(); +// orders = new ArrayList(); +// save(dir, orders); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } } + + +// +// /** +// * This method is to load orders from external files +// * @param filename +// * specifies where the external files stored +// * @return all reservations read from the file +// */ +// @Override +// public ArrayList load(String filename) { +// ArrayList stringArray = (ArrayList) read(filename); +// ArrayList alr = new ArrayList(); +// +// for (int i = 0; i < stringArray.size(); i++) { +// String st = (String) stringArray.get(i); +// StringTokenizer star = new StringTokenizer(st, "|"); +// +// int orderId = Integer.parseInt(star.nextToken().trim()); +// int staffId = Integer.parseInt(star.nextToken().trim()); +// int tableId = Integer.parseInt(star.nextToken().trim()); +// int numberOfPax = Integer.parseInt(star.nextToken().trim()); +// int orderSize = Integer.parseInt(star.nextToken().trim()); // write the orderSize in the file in order to read different size of order items +// boolean isActive = Boolean.parseBoolean(star.nextToken().trim()); +// +// //create order with no order item +// Order order = new Order(orderId, staffId, tableId, numberOfPax, isActive); +// //add order item in order +// for (int j = 0; j < orderSize; j++) { +// int itemId = Integer.parseInt(star.nextToken().trim()); +// String name = star.nextToken().trim(); +// int quantity = Integer.parseInt(star.nextToken().trim()); +// double price = Double.parseDouble(star.nextToken().trim()); +// order.addOrderItem(itemId, quantity, name, price); +// } +// //add order to order list +// alr.add(order); +// } +// return alr; +// +// } + + // //public class Cup{ // private T item; diff --git a/src/main/java/task/Deadline.java b/src/main/java/task/Deadline.java index e8b85bb85..69983a58a 100644 --- a/src/main/java/task/Deadline.java +++ b/src/main/java/task/Deadline.java @@ -21,4 +21,10 @@ public String toString(){ return "[D]" + super.toString() + "(by: " + by + ")"; } + public String getTypeIcon(){return "D";} + + public String getTime(){ + return by; + } + } diff --git a/src/main/java/task/Event.java b/src/main/java/task/Event.java index c817bad1a..1694ad2bb 100644 --- a/src/main/java/task/Event.java +++ b/src/main/java/task/Event.java @@ -11,6 +11,10 @@ public Event(String description, String at) { public String toString(){ return "[E]" + super.toString() + "(at: " + at + ")"; } + public String getTypeIcon(){return "E";} + public String getTime(){ + return at; + } diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 76cd5b776..23aab17ef 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -13,6 +13,10 @@ public String getStatusIcon(){ return (isDone ? "X" : " "); } + public String getDescription() { + return description; + } + public void markAsDone(){ this.isDone=true; } @@ -25,4 +29,9 @@ public String toString(){ return "[" + getStatusIcon() + "] " + description; } + public String getTypeIcon(){return null;} + + public String getTime(){return null;} + + } diff --git a/src/main/java/task/Todo.java b/src/main/java/task/Todo.java index 869d894d3..c3f982cd3 100644 --- a/src/main/java/task/Todo.java +++ b/src/main/java/task/Todo.java @@ -9,4 +9,7 @@ public Todo(String description){ public String toString(){ return "[T]"+super.toString(); } + + public String getTypeIcon(){return "T";} + } From 79d39e910d7c243613e597d8ee088b5d684a9776 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Fri, 18 Feb 2022 15:25:36 +0800 Subject: [PATCH 12/23] no message --- src/main/java/IllegalShapeException.java | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 src/main/java/IllegalShapeException.java diff --git a/src/main/java/IllegalShapeException.java b/src/main/java/IllegalShapeException.java new file mode 100644 index 000000000..4125ee21b --- /dev/null +++ b/src/main/java/IllegalShapeException.java @@ -0,0 +1,3 @@ +public class IllegalShapeException extends Exception { + //no other code needed +} \ No newline at end of file From ebecfe34a71b237c20bd0caa163748807324162a Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Fri, 4 Mar 2022 15:57:42 +0800 Subject: [PATCH 13/23] manychangesss --- .gitignore | 3 + data/task-file | 5 +- src/main/java/Duke.java | 10 - src/main/java/Greet.java | 30 - src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/command/AddCommand.java | 35 ++ src/main/java/command/Command.java | 10 + src/main/java/command/DeleteCommand.java | 25 + src/main/java/command/ExitCommand.java | 15 + src/main/java/command/ListCommand.java | 20 + src/main/java/command/UpdateCommand.java | 28 + src/main/java/controller/Duke.java | 49 ++ src/main/java/controller/DukeException.java | 10 + src/main/java/controller/Parser.java | 50 ++ src/main/java/controller/Storage.java | 69 +++ src/main/java/controller/TaskList.java | 111 ++++ src/main/java/controller/TaskManager.java | 632 ++++++++++---------- src/main/java/controller/UI.java | 82 +++ src/main/java/task/Task.java | 2 +- 19 files changed, 832 insertions(+), 357 deletions(-) delete mode 100644 src/main/java/Duke.java delete mode 100644 src/main/java/Greet.java create mode 100644 src/main/java/META-INF/MANIFEST.MF create mode 100644 src/main/java/command/AddCommand.java create mode 100644 src/main/java/command/Command.java create mode 100644 src/main/java/command/DeleteCommand.java create mode 100644 src/main/java/command/ExitCommand.java create mode 100644 src/main/java/command/ListCommand.java create mode 100644 src/main/java/command/UpdateCommand.java create mode 100644 src/main/java/controller/Duke.java create mode 100644 src/main/java/controller/DukeException.java create mode 100644 src/main/java/controller/Parser.java create mode 100644 src/main/java/controller/Storage.java create mode 100644 src/main/java/controller/TaskList.java create mode 100644 src/main/java/controller/UI.java diff --git a/.gitignore b/.gitignore index f69985ef1..7d958bad6 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ bin/ /text-ui-test/ACTUAL.txt text-ui-test/EXPECTED-UNIX.TXT +src/main/java/IllegalShapeException.java +src/main/java/Main.java +src/main/java/temp.java diff --git a/data/task-file b/data/task-file index 61a8f34a5..a13a78936 100644 --- a/data/task-file +++ b/data/task-file @@ -1 +1,4 @@ -T | | read +T | X | task +T | 0 | task +E | X | project | today +T | 0 | new diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334c..000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/Greet.java b/src/main/java/Greet.java deleted file mode 100644 index 120646637..000000000 --- a/src/main/java/Greet.java +++ /dev/null @@ -1,30 +0,0 @@ -import java.util.Scanner; - -public class Greet { - public static void main(String[] args){ - - Scanner sc = new Scanner(System.in); - - String[] input = new String[100]; - int idx = 0; - input[0] = sc.nextLine(); - while(!input[idx].equals("bye")){ - if(input[idx].equals("list")){ - idx--; - for(int i =0; i<=idx ;i++){ - System.out.println(" " + (i+1) + ". " + input[i]); - } - } - else { - if (idx >= 100) { - break; - } - System.out.println(" added: " + input[idx]); - } - idx++; - input[idx] = sc.nextLine(); - } - - System.out.println("Bye. Hope to see you again soon!"); - } -} diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..bbf9ca08a --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: controller.TaskManager + diff --git a/src/main/java/command/AddCommand.java b/src/main/java/command/AddCommand.java new file mode 100644 index 000000000..846ebd6f0 --- /dev/null +++ b/src/main/java/command/AddCommand.java @@ -0,0 +1,35 @@ +package command; + +import controller.Storage; +import controller.TaskList; +import controller.UI; +import task.Task; +import task.Todo; + +public class AddCommand extends Command { + String input; + String action; + + public AddCommand(String action, String input) { + super(); + this.input = input; + this.action =action; + } + + public void execute(TaskList tasks, UI ui, Storage storage){ + String t = tasks.addTask(action,input); + if(t == null){ + ui.showErrorTask(); + return; + } + ui.showAdd(); + ui.showTask(t); + ui.showTaskCount(tasks.getCount()); + storage.save(tasks.getTaskList()); + } + + public boolean isExit(){ + return false; + } + +} diff --git a/src/main/java/command/Command.java b/src/main/java/command/Command.java new file mode 100644 index 000000000..15e3ce31b --- /dev/null +++ b/src/main/java/command/Command.java @@ -0,0 +1,10 @@ +package command; + +import controller.Storage; +import controller.TaskList; +import controller.UI; + +public abstract class Command { + public abstract void execute(TaskList tasks, UI ui, Storage storage); + public abstract boolean isExit(); +} diff --git a/src/main/java/command/DeleteCommand.java b/src/main/java/command/DeleteCommand.java new file mode 100644 index 000000000..8ac0b39e4 --- /dev/null +++ b/src/main/java/command/DeleteCommand.java @@ -0,0 +1,25 @@ +package command; + +import controller.Storage; +import controller.TaskList; +import controller.UI; + +public class DeleteCommand extends Command{ + int idx; + public DeleteCommand(int i) { + super(); + idx = i; + } + + public void execute(TaskList tasks, UI ui, Storage storage){ + ui.showDelete(); + tasks.removeTaskByIdx(idx); + ui.showList(); + ui.showTaskCount(tasks.getCount()); + storage.save(tasks.getTaskList()); + } + + public boolean isExit(){ + return false; + } +} diff --git a/src/main/java/command/ExitCommand.java b/src/main/java/command/ExitCommand.java new file mode 100644 index 000000000..6465b7bed --- /dev/null +++ b/src/main/java/command/ExitCommand.java @@ -0,0 +1,15 @@ +package command; + +import controller.Storage; +import controller.TaskList; +import controller.UI; + +public class ExitCommand extends Command{ + public void execute(TaskList tasks, UI ui, Storage storage){ + ui.showBye(); + storage.save(tasks.getTaskList()); + } + public boolean isExit(){ + return true; + } +} diff --git a/src/main/java/command/ListCommand.java b/src/main/java/command/ListCommand.java new file mode 100644 index 000000000..015afa051 --- /dev/null +++ b/src/main/java/command/ListCommand.java @@ -0,0 +1,20 @@ +package command; + + +import controller.Storage; +import controller.TaskList; +import controller.UI; + +public class ListCommand extends Command { + private static final String INDENT = " "; + + public void execute(TaskList taskList, UI ui, Storage storage){ + ui.showList(); + for(int i = 0; i al) { + StringBuilder sb = new StringBuilder(); + List newList = new ArrayList(); + for (int i = 0; i < al.size(); i++) { + Task task = al.get(i); + if (task == null) break; + String taskType = task.getTypeIcon(); + String taskStatus = task.getStatusIcon(); + String taskDetails = task.getDescription(); + sb.append(taskType); + sb.append(FILE_SEPARATOR); + sb.append(taskStatus); + sb.append(FILE_SEPARATOR); + sb.append(taskDetails); + if (taskType.equals("D") || taskType.equals("E")) { + sb.append(FILE_SEPARATOR); + sb.append(task.getTime()); + } + sb.append(System.lineSeparator()); + } + try { + writeToFile(sb.toString()); + } catch (IOException e) { + System.out.println("Something went wrong:" + e.getMessage()); + } + } + + private void writeToFile(String data) throws IOException { + FileWriter fw = new FileWriter(filePath); + fw.write(data); + fw.close(); + } + + public String load() throws java.io.FileNotFoundException { + StringBuilder sb = new StringBuilder(); + File f = new File(filePath); // create a File for the given file path + Scanner s = new Scanner(f); // create a Scanner using the File as the source + while (s.hasNext()) { + sb.append(s.nextLine()); + sb.append(System.lineSeparator()); + } + return sb.toString(); + } +} + + + diff --git a/src/main/java/controller/TaskList.java b/src/main/java/controller/TaskList.java new file mode 100644 index 000000000..fa339b9a4 --- /dev/null +++ b/src/main/java/controller/TaskList.java @@ -0,0 +1,111 @@ +package controller; + +import task.Deadline; +import task.Event; +import task.Task; +import task.Todo; + +import java.util.ArrayList; +import java.util.StringTokenizer; + +public class TaskList { + private static final String FILE_SEPARATOR = " | "; + private static final String INDENT = " "; + private static final String LINE = "-------------------------------------------"; + public static final String SPACE = " "; + private static ArrayList taskList = new ArrayList<>(); + + public TaskList(String data) { + String[] taskStrList = data.split(System.lineSeparator()); + System.out.println("string list length: " + taskStrList.length); + for (int i = 0; i < taskStrList.length; i++) { + String newLine = taskStrList[i]; + Task newTask; + StringTokenizer st = new StringTokenizer(newLine, FILE_SEPARATOR); + String taskType = st.nextToken(); + String taskStatus = st.nextToken(); + String description = st.nextToken(); + + if (taskType.equals("D")) { + String time = st.nextToken(); + newTask = new Deadline(description, time); + } else if (taskType.equals("E")) { + String time = st.nextToken(); + newTask = new Event(description, time); + } else { + newTask = new Todo(description); + } + + if (taskStatus.equals("X")) newTask.markAsDone(); + taskList.add(newTask); + } + } + + public TaskList() { + taskList = new ArrayList<>(); + } + + public int getCount () { + return taskList.size(); + } + + public Task getTaskByIdx ( int i){ + return taskList.get(i); + } + + public void removeTaskByIdx ( int i){ + taskList.remove(i); + } + + public ArrayList getTaskList () { + return taskList; + } + + public String addTask (String type, String input){ + String[] inputWord = input.split(SPACE); + if (inputWord.length == 1) { + System.out.println(INDENT + "OOPS! The task doesn't have description :("); // check description + return null; + } + int desIdx = input.indexOf(" ") + 1; + Task newTask = null; + switch (type) { + case "todo": + String description = input.substring(desIdx); + newTask = new Todo(description); + break; + case "deadline": + case "event": + if (!input.contains("/")) { + // check time + return null; + } + + int timeIdx = input.indexOf("/") + 4; + int desEndIdx = input.indexOf("/") - 1; + description = input.substring(desIdx, desEndIdx); + String time = input.substring(timeIdx); + if (type.equals("deadline")) { + newTask = new Deadline(description, time); + } else { + newTask = new Event(description, time); + } + break; + } + taskList.add(newTask); + return newTask.toString(); + } + + public String markTaskByIdx(int i){ + taskList.get(i).markAsDone(); + return taskList.get(i).toString(); + } + + public String unmarkTaskByIdx(int i){ + taskList.get(i).markAsUndone(); + return taskList.get(i).toString(); + } + +} + + diff --git a/src/main/java/controller/TaskManager.java b/src/main/java/controller/TaskManager.java index 48b86fd68..30d3dcce9 100644 --- a/src/main/java/controller/TaskManager.java +++ b/src/main/java/controller/TaskManager.java @@ -1,330 +1,332 @@ -package controller; - -import task.Deadline; -import task.Event; -import task.Task; -import task.Todo; - - -import java.util.ArrayList; -import java.util.Scanner; -import java.util.List; -import java.io.IOException; -import java.io.FileWriter; - - -public class TaskManager { - private static final String INDENT = " "; - private static final String LINE="-------------------------------------------"; - public static final String SPACE = " "; - private static final int MAX_TASK_COUNT = 100; - private static final String DIR = "data/task-file"; - private static final String FILE_SEPARATOR = " | "; - - - private static int taskCount=0; - private static ArrayList taskList = new ArrayList<>(); - private static Scanner sc = new Scanner(System.in); - - - public static void main(String[] args) { - greet(); - String input = getInput(); - String action = getAction(input); - printLine(); - - while(!action.equals("bye")){ - switch(action){ - case "list": - listTask(); - break; - case "mark": - updateTask(Integer.parseInt(input.split(SPACE)[1])-1,true); //refractor get index - break; - case "unmark": - updateTask(Integer.parseInt(input.split(SPACE)[1])-1,false); - break; - case "todo": - case "deadline": - case "event": - addTaskByMessage(action, input); - break; - case "delete": - deleteTask(Integer.parseInt(input.split(SPACE)[1])-1); - break; - default: - printErrorInputMessage(); - } - printLine(); - input = getInput(); - action = getAction(input); - printLine(); - } - bye(); - } - - private static void deleteTask(int idx){ - System.out.println(INDENT+"Noted. I've removed this task:"); - printTask(taskList.get(idx)); - taskList.remove(idx); - printTaskCount(); - } - - private static void updateTaskCount(){ - taskCount = taskList.size(); - } - - private static void bye() { - save(DIR, taskList); - System.out.println(INDENT + "Bye. Hope to see you again soon!"); - printLine(); - } - - private static void listTask() { - System.out.println(INDENT+"Here are the tasks in your list:"); - for(int i =0 ; i(); -// save(DIR, taskList); +//package controller; +// +//import task.Deadline; +//import task.Event; +//import task.Task; +//import task.Todo; +// +// +//import java.util.ArrayList; +//import java.util.Scanner; +//import java.util.List; +//import java.io.IOException; +//import java.io.FileWriter; +// +// +//public class TaskManager { +// private static final String INDENT = " "; +// private static final String LINE="-------------------------------------------"; +// public static final String SPACE = " "; +// private static final int MAX_TASK_COUNT = 100; +// private static final String DIR = "data/task-file"; +// private static final String FILE_SEPARATOR = " | "; +// +// +// private static int taskCount=0; +// +// private static Scanner sc = new Scanner(System.in); +// +// +// public static void main(String[] args) { +// UI ui = new UI(); +// +// greet(); +// String input = getInput(); +// String action = getAction(input); +// printLine(); +// +// while(!action.equals("bye")){ +// switch(action){ +// case "list": +// listTask(); +// break; +// case "mark": +// updateTask(Integer.parseInt(input.split(SPACE)[1])-1,true); //refractor get index +// break; +// case "unmark": +// updateTask(Integer.parseInt(input.split(SPACE)[1])-1,false); +// break; +// case "todo": +// case "deadline": +// case "event": +// addTaskByMessage(action, input); +// break; +// case "delete": +// deleteTask(Integer.parseInt(input.split(SPACE)[1])-1); +// break; +// default: +// printErrorInputMessage(); +// } +// printLine(); +// input = getInput(); +// action = getAction(input); +// printLine(); // } +// bye(); // } - - private static void save(String filename, ArrayList al){ - StringBuilder sb = new StringBuilder(); - List newList = new ArrayList(); - for(int i = 0; i < al.size(); i++){ - Task task = al.get(i); - if(task == null) break; - String taskType = task.getTypeIcon(); - String taskStatus = task.getStatusIcon(); - String taskDetails = task.getDescription(); - sb.append(taskType); - sb.append(FILE_SEPARATOR); - sb.append(taskStatus); - sb.append(FILE_SEPARATOR); - sb.append(taskDetails); - if(taskType.equals("D") || taskType.equals("E")){ - sb.append(FILE_SEPARATOR); - sb.append(task.getTime()); - } - sb.append(System.lineSeparator()); - } - try { - writeToFile(filename, sb.toString()); - } catch (IOException e) { - System.out.println("Something went wrong:" + e.getMessage()); - } - } - - public static void writeToFile(String fileName, String data) throws IOException { - FileWriter fw = new FileWriter(fileName); - fw.write(data); - fw.close(); - } - - -// -// public static List read(String filename) throws IOException{ -// File f = new File(filename); -// Scanner s = new Scanner(f); -// while(s.hasNext()) { -// String newLine = s.nextLine(); -// Task newTask; -// StringTokenizer st = new StringTokenizer(newLine, FILE_SEPARATOR); -// -// String taskType = st.nextToken(); -// String taskStatus = st.nextToken(); -// String description = st.nextToken(); -// if(taskType.equals("D")) { -// String time = st.nextToken(); -// newTask = new Deadline(description,time); -// newTask. -// } // +// private static void deleteTask(int idx){ +// System.out.println(INDENT+"Noted. I've removed this task:"); +// printTask(taskList.get(idx)); +// taskList.remove(idx); +// printTaskCount(); +// } // +// private static void updateTaskCount(){ +// taskCount = taskList.size(); +// } // +// private static void bye() { +// save(DIR, taskList); +// System.out.println(INDENT + "Bye. Hope to see you again soon!"); +// printLine(); +// } +// +// private static void listTask() { +// System.out.println(INDENT+"Here are the tasks in your list:"); +// for(int i =0 ; i(); -// save(dir, orders); +// +// private static void printLine(){ +// System.out.println(INDENT + LINE); +// } +// +// public static void printErrorInputMessage(){ +// System.out.println(INDENT+"OOPS!! I'm sorry, but i don;t know what that means :("); +// } +// +// public static void printTaskCount(){ +// updateTaskCount(); +// System.out.println(INDENT + "Now you have " + taskCount + " tasks in the list."); +// } +// +// public static void addTask(Task t){ +// taskList.add(t); +// System.out.println(INDENT+"Got it. I've added this task:"); +// System.out.println(INDENT+t); +// printTaskCount(); +// +// } +// +// private static void greet(){ +// printLine(); +// System.out.println(INDENT + "Hi, I am XiaoAi TongXue ;D"); +// printLine(); +// } +// +// private static String getInput(){ +// String input = sc.nextLine().trim(); +// return input; +// } +// +// private static String getAction(String input){ +// String action = input.split(SPACE)[0].toLowerCase(); +// return action; +// } +// +// private static void updateTask(int idx, boolean isMark){ +// if(isMark){ +// System.out.println(INDENT+"Nice! I have marked this task as done:"); +// taskList.get(idx).markAsDone(); +// }else{ +// System.out.println(INDENT+"Nice! I have marked this task as done:"); +// taskList.get(idx).markAsUndone(); // } -// } catch (IOException e) { -// e.printStackTrace(); +// printTask(taskList.get(idx)); // } -} - - -// -// /** -// * This method is to load orders from external files -// * @param filename -// * specifies where the external files stored -// * @return all reservations read from the file -// */ -// @Override -// public ArrayList load(String filename) { -// ArrayList stringArray = (ArrayList) read(filename); -// ArrayList alr = new ArrayList(); -// -// for (int i = 0; i < stringArray.size(); i++) { -// String st = (String) stringArray.get(i); -// StringTokenizer star = new StringTokenizer(st, "|"); -// -// int orderId = Integer.parseInt(star.nextToken().trim()); -// int staffId = Integer.parseInt(star.nextToken().trim()); -// int tableId = Integer.parseInt(star.nextToken().trim()); -// int numberOfPax = Integer.parseInt(star.nextToken().trim()); -// int orderSize = Integer.parseInt(star.nextToken().trim()); // write the orderSize in the file in order to read different size of order items -// boolean isActive = Boolean.parseBoolean(star.nextToken().trim()); -// -// //create order with no order item -// Order order = new Order(orderId, staffId, tableId, numberOfPax, isActive); -// //add order item in order -// for (int j = 0; j < orderSize; j++) { -// int itemId = Integer.parseInt(star.nextToken().trim()); -// String name = star.nextToken().trim(); -// int quantity = Integer.parseInt(star.nextToken().trim()); -// double price = Double.parseDouble(star.nextToken().trim()); -// order.addOrderItem(itemId, quantity, name, price); -// } -// //add order to order list -// alr.add(order); +// +// private static void printTask(Task task){ +// System.out.println(INDENT + task); +// } +// +// private static void addTaskByMessage(String action, String input){ +// String[] inputWord = input.split(SPACE); +// if(inputWord.length == 1){ +// System.out.println(INDENT + "OOPS! The task doesn't have description :("); // check description +// return; // } -// return alr; +// int desIdx = input.indexOf(" ")+1; +// Task newTask = null; +// switch(action){ +// case "todo": +// String description = input.substring(desIdx); +// newTask = new Todo(description); +// break; +// case "deadline": +// case "event": +// if(!input.contains("/")){ +// System.out.println(INDENT + "OOPS! the task doesn't have time :("); // check time +// return; +// } // +// int timeIdx = input.indexOf("/")+4; +// int desEndIdx = input.indexOf("/")-1; +// description = input.substring(desIdx,desEndIdx); +// String time = input.substring(timeIdx); +// if(action.equals("deadline")){ +// newTask = new Deadline(description,time); +// }else{ +// newTask = new Event(description,time); +// } +// break; +// } +// addTask(newTask); // } - - // -//public class Cup{ -// private T item; -// public void set(T item){ -// this.item = item; +//// private static void retrieveData() { +//// File f = new File(DIR); +//// if(f.exists()){ +//// taskList = load(f); +//// }else{ +//// f.getParentFile().mkdir(); +//// f.createNewFile(); +//// taskList = new ArrayList(); +//// save(DIR, taskList); +//// } +//// } +// +// private static void save(String filename, ArrayList al){ +// StringBuilder sb = new StringBuilder(); +// List newList = new ArrayList(); +// for(int i = 0; i < al.size(); i++){ +// Task task = al.get(i); +// if(task == null) break; +// String taskType = task.getTypeIcon(); +// String taskStatus = task.getStatusIcon(); +// String taskDetails = task.getDescription(); +// sb.append(taskType); +// sb.append(FILE_SEPARATOR); +// sb.append(taskStatus); +// sb.append(FILE_SEPARATOR); +// sb.append(taskDetails); +// if(taskType.equals("D") || taskType.equals("E")){ +// sb.append(FILE_SEPARATOR); +// sb.append(task.getTime()); +// } +// sb.append(System.lineSeparator()); +// } +// try { +// writeToFile(filename, sb.toString()); +// } catch (IOException e) { +// System.out.println("Something went wrong:" + e.getMessage()); +// } // } -// public T get(){ -// return item; +// +// public static void writeToFile(String fileName, String data) throws IOException { +// FileWriter fw = new FileWriter(fileName); +// fw.write(data); +// fw.close(); // } -//} \ No newline at end of file +// +// +//// +//// public static List read(String filename) throws IOException{ +//// File f = new File(filename); +//// Scanner s = new Scanner(f); +//// while(s.hasNext()) { +//// String newLine = s.nextLine(); +//// Task newTask; +//// StringTokenizer st = new StringTokenizer(newLine, FILE_SEPARATOR); +//// +//// String taskType = st.nextToken(); +//// String taskStatus = st.nextToken(); +//// String description = st.nextToken(); +//// if(taskType.equals("D")) { +//// String time = st.nextToken(); +//// newTask = new Deadline(description,time); +//// newTask. +//// } +//// +//// +//// +//// } +//// try{ +//// List data = new ArrayList(); +//// Scanner scanner = new Scanner(new FileInputStream(filename)); +//// try { +//// while (scanner.hasNextLine()) { +//// data.add(scanner.nextLine()); +//// } +//// } finally { +//// scanner.close(); +//// } +//// return data; +//// }catch(IOException e){ +//// System.out.println("reading file unsuccessfully"); +//// e.printStackTrace(); +//// return null; +//// } +//// } +// +// +// +// +//// try { +//// File file = new File(dir); +//// if (file.exists()) { +//// orders = load(dir); +//// } else { +//// file.getParentFile().mkdir(); +//// file.createNewFile(); +//// orders = new ArrayList(); +//// save(dir, orders); +//// } +//// } catch (IOException e) { +//// e.printStackTrace(); +//// } +//} +// +// +//// +//// /** +//// * This method is to load orders from external files +//// * @param filename +//// * specifies where the external files stored +//// * @return all reservations read from the file +//// */ +//// @Override +//// public ArrayList load(String filename) { +//// ArrayList stringArray = (ArrayList) read(filename); +//// ArrayList alr = new ArrayList(); +//// +//// for (int i = 0; i < stringArray.size(); i++) { +//// String st = (String) stringArray.get(i); +//// StringTokenizer star = new StringTokenizer(st, "|"); +//// +//// int orderId = Integer.parseInt(star.nextToken().trim()); +//// int staffId = Integer.parseInt(star.nextToken().trim()); +//// int tableId = Integer.parseInt(star.nextToken().trim()); +//// int numberOfPax = Integer.parseInt(star.nextToken().trim()); +//// int orderSize = Integer.parseInt(star.nextToken().trim()); // write the orderSize in the file in order to read different size of order items +//// boolean isActive = Boolean.parseBoolean(star.nextToken().trim()); +//// +//// //create order with no order item +//// Order order = new Order(orderId, staffId, tableId, numberOfPax, isActive); +//// //add order item in order +//// for (int j = 0; j < orderSize; j++) { +//// int itemId = Integer.parseInt(star.nextToken().trim()); +//// String name = star.nextToken().trim(); +//// int quantity = Integer.parseInt(star.nextToken().trim()); +//// double price = Double.parseDouble(star.nextToken().trim()); +//// order.addOrderItem(itemId, quantity, name, price); +//// } +//// //add order to order list +//// alr.add(order); +//// } +//// return alr; +//// +//// } +// +// +//// +////public class Cup{ +//// private T item; +//// public void set(T item){ +//// this.item = item; +//// } +//// public T get(){ +//// return item; +//// } +////} \ No newline at end of file diff --git a/src/main/java/controller/UI.java b/src/main/java/controller/UI.java new file mode 100644 index 000000000..163054ea7 --- /dev/null +++ b/src/main/java/controller/UI.java @@ -0,0 +1,82 @@ +package controller; + +import java.util.Scanner; + +public class UI { + private static final String INDENT = " "; + private static final String LINE="-------------------------------------------"; + public static final String SPACE = " "; + private static final int MAX_TASK_COUNT = 100; + private static final String DIR = "data/task-file"; + private static final String FILE_SEPARATOR = " | "; + private Scanner sc = new Scanner(System.in); + public UI(){ + + } + public void showWelcome(){ + greet(); + } + + private void greet(){ + printLine(); + System.out.println(INDENT + "Hi, I am XiaoAi TongXue ;D"); + printLine(); + } + + private void printLine(){ + System.out.println(INDENT + LINE); + } + + public String readCommand(){ + String input = getInput(); + return input; + } + + private String getInput(){ + String input = sc.nextLine().trim(); + return input; + } + + public void showLine(){ + printLine(); + } + + public void showError(String s){ + System.out.println(INDENT + s); + } + + public void showLoadingError(){ + System.out.println(INDENT + "oops! cannot find file!"); + } + + public void showList() { + System.out.println(INDENT+"Here are the tasks in your list:"); + } + + public void showDelete() { + System.out.println(INDENT+"Noted. I've removed this task:"); + } + + public void showTaskCount(int taskCount){ + System.out.println(INDENT + "Now you have " + taskCount + " tasks in the list."); + } + + public void showAdd(){ + System.out.println(INDENT+"Got it. I've added this task:"); + // System.out.println(INDENT+t); + } + + public void showErrorTask() { + System.out.println(INDENT + "OOPS! the task doesn't have description or time :("); + } + + public void showTask(String t) { + System.out.println(INDENT + t); + } + + public void showBye() { + System.out.println(INDENT + "Bye. Hope to see you again soon!"); + printLine(); + } +} + diff --git a/src/main/java/task/Task.java b/src/main/java/task/Task.java index 23aab17ef..d10bfadbd 100644 --- a/src/main/java/task/Task.java +++ b/src/main/java/task/Task.java @@ -10,7 +10,7 @@ public Task(String description){ } public String getStatusIcon(){ - return (isDone ? "X" : " "); + return (isDone ? "X" : "0"); } public String getDescription() { From 0aece75e9226da4c6cc6e811ad77310b803f16ca Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Fri, 4 Mar 2022 16:19:36 +0800 Subject: [PATCH 14/23] add find function --- src/main/java/command/FindCommand.java | 41 ++++++++++++++++++++++++++ src/main/java/controller/Parser.java | 6 ++-- src/main/java/controller/UI.java | 8 +++++ 3 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/main/java/command/FindCommand.java diff --git a/src/main/java/command/FindCommand.java b/src/main/java/command/FindCommand.java new file mode 100644 index 000000000..cf8f4e1a5 --- /dev/null +++ b/src/main/java/command/FindCommand.java @@ -0,0 +1,41 @@ +package command; + +import controller.Storage; +import controller.TaskList; +import controller.UI; +import task.Task; + +import javax.naming.directory.InitialDirContext; +import java.util.ArrayList; + +public class FindCommand extends Command{ + private static final String INDENT = " "; + private String text; + public FindCommand(String fullCommand) { + int textIdx = 1 + fullCommand.indexOf(" "); + text = fullCommand.substring(textIdx); + } + public void execute(TaskList tasks, UI ui, Storage storage){ + ArrayList targetList = new ArrayList<>(); + for(Task task : tasks.getTaskList()){ + if(task.toString().contains(text)){ + targetList.add(task); + } + } + + if(targetList.size() ==0){ + ui.showNotFound(); + return; + } + ui.showFound(); + int i = 1; + for(Task task : targetList){ + System.out.println(INDENT + (i++)+ "." + task.toString()); + } + } + + public boolean isExit(){ + return false; + } + +} diff --git a/src/main/java/controller/Parser.java b/src/main/java/controller/Parser.java index 5fd03ce09..8bc54b94b 100644 --- a/src/main/java/controller/Parser.java +++ b/src/main/java/controller/Parser.java @@ -33,6 +33,8 @@ public static Command parse(String fullCommand) throws DukeException { //deleteTask(Integer.parseInt(input.split(SPACE)[1])-1); case "bye": return new ExitCommand(); + case "find": + return new FindCommand(fullCommand); default: throw new DukeException(); } @@ -43,8 +45,4 @@ private static String getAction(String input){ return action; } - private static String getDescription(String input) { - int desIdx = input.indexOf(" ")+1; - return input.substring(desIdx); - } } diff --git a/src/main/java/controller/UI.java b/src/main/java/controller/UI.java index 163054ea7..3b97b3edd 100644 --- a/src/main/java/controller/UI.java +++ b/src/main/java/controller/UI.java @@ -78,5 +78,13 @@ public void showBye() { System.out.println(INDENT + "Bye. Hope to see you again soon!"); printLine(); } + + public void showFound() { + System.out.println(INDENT + "Here are the matching tasks in your list:"); + } + + public void showNotFound() { + System.out.println(INDENT + "Opps, no matching tasks found in your list :("); + } } From ce78e719305acde47a9bbc47c62783033833df94 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Fri, 4 Mar 2022 16:44:56 +0800 Subject: [PATCH 15/23] add java doc --- src/main/java/command/AddCommand.java | 9 ++++++++ src/main/java/command/DeleteCommand.java | 9 ++++++++ src/main/java/command/ExitCommand.java | 7 +++++++ src/main/java/command/ListCommand.java | 6 ++++++ src/main/java/command/UpdateCommand.java | 9 ++++++++ src/main/java/controller/Parser.java | 10 +++++++++ src/main/java/controller/TaskList.java | 26 +++++++++++++++++++++++- 7 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/main/java/command/AddCommand.java b/src/main/java/command/AddCommand.java index 846ebd6f0..dfa24933c 100644 --- a/src/main/java/command/AddCommand.java +++ b/src/main/java/command/AddCommand.java @@ -16,6 +16,15 @@ public AddCommand(String action, String input) { this.action =action; } + /** + * Add specific task into task list according to the input + * Show task count + * Save the task list data into file + * @param tasks task list + * @param ui user interface + * @param storage storage. + */ + public void execute(TaskList tasks, UI ui, Storage storage){ String t = tasks.addTask(action,input); if(t == null){ diff --git a/src/main/java/command/DeleteCommand.java b/src/main/java/command/DeleteCommand.java index 8ac0b39e4..cd2a28d43 100644 --- a/src/main/java/command/DeleteCommand.java +++ b/src/main/java/command/DeleteCommand.java @@ -11,6 +11,15 @@ public DeleteCommand(int i) { idx = i; } + /** + * Remove task from task list according to the input + * Show task count + * Save the task list data into file + * @param tasks task list + * @param ui user interface + * @param storage storage. + */ + public void execute(TaskList tasks, UI ui, Storage storage){ ui.showDelete(); tasks.removeTaskByIdx(idx); diff --git a/src/main/java/command/ExitCommand.java b/src/main/java/command/ExitCommand.java index 6465b7bed..7b6212c76 100644 --- a/src/main/java/command/ExitCommand.java +++ b/src/main/java/command/ExitCommand.java @@ -5,6 +5,13 @@ import controller.UI; public class ExitCommand extends Command{ + /** + * Show Bye message. + * Save the task list data into file. + * @param tasks task list. + * @param ui user interface. + * @param storage storage. + */ public void execute(TaskList tasks, UI ui, Storage storage){ ui.showBye(); storage.save(tasks.getTaskList()); diff --git a/src/main/java/command/ListCommand.java b/src/main/java/command/ListCommand.java index 015afa051..0b23caf2a 100644 --- a/src/main/java/command/ListCommand.java +++ b/src/main/java/command/ListCommand.java @@ -8,6 +8,12 @@ public class ListCommand extends Command { private static final String INDENT = " "; + /** + * Print out all task in the task list. + * @param taskList task list. + * @param ui user interface. + * @param storage storage. + */ public void execute(TaskList taskList, UI ui, Storage storage){ ui.showList(); for(int i = 0; i(); } - + /** + * Return total task count in the task list + * @return total task count + */ public int getCount () { return taskList.size(); } + /** + * Return task of corresponding index. + * @param i the index of the target task. + * @return task of corresponding index. + */ public Task getTaskByIdx ( int i){ return taskList.get(i); } + /** + * Remove corresponding task by index + * @param i the index of target task. + * @return total task count + */ public void removeTaskByIdx ( int i){ taskList.remove(i); } + /** + * Return all tasks in the list + * @return arraylist of tasks + */ public ArrayList getTaskList () { return taskList; } + /** + * Classify task type. + * Add corresponding task into list according to input + * @param input user input + * @param type task type + * @return task details + */ public String addTask (String type, String input){ String[] inputWord = input.split(SPACE); if (inputWord.length == 1) { From 0b0a212bc1701ffe7f6c28947e4ce582178d454a Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Fri, 4 Mar 2022 17:10:47 +0800 Subject: [PATCH 16/23] delete taskmanager --- src/main/java/controller/TaskManager.java | 332 ---------------------- 1 file changed, 332 deletions(-) delete mode 100644 src/main/java/controller/TaskManager.java diff --git a/src/main/java/controller/TaskManager.java b/src/main/java/controller/TaskManager.java deleted file mode 100644 index 30d3dcce9..000000000 --- a/src/main/java/controller/TaskManager.java +++ /dev/null @@ -1,332 +0,0 @@ -//package controller; -// -//import task.Deadline; -//import task.Event; -//import task.Task; -//import task.Todo; -// -// -//import java.util.ArrayList; -//import java.util.Scanner; -//import java.util.List; -//import java.io.IOException; -//import java.io.FileWriter; -// -// -//public class TaskManager { -// private static final String INDENT = " "; -// private static final String LINE="-------------------------------------------"; -// public static final String SPACE = " "; -// private static final int MAX_TASK_COUNT = 100; -// private static final String DIR = "data/task-file"; -// private static final String FILE_SEPARATOR = " | "; -// -// -// private static int taskCount=0; -// -// private static Scanner sc = new Scanner(System.in); -// -// -// public static void main(String[] args) { -// UI ui = new UI(); -// -// greet(); -// String input = getInput(); -// String action = getAction(input); -// printLine(); -// -// while(!action.equals("bye")){ -// switch(action){ -// case "list": -// listTask(); -// break; -// case "mark": -// updateTask(Integer.parseInt(input.split(SPACE)[1])-1,true); //refractor get index -// break; -// case "unmark": -// updateTask(Integer.parseInt(input.split(SPACE)[1])-1,false); -// break; -// case "todo": -// case "deadline": -// case "event": -// addTaskByMessage(action, input); -// break; -// case "delete": -// deleteTask(Integer.parseInt(input.split(SPACE)[1])-1); -// break; -// default: -// printErrorInputMessage(); -// } -// printLine(); -// input = getInput(); -// action = getAction(input); -// printLine(); -// } -// bye(); -// } -// -// private static void deleteTask(int idx){ -// System.out.println(INDENT+"Noted. I've removed this task:"); -// printTask(taskList.get(idx)); -// taskList.remove(idx); -// printTaskCount(); -// } -// -// private static void updateTaskCount(){ -// taskCount = taskList.size(); -// } -// -// private static void bye() { -// save(DIR, taskList); -// System.out.println(INDENT + "Bye. Hope to see you again soon!"); -// printLine(); -// } -// -// private static void listTask() { -// System.out.println(INDENT+"Here are the tasks in your list:"); -// for(int i =0 ; i(); -//// save(DIR, taskList); -//// } -//// } -// -// private static void save(String filename, ArrayList al){ -// StringBuilder sb = new StringBuilder(); -// List newList = new ArrayList(); -// for(int i = 0; i < al.size(); i++){ -// Task task = al.get(i); -// if(task == null) break; -// String taskType = task.getTypeIcon(); -// String taskStatus = task.getStatusIcon(); -// String taskDetails = task.getDescription(); -// sb.append(taskType); -// sb.append(FILE_SEPARATOR); -// sb.append(taskStatus); -// sb.append(FILE_SEPARATOR); -// sb.append(taskDetails); -// if(taskType.equals("D") || taskType.equals("E")){ -// sb.append(FILE_SEPARATOR); -// sb.append(task.getTime()); -// } -// sb.append(System.lineSeparator()); -// } -// try { -// writeToFile(filename, sb.toString()); -// } catch (IOException e) { -// System.out.println("Something went wrong:" + e.getMessage()); -// } -// } -// -// public static void writeToFile(String fileName, String data) throws IOException { -// FileWriter fw = new FileWriter(fileName); -// fw.write(data); -// fw.close(); -// } -// -// -//// -//// public static List read(String filename) throws IOException{ -//// File f = new File(filename); -//// Scanner s = new Scanner(f); -//// while(s.hasNext()) { -//// String newLine = s.nextLine(); -//// Task newTask; -//// StringTokenizer st = new StringTokenizer(newLine, FILE_SEPARATOR); -//// -//// String taskType = st.nextToken(); -//// String taskStatus = st.nextToken(); -//// String description = st.nextToken(); -//// if(taskType.equals("D")) { -//// String time = st.nextToken(); -//// newTask = new Deadline(description,time); -//// newTask. -//// } -//// -//// -//// -//// } -//// try{ -//// List data = new ArrayList(); -//// Scanner scanner = new Scanner(new FileInputStream(filename)); -//// try { -//// while (scanner.hasNextLine()) { -//// data.add(scanner.nextLine()); -//// } -//// } finally { -//// scanner.close(); -//// } -//// return data; -//// }catch(IOException e){ -//// System.out.println("reading file unsuccessfully"); -//// e.printStackTrace(); -//// return null; -//// } -//// } -// -// -// -// -//// try { -//// File file = new File(dir); -//// if (file.exists()) { -//// orders = load(dir); -//// } else { -//// file.getParentFile().mkdir(); -//// file.createNewFile(); -//// orders = new ArrayList(); -//// save(dir, orders); -//// } -//// } catch (IOException e) { -//// e.printStackTrace(); -//// } -//} -// -// -//// -//// /** -//// * This method is to load orders from external files -//// * @param filename -//// * specifies where the external files stored -//// * @return all reservations read from the file -//// */ -//// @Override -//// public ArrayList load(String filename) { -//// ArrayList stringArray = (ArrayList) read(filename); -//// ArrayList alr = new ArrayList(); -//// -//// for (int i = 0; i < stringArray.size(); i++) { -//// String st = (String) stringArray.get(i); -//// StringTokenizer star = new StringTokenizer(st, "|"); -//// -//// int orderId = Integer.parseInt(star.nextToken().trim()); -//// int staffId = Integer.parseInt(star.nextToken().trim()); -//// int tableId = Integer.parseInt(star.nextToken().trim()); -//// int numberOfPax = Integer.parseInt(star.nextToken().trim()); -//// int orderSize = Integer.parseInt(star.nextToken().trim()); // write the orderSize in the file in order to read different size of order items -//// boolean isActive = Boolean.parseBoolean(star.nextToken().trim()); -//// -//// //create order with no order item -//// Order order = new Order(orderId, staffId, tableId, numberOfPax, isActive); -//// //add order item in order -//// for (int j = 0; j < orderSize; j++) { -//// int itemId = Integer.parseInt(star.nextToken().trim()); -//// String name = star.nextToken().trim(); -//// int quantity = Integer.parseInt(star.nextToken().trim()); -//// double price = Double.parseDouble(star.nextToken().trim()); -//// order.addOrderItem(itemId, quantity, name, price); -//// } -//// //add order to order list -//// alr.add(order); -//// } -//// return alr; -//// -//// } -// -// -//// -////public class Cup{ -//// private T item; -//// public void set(T item){ -//// this.item = item; -//// } -//// public T get(){ -//// return item; -//// } -////} \ No newline at end of file From c4a2a3aeb0ad12074c7da6a95138f6f68acd59c6 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+XunyiiZ@users.noreply.github.com> Date: Fri, 4 Mar 2022 17:19:51 +0800 Subject: [PATCH 17/23] Update README.md --- README.md | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8715d4d91..f76aff5e4 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,23 @@ -# Duke project template +# Smart XiaoAi TongXue -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. +This is a task management Java project. It's named after the Smart XiaoAi TongXue. Given below are instructions on how to use it. -## Setting up in Intellij +## list +list all tasks in the task list +## todo {task description} +add Todo task +## deadline {task description} /by {time} +add Deadline +## Event {task description} /at {time} +add Event +## mark {index} +mark task +## unmark {index} +unmark task +## delete {index} +delete task from list +## find {text} +find matching tasks containing the {text} +## bye +exit the program -Prerequisites: JDK 11, update Intellij to the most recent version. - -1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) -1. Open the project into Intellij as follows: - 1. Click `Open`. - 1. Select the project directory, and click `OK`. - 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: - ``` - Hello from - ____ _ - | _ \ _ _| | _____ - | | | | | | | |/ / _ \ - | |_| | |_| | < __/ - |____/ \__,_|_|\_\___| - ``` From 61a21f21c14743e8183710204f894f834f418c0b Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Fri, 4 Mar 2022 17:46:39 +0800 Subject: [PATCH 18/23] add userguide --- docs/README.md | 38 +++++++++++++++++------------- src/main/java/META-INF/MANIFEST.MF | 2 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8077118eb..3bf1ec0b9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,29 +1,35 @@ # User Guide - +Smart XiaoAI is a task management app use via a Command Line Interface(CLI). ## Features -### Feature-ABC - -Description of the feature. +### Feature- list all task -### Feature-XYZ +'list' +list all tasks -Description of the feature. +### Feature- Add a todo task +'todo {description}' -## Usage +### Feature- Add a deadline task +'deadline {description} /by {time}' -### `Keyword` - Describe action +### Feature- Add a event task +'event {description} /at {time}' -Describe the action and its outcome. +### Feature- Mark a task as done +'mark {index}' -Example of usage: +### Feature- Unmark a task +'unmark {index}' -`keyword (optional arguments)` +### Feature- Unmark a task +'unmark {index}' -Expected outcome: +### Feature- remove a task +'delete {index}' -Description of the outcome. +### Feature- Find matching tasks +'find {text}' -``` -expected output -``` +### Feature- Exit program +'bye' diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF index bbf9ca08a..fea33e730 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: controller.TaskManager +Main-Class: controller.Duke From 29b3817144118de1f5eb4d214ecbac02ea08870c Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Sun, 20 Mar 2022 12:47:51 +0800 Subject: [PATCH 19/23] fix program --- src/main/java/command/AddCommand.java | 58 +++++++++++++++-- src/main/java/command/Command.java | 4 +- src/main/java/command/DeleteCommand.java | 20 ++++-- src/main/java/command/ListCommand.java | 4 ++ src/main/java/command/UpdateCommand.java | 22 ++++--- src/main/java/controller/Duke.java | 6 +- src/main/java/controller/DukeException.java | 10 --- src/main/java/controller/Helper.java | 23 +++++++ src/main/java/controller/Parser.java | 10 +-- src/main/java/controller/Storage.java | 5 +- src/main/java/controller/TaskList.java | 64 ++++++------------- src/main/java/controller/UI.java | 9 ++- src/main/java/exception/DukeException.java | 9 +++ .../exception/InvalidCommandException.java | 9 +++ .../java/exception/InvalidIndexException.java | 9 +++ .../exception/NoDescriptionException.java | 9 +++ .../exception/NoTaskIndexFoundException.java | 10 +++ src/main/java/exception/NoTimeException.java | 9 +++ 18 files changed, 205 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/controller/DukeException.java create mode 100644 src/main/java/controller/Helper.java create mode 100644 src/main/java/exception/DukeException.java create mode 100644 src/main/java/exception/InvalidCommandException.java create mode 100644 src/main/java/exception/InvalidIndexException.java create mode 100644 src/main/java/exception/NoDescriptionException.java create mode 100644 src/main/java/exception/NoTaskIndexFoundException.java create mode 100644 src/main/java/exception/NoTimeException.java diff --git a/src/main/java/command/AddCommand.java b/src/main/java/command/AddCommand.java index dfa24933c..190e250d7 100644 --- a/src/main/java/command/AddCommand.java +++ b/src/main/java/command/AddCommand.java @@ -3,6 +3,10 @@ import controller.Storage; import controller.TaskList; import controller.UI; +import exception.DukeException; +import exception.InvalidCommandException; +import exception.NoDescriptionException; +import exception.NoTimeException; import task.Task; import task.Todo; @@ -25,18 +29,60 @@ public AddCommand(String action, String input) { * @param storage storage. */ - public void execute(TaskList tasks, UI ui, Storage storage){ - String t = tasks.addTask(action,input); - if(t == null){ - ui.showErrorTask(); - return; + public void execute(TaskList tasks, UI ui, Storage storage) throws DukeException { + String description = ""; + String time = ""; + String[] info = new String[2]; + switch (action){ + case "todo": + description = input.replace("todo", "").trim(); + if(description == null || input.isEmpty()){ + throw new NoDescriptionException(); + } + tasks.addNewTodo(description); + break; + case "event": + info = getInfo("event"); + description = info[0]; + time = info[1]; + tasks.addNewEvent(description,time); + break; + case "deadline": + info = getInfo("deadline"); + description = info[0]; + time = info[1]; + tasks.addNewDeadline(description,time); + break; + default: + throw new InvalidCommandException(); } ui.showAdd(); - ui.showTask(t); + ui.showTask(tasks.getNewAdd()); ui.showTaskCount(tasks.getCount()); storage.save(tasks.getTaskList()); } + private String[] getInfo(String action) throws DukeException { + int descriptionIdx = input.indexOf(" "); + if(descriptionIdx == -1){ + throw new NoDescriptionException(); + } + int timeIdx = 0; + if(action.equals("event")) { + timeIdx = input.indexOf("/at"); + }else if(action.equals("deadline")){ + timeIdx = input.indexOf("/by"); + } + if(timeIdx == -1){ + throw new NoTimeException(); + } + String description = input.substring(descriptionIdx+1, timeIdx).trim(); + String time = input.substring(timeIdx+4).trim(); + String[] res = new String[2]; + res[0] = description; + res[1] = time; + return res; + } public boolean isExit(){ return false; } diff --git a/src/main/java/command/Command.java b/src/main/java/command/Command.java index 15e3ce31b..cf0fe233f 100644 --- a/src/main/java/command/Command.java +++ b/src/main/java/command/Command.java @@ -3,8 +3,10 @@ import controller.Storage; import controller.TaskList; import controller.UI; +import exception.DukeException; +import exception.NoDescriptionException; public abstract class Command { - public abstract void execute(TaskList tasks, UI ui, Storage storage); + public abstract void execute(TaskList tasks, UI ui, Storage storage) throws NoDescriptionException, DukeException; public abstract boolean isExit(); } diff --git a/src/main/java/command/DeleteCommand.java b/src/main/java/command/DeleteCommand.java index cd2a28d43..3dce98b93 100644 --- a/src/main/java/command/DeleteCommand.java +++ b/src/main/java/command/DeleteCommand.java @@ -1,14 +1,16 @@ package command; +import controller.Helper; import controller.Storage; import controller.TaskList; import controller.UI; +import exception.DukeException; public class DeleteCommand extends Command{ - int idx; - public DeleteCommand(int i) { - super(); - idx = i; + private static final String INDENT = " "; + String command; + public DeleteCommand(String command) { + this.command=command; } /** @@ -20,10 +22,16 @@ public DeleteCommand(int i) { * @param storage storage. */ - public void execute(TaskList tasks, UI ui, Storage storage){ + public void execute(TaskList tasks, UI ui, Storage storage) throws DukeException { + String idxInString = command.replace("delete","").trim(); + Helper.checkIndex(idxInString,tasks); + int idx = Integer.parseInt(idxInString)-1; ui.showDelete(); - tasks.removeTaskByIdx(idx); + ui.showTask(tasks.removeTaskByIdx(idx)); ui.showList(); + for(int i = 0; i= tasks.getCount() || idx < 0){ + throw new InvalidIndexException(); + } + } +} diff --git a/src/main/java/controller/Parser.java b/src/main/java/controller/Parser.java index 63bd2bc08..6080b0c54 100644 --- a/src/main/java/controller/Parser.java +++ b/src/main/java/controller/Parser.java @@ -1,6 +1,8 @@ package controller; import command.*; +import exception.DukeException; +import exception.InvalidCommandException; //import command.MarkCommand; @@ -27,10 +29,10 @@ public static Command parse(String fullCommand) throws DukeException { case "list": return new ListCommand(); case "mark": - return new UpdateCommand("mark",Integer.parseInt(fullCommand.split(SPACE)[1])-1); + return new UpdateCommand("mark",fullCommand); //updateTask(Integer.parseInt(input.split(SPACE)[1])-1,true); //refractor get index case "unmark": - return new UpdateCommand("unmark",Integer.parseInt(fullCommand.split(SPACE)[1])-1); + return new UpdateCommand("unmark",fullCommand); //updateTask(Integer.parseInt(input.split(SPACE)[1])-1,false); case "todo": @@ -39,14 +41,14 @@ public static Command parse(String fullCommand) throws DukeException { case "event": return new AddCommand(action, fullCommand); case "delete": - return new DeleteCommand(Integer.parseInt(fullCommand.split(SPACE)[1])-1); + return new DeleteCommand(fullCommand); //deleteTask(Integer.parseInt(input.split(SPACE)[1])-1); case "bye": return new ExitCommand(); case "find": return new FindCommand(fullCommand); default: - throw new DukeException(); + throw new InvalidCommandException(); } } diff --git a/src/main/java/controller/Storage.java b/src/main/java/controller/Storage.java index 3f7f42cf8..cb45461bf 100644 --- a/src/main/java/controller/Storage.java +++ b/src/main/java/controller/Storage.java @@ -53,9 +53,10 @@ private void writeToFile(String data) throws IOException { fw.close(); } - public String load() throws java.io.FileNotFoundException { + public String load() throws IOException { StringBuilder sb = new StringBuilder(); - File f = new File(filePath); // create a File for the given file path + File f = new File(filePath); + f.createNewFile(); // if the file doesn't exist, it will create one Scanner s = new Scanner(f); // create a Scanner using the File as the source while (s.hasNext()) { sb.append(s.nextLine()); diff --git a/src/main/java/controller/TaskList.java b/src/main/java/controller/TaskList.java index 283aed265..40143e2f0 100644 --- a/src/main/java/controller/TaskList.java +++ b/src/main/java/controller/TaskList.java @@ -11,13 +11,12 @@ public class TaskList { private static final String FILE_SEPARATOR = " | "; private static final String INDENT = " "; - private static final String LINE = "-------------------------------------------"; public static final String SPACE = " "; private static ArrayList taskList = new ArrayList<>(); public TaskList(String data) { + if(data == null || data.isEmpty()) return; String[] taskStrList = data.split(System.lineSeparator()); - System.out.println("string list length: " + taskStrList.length); for (int i = 0; i < taskStrList.length; i++) { String newLine = taskStrList[i]; Task newTask; @@ -66,8 +65,10 @@ public Task getTaskByIdx ( int i){ * @param i the index of target task. * @return total task count */ - public void removeTaskByIdx ( int i){ + public String removeTaskByIdx ( int i){ + String task = taskList.get(i).toString(); taskList.remove(i); + return task; } /** @@ -78,48 +79,6 @@ public ArrayList getTaskList () { return taskList; } - /** - * Classify task type. - * Add corresponding task into list according to input - * @param input user input - * @param type task type - * @return task details - */ - public String addTask (String type, String input){ - String[] inputWord = input.split(SPACE); - if (inputWord.length == 1) { - System.out.println(INDENT + "OOPS! The task doesn't have description :("); // check description - return null; - } - int desIdx = input.indexOf(" ") + 1; - Task newTask = null; - switch (type) { - case "todo": - String description = input.substring(desIdx); - newTask = new Todo(description); - break; - case "deadline": - case "event": - if (!input.contains("/")) { - // check time - return null; - } - - int timeIdx = input.indexOf("/") + 4; - int desEndIdx = input.indexOf("/") - 1; - description = input.substring(desIdx, desEndIdx); - String time = input.substring(timeIdx); - if (type.equals("deadline")) { - newTask = new Deadline(description, time); - } else { - newTask = new Event(description, time); - } - break; - } - taskList.add(newTask); - return newTask.toString(); - } - public String markTaskByIdx(int i){ taskList.get(i).markAsDone(); return taskList.get(i).toString(); @@ -130,6 +89,21 @@ public String unmarkTaskByIdx(int i){ return taskList.get(i).toString(); } + public void addNewTodo(String description) { + taskList.add(new Todo(description)); + } + + public void addNewEvent(String description, String time) { + taskList.add(new Event(description,time)); + } + + public void addNewDeadline(String description, String time) { + taskList.add(new Deadline(description, time)); + } + + public String getNewAdd() { + return taskList.get(taskList.size()-1).toString(); + } } diff --git a/src/main/java/controller/UI.java b/src/main/java/controller/UI.java index 3b97b3edd..d96517013 100644 --- a/src/main/java/controller/UI.java +++ b/src/main/java/controller/UI.java @@ -20,6 +20,7 @@ public void showWelcome(){ private void greet(){ printLine(); System.out.println(INDENT + "Hi, I am XiaoAi TongXue ;D"); + System.out.println(INDENT + "What can I do for you?"); printLine(); } @@ -50,7 +51,7 @@ public void showLoadingError(){ } public void showList() { - System.out.println(INDENT+"Here are the tasks in your list:"); + System.out.println(INDENT+"Here are the task(s) in your list:"); } public void showDelete() { @@ -58,7 +59,7 @@ public void showDelete() { } public void showTaskCount(int taskCount){ - System.out.println(INDENT + "Now you have " + taskCount + " tasks in the list."); + System.out.println(INDENT + "Now you have " + taskCount + " task(s) in the list."); } public void showAdd(){ @@ -86,5 +87,9 @@ public void showFound() { public void showNotFound() { System.out.println(INDENT + "Opps, no matching tasks found in your list :("); } + + public void showEmptyList() { + System.out.println(INDENT + "There are nothing in the task list now"); + } } diff --git a/src/main/java/exception/DukeException.java b/src/main/java/exception/DukeException.java new file mode 100644 index 000000000..671fd58e4 --- /dev/null +++ b/src/main/java/exception/DukeException.java @@ -0,0 +1,9 @@ +package exception; + +public class DukeException extends Throwable{ + private static final String ERROR_MESSAGE = ""; + + public String getErrorMessage() { + return ERROR_MESSAGE; + } +} diff --git a/src/main/java/exception/InvalidCommandException.java b/src/main/java/exception/InvalidCommandException.java new file mode 100644 index 000000000..8adf98e56 --- /dev/null +++ b/src/main/java/exception/InvalidCommandException.java @@ -0,0 +1,9 @@ +package exception; + +public class InvalidCommandException extends DukeException{ + private static final String ERROR_MESSAGE = "Oops! I don't understand this command"; + + public String getErrorMessage() { + return ERROR_MESSAGE; + } +} diff --git a/src/main/java/exception/InvalidIndexException.java b/src/main/java/exception/InvalidIndexException.java new file mode 100644 index 000000000..db39f952e --- /dev/null +++ b/src/main/java/exception/InvalidIndexException.java @@ -0,0 +1,9 @@ +package exception; + +public class InvalidIndexException extends DukeException{ + private static final String ERROR_MESSAGE = "Oops! This is invalid index!"; + + public String getErrorMessage() { + return ERROR_MESSAGE; + } +} diff --git a/src/main/java/exception/NoDescriptionException.java b/src/main/java/exception/NoDescriptionException.java new file mode 100644 index 000000000..4bf100852 --- /dev/null +++ b/src/main/java/exception/NoDescriptionException.java @@ -0,0 +1,9 @@ +package exception; + +public class NoDescriptionException extends DukeException{ + private static final String ERROR_MESSAGE = "Oops! No Description Found!"; + + public String getErrorMessage() { + return ERROR_MESSAGE; + } +} diff --git a/src/main/java/exception/NoTaskIndexFoundException.java b/src/main/java/exception/NoTaskIndexFoundException.java new file mode 100644 index 000000000..ce51db61b --- /dev/null +++ b/src/main/java/exception/NoTaskIndexFoundException.java @@ -0,0 +1,10 @@ +package exception; + +public class NoTaskIndexFoundException extends DukeException{ + private static final String ERROR_MESSAGE = "Oops! No Task Index Found!"; + + public String getErrorMessage() { + return ERROR_MESSAGE; + } +} + diff --git a/src/main/java/exception/NoTimeException.java b/src/main/java/exception/NoTimeException.java new file mode 100644 index 000000000..0cc259546 --- /dev/null +++ b/src/main/java/exception/NoTimeException.java @@ -0,0 +1,9 @@ +package exception; + +public class NoTimeException extends DukeException{ + private static final String ERROR_MESSAGE = "Oops! No time information found!"; + + public String getErrorMessage() { + return ERROR_MESSAGE; + } +} From eb819e82c3477dbb041b8a7aa66698e59d9c99fd Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Sun, 20 Mar 2022 12:54:05 +0800 Subject: [PATCH 20/23] ug --- docs/README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 3bf1ec0b9..1d12755df 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,13 @@ # User Guide Smart XiaoAI is a task management app use via a Command Line Interface(CLI). -## Features + +---- +The file path: data/task-file + +---- + + +## Features ### Feature- list all task From a258d97fb67c5d969932e381dfc0ab85619c5859 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Mon, 21 Mar 2022 09:54:51 +0800 Subject: [PATCH 21/23] little changes --- data/task-file | 4 ---- src/main/java/controller/Duke.java | 32 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 20 deletions(-) delete mode 100644 data/task-file diff --git a/data/task-file b/data/task-file deleted file mode 100644 index a13a78936..000000000 --- a/data/task-file +++ /dev/null @@ -1,4 +0,0 @@ -T | X | task -T | 0 | task -E | X | project | today -T | 0 | new diff --git a/src/main/java/controller/Duke.java b/src/main/java/controller/Duke.java index eefe4c065..c6c4b91cb 100644 --- a/src/main/java/controller/Duke.java +++ b/src/main/java/controller/Duke.java @@ -27,23 +27,23 @@ public Duke(String filePath) { } - public void run() { - ui.showWelcome(); - boolean isExit = false; - while (!isExit) { - try { - String fullCommand = ui.readCommand(); - ui.showLine(); // show the divider line ("_______") - Command c = Parser.parse(fullCommand); // make parse as static method !!! - c.execute(tasks, ui, storage); - isExit = c.isExit(); - } catch (DukeException e) { - ui.showError(e.getErrorMessage()); - } finally { - ui.showLine(); - } + public void run() { + ui.showWelcome(); + boolean isExit = false; + while (!isExit) { + try { + String fullCommand = ui.readCommand(); + ui.showLine(); // show the divider line ("_______") + Command c = Parser.parse(fullCommand); // make parse as static method !!! + c.execute(tasks, ui, storage); + isExit = c.isExit(); + } catch (DukeException e) { + ui.showError(e.getErrorMessage()); + } finally { + ui.showLine(); } - return; + } + return; } From dc57be4bbb1ca83f6e02992a896fb19ef47d9b47 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Tue, 22 Mar 2022 14:02:37 +0800 Subject: [PATCH 22/23] change file location --- docs/README.md | 2 +- src/main/java/controller/Duke.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 1d12755df..fea907c7d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,7 +2,7 @@ Smart XiaoAI is a task management app use via a Command Line Interface(CLI). ---- -The file path: data/task-file +The file path: task-file ---- diff --git a/src/main/java/controller/Duke.java b/src/main/java/controller/Duke.java index c6c4b91cb..470eebcaf 100644 --- a/src/main/java/controller/Duke.java +++ b/src/main/java/controller/Duke.java @@ -48,6 +48,6 @@ public void run() { public static void main(String[] args) { - new Duke("data/task-file").run(); + new Duke("task-file").run(); } } \ No newline at end of file From 458cdd609ebd2e4959689209dc33db2649e96ca7 Mon Sep 17 00:00:00 2001 From: Xunyi <73570047+icthenic@users.noreply.github.com> Date: Tue, 22 Mar 2022 14:07:11 +0800 Subject: [PATCH 23/23] change file name to constant --- src/main/java/controller/Duke.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/controller/Duke.java b/src/main/java/controller/Duke.java index 470eebcaf..1e0b84f78 100644 --- a/src/main/java/controller/Duke.java +++ b/src/main/java/controller/Duke.java @@ -11,6 +11,7 @@ public class Duke { private Storage storage; private TaskList tasks; private UI ui; + private static final String FILE_PATH = "task-file"; public Duke(String filePath) { @@ -48,6 +49,6 @@ public void run() { public static void main(String[] args) { - new Duke("task-file").run(); + new Duke(FILE_PATH).run(); } } \ No newline at end of file