From 8e6490320272e4124c19e10165881471e309e74b Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Tue, 1 Sep 2020 16:00:00 +0800 Subject: [PATCH 01/12] Level-0 --- src/main/java/Duke.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334cc..282033429e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,10 +1,17 @@ public class Duke { public static void main(String[] args) { - String logo = " ____ _ \n" + /*String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); + */ + String greet = "____________________________________________________________\n" + + " Hello! I'm Duke\n" + + " What can I do for you?\n" + + "____________________________________________________________\n" + + " Bye. Hope to see you again soon!\n" + + "____________________________________________________________"; + System.out.println(greet); } } From 1466cf63b9b202021966482887fef218ac34b2da Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Tue, 1 Sep 2020 16:25:19 +0800 Subject: [PATCH 02/12] Level 1. Greet, Echo, Exit --- src/main/java/Duke.java | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 282033429e..a7b2254795 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,17 +1,35 @@ +import java.util.Scanner; + public class Duke { + public static void printHorizonLine(){ + System.out.println(" ____________________________________________________________"); + } + public static void printHello(){ + System.out.println(" Hello! I'm Duke"); + System.out.println(" What can I do for you?"); + } + public static void printGoodBye(){ + System.out.println(" Bye. Hope to see you again soon!"); + } public static void main(String[] args) { - /*String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - */ - String greet = "____________________________________________________________\n" - + " Hello! I'm Duke\n" - + " What can I do for you?\n" - + "____________________________________________________________\n" - + " Bye. Hope to see you again soon!\n" - + "____________________________________________________________"; - System.out.println(greet); + Scanner k = new Scanner(System.in); + String input; + printHorizonLine(); + printHello(); + printHorizonLine(); + System.out.println(); + while(true){ + input = k.nextLine(); + if(input.toLowerCase().equals("bye")){ + break; + } + printHorizonLine(); + System.out.println(" " + input); + printHorizonLine(); + System.out.println(); + } + printHorizonLine(); + printGoodBye(); + printHorizonLine(); } -} +} \ No newline at end of file From 1ca9f96015113da880238aa231a3a26dc3aa8990 Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Tue, 1 Sep 2020 16:57:46 +0800 Subject: [PATCH 03/12] Level 2. Add, List --- src/main/java/Duke.java | 34 ++++++++++++++++++----- src/main/java/Request.java | 49 ++++++++++++++++++++++++++++++++++ src/main/java/RequestList.java | 33 +++++++++++++++++++++++ 3 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 src/main/java/Request.java create mode 100644 src/main/java/RequestList.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a7b2254795..e57cb0f9e6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,32 +1,52 @@ import java.util.Scanner; public class Duke { + public static RequestList list = new RequestList(); + public static void printHorizonLine(){ System.out.println(" ____________________________________________________________"); } public static void printHello(){ + printHorizonLine(); System.out.println(" Hello! I'm Duke"); System.out.println(" What can I do for you?"); + printHorizonLine(); } public static void printGoodBye(){ System.out.println(" Bye. Hope to see you again soon!"); } + + public static void printRequestList(){ + for (int i = 0; i < list.getSize(); i++) { + Request t = list.getRequest(i); + System.out.println(" " + (i + 1) + ". " + t.getTitle()); + } + } + + public static void addRequest(String title){ + list.addRequest(new Request(title)); + } + public static void main(String[] args) { Scanner k = new Scanner(System.in); String input; - printHorizonLine(); printHello(); - printHorizonLine(); - System.out.println(); while(true){ input = k.nextLine(); if(input.toLowerCase().equals("bye")){ break; + } else if(input.toLowerCase().equals("list")){ + printHorizonLine(); + printRequestList(); + printHorizonLine(); + continue; + }else { + addRequest(input); + printHorizonLine(); + System.out.println(" Added : " + input); + printHorizonLine(); + continue; } - printHorizonLine(); - System.out.println(" " + input); - printHorizonLine(); - System.out.println(); } printHorizonLine(); printGoodBye(); diff --git a/src/main/java/Request.java b/src/main/java/Request.java new file mode 100644 index 0000000000..875396a7a7 --- /dev/null +++ b/src/main/java/Request.java @@ -0,0 +1,49 @@ +public class Request { + private String title; + private String description; + private boolean isDone; + + public Request(String title, String description) { + this.title = title; + this.description = description; + this.isDone = false; + } + + public Request(String title) { + this.title = title; + this.description = ""; + this.isDone = false; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public boolean getDone() { + return isDone; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setDone() { + isDone = true; + } + + public void setNotDone() { + isDone = false; + } + + public void print() { + System.out.print("[" + (isDone ? "*" : " ") + "] " + title); + } +} \ No newline at end of file diff --git a/src/main/java/RequestList.java b/src/main/java/RequestList.java new file mode 100644 index 0000000000..d1a10b73d5 --- /dev/null +++ b/src/main/java/RequestList.java @@ -0,0 +1,33 @@ +import java.util.ArrayList; + +public class RequestList { + private ArrayList Requests; + + public RequestList() { + Requests = new ArrayList(); + } + + public ArrayList getAllRequests() { + return Requests; + } + + public void addRequest(Request t) { + Requests.add(t); + } + + public void deleteRequest(int index) throws IndexOutOfBoundsException { + Requests.remove(index); + } + + public void markRequestDone(int index) throws IndexOutOfBoundsException { + Requests.get(index).setDone(); + } + + public int getSize() { + return Requests.size(); + } + + public Request getRequest(int index) { + return Requests.get(index); + } +} \ No newline at end of file From b40bb7afd67b45d2fe33b72e7768cfe710df7788 Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Tue, 1 Sep 2020 17:25:31 +0800 Subject: [PATCH 04/12] Level 3. Mark as done --- src/main/java/Duke.java | 80 +++++++++++++++------------------- src/main/java/Request.java | 49 --------------------- src/main/java/RequestList.java | 33 -------------- src/main/java/Task.java | 22 ++++++++++ 4 files changed, 56 insertions(+), 128 deletions(-) delete mode 100644 src/main/java/Request.java delete mode 100644 src/main/java/RequestList.java create mode 100644 src/main/java/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e57cb0f9e6..f2576715d4 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,55 +1,43 @@ -import java.util.Scanner; +import java.util.*; public class Duke { - public static RequestList list = new RequestList(); - - public static void printHorizonLine(){ - System.out.println(" ____________________________________________________________"); - } - public static void printHello(){ - printHorizonLine(); + public static void main(String[] args) { + String line; + ArrayList tasks = new ArrayList<>(); + Scanner sc = new Scanner(System.in); + final String HORIZONTAL_LINE = " ____________________________________________________________"; + System.out.println(HORIZONTAL_LINE); System.out.println(" Hello! I'm Duke"); System.out.println(" What can I do for you?"); - printHorizonLine(); - } - public static void printGoodBye(){ - System.out.println(" Bye. Hope to see you again soon!"); - } + System.out.println(HORIZONTAL_LINE); + line = sc.nextLine(); + while (!line.equals("bye")){ + if(line.equals("list")){ + System.out.println(HORIZONTAL_LINE); + for (int i = 0; i < tasks.size(); i++) { - public static void printRequestList(){ - for (int i = 0; i < list.getSize(); i++) { - Request t = list.getRequest(i); - System.out.println(" " + (i + 1) + ". " + t.getTitle()); - } - } - - public static void addRequest(String title){ - list.addRequest(new Request(title)); - } - - public static void main(String[] args) { - Scanner k = new Scanner(System.in); - String input; - printHello(); - while(true){ - input = k.nextLine(); - if(input.toLowerCase().equals("bye")){ - break; - } else if(input.toLowerCase().equals("list")){ - printHorizonLine(); - printRequestList(); - printHorizonLine(); - continue; - }else { - addRequest(input); - printHorizonLine(); - System.out.println(" Added : " + input); - printHorizonLine(); - continue; + System.out.println((i+1)+":"+"["+tasks.get(i).getStatusIcon()+"] "+tasks.get(i).description); + } + System.out.println(HORIZONTAL_LINE); + }else if(line.startsWith("done")){ + System.out.println(HORIZONTAL_LINE); + int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; + tasks.set(taskNumberCompleted,tasks.get(taskNumberCompleted).completeTask()); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" " + "["+tasks.get(taskNumberCompleted).getStatusIcon()+"] "+tasks.get(taskNumberCompleted).description); + System.out.println(HORIZONTAL_LINE); + System.out.println(); + } else{ + System.out.println(HORIZONTAL_LINE); + System.out.println(" added :" + line); + tasks.add(new Task(line)); + System.out.println(HORIZONTAL_LINE); + System.out.println(); } + line = sc.nextLine(); } - printHorizonLine(); - printGoodBye(); - printHorizonLine(); + System.out.println(HORIZONTAL_LINE); + System.out.println(" Bye. Hope to see you again soon!"); + System.out.println(HORIZONTAL_LINE); } } \ No newline at end of file diff --git a/src/main/java/Request.java b/src/main/java/Request.java deleted file mode 100644 index 875396a7a7..0000000000 --- a/src/main/java/Request.java +++ /dev/null @@ -1,49 +0,0 @@ -public class Request { - private String title; - private String description; - private boolean isDone; - - public Request(String title, String description) { - this.title = title; - this.description = description; - this.isDone = false; - } - - public Request(String title) { - this.title = title; - this.description = ""; - this.isDone = false; - } - - public String getTitle() { - return title; - } - - public String getDescription() { - return description; - } - - public boolean getDone() { - return isDone; - } - - public void setTitle(String title) { - this.title = title; - } - - public void setDescription(String description) { - this.description = description; - } - - public void setDone() { - isDone = true; - } - - public void setNotDone() { - isDone = false; - } - - public void print() { - System.out.print("[" + (isDone ? "*" : " ") + "] " + title); - } -} \ No newline at end of file diff --git a/src/main/java/RequestList.java b/src/main/java/RequestList.java deleted file mode 100644 index d1a10b73d5..0000000000 --- a/src/main/java/RequestList.java +++ /dev/null @@ -1,33 +0,0 @@ -import java.util.ArrayList; - -public class RequestList { - private ArrayList Requests; - - public RequestList() { - Requests = new ArrayList(); - } - - public ArrayList getAllRequests() { - return Requests; - } - - public void addRequest(Request t) { - Requests.add(t); - } - - public void deleteRequest(int index) throws IndexOutOfBoundsException { - Requests.remove(index); - } - - public void markRequestDone(int index) throws IndexOutOfBoundsException { - Requests.get(index).setDone(); - } - - public int getSize() { - return Requests.size(); - } - - public Request getRequest(int index) { - return Requests.get(index); - } -} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java new file mode 100644 index 0000000000..1ed036f39d --- /dev/null +++ b/src/main/java/Task.java @@ -0,0 +1,22 @@ +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + public Task(String description,boolean isDone) { + this.description = description; + this.isDone = isDone; + + } + + public String getStatusIcon() { + return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols + } + + public Task completeTask(){ + return new Task(this.description, true); + } +} \ No newline at end of file From 08c17cb87ed0853ad6be0292473e69ca3119727d Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Wed, 2 Sep 2020 23:34:30 +0800 Subject: [PATCH 05/12] Level-4. code quality --- src/main/java/Deadline.java | 21 ++++++++++++++++++++ src/main/java/Duke.java | 38 +++++++++++++++++++++++++++++-------- src/main/java/Event.java | 20 +++++++++++++++++++ src/main/java/Task.java | 12 ++++++------ src/main/java/ToDo.java | 10 ++++++++++ 5 files changed, 87 insertions(+), 14 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 0000000000..f8f2079674 --- /dev/null +++ b/src/main/java/Deadline.java @@ -0,0 +1,21 @@ +public class Deadline extends Task { + + protected String by; + + public Deadline(String line) { + super(line.substring(9,(line.indexOf("/")-1))); + this.by = line.substring((line.indexOf("/")+4)); + } + + public void setBy(String by) { + this.by = by; + } + + public String getBy() { + return by; + } + @Override + public String toString(){ + return "[" + "D" + "]" + super.toString() + " (at: "+ by +")"; + } +} \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f2576715d4..ffb09053e1 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -14,30 +14,52 @@ public static void main(String[] args) { while (!line.equals("bye")){ if(line.equals("list")){ System.out.println(HORIZONTAL_LINE); + System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { - - System.out.println((i+1)+":"+"["+tasks.get(i).getStatusIcon()+"] "+tasks.get(i).description); + System.out.println(" " + (i+1) +":" + tasks.get(i)); } System.out.println(HORIZONTAL_LINE); }else if(line.startsWith("done")){ System.out.println(HORIZONTAL_LINE); int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; tasks.set(taskNumberCompleted,tasks.get(taskNumberCompleted).completeTask()); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" " + "["+tasks.get(taskNumberCompleted).getStatusIcon()+"] "+tasks.get(taskNumberCompleted).description); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" "+":"+tasks.get(taskNumberCompleted)); System.out.println(HORIZONTAL_LINE); System.out.println(); - } else{ + } else if(line.startsWith("todo")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new ToDo(line)); + System.out.println(" " + new ToDo(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } else if(line.startsWith("deadline")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Deadline(line)); + System.out.println(" " + new Deadline(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } + else if(line.startsWith("event")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Event(line)); + System.out.println(" " + new Event(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } + else { System.out.println(HORIZONTAL_LINE); - System.out.println(" added :" + line); - tasks.add(new Task(line)); + System.out.println(" Invalid input." + System.lineSeparator() + " Type bye to exit."); System.out.println(HORIZONTAL_LINE); System.out.println(); } line = sc.nextLine(); } System.out.println(HORIZONTAL_LINE); - System.out.println(" Bye. Hope to see you again soon!"); + System.out.println(" Bye. Hope to see you again soon!"); System.out.println(HORIZONTAL_LINE); } } \ No newline at end of file diff --git a/src/main/java/Event.java b/src/main/java/Event.java new file mode 100644 index 0000000000..a14214660a --- /dev/null +++ b/src/main/java/Event.java @@ -0,0 +1,20 @@ +public class Event extends Task{ + protected String at; + + public Event(String line) { + super(line.substring(6,(line.indexOf("/")-1))); + this.at = line.substring((line.indexOf("/")+4)); + } + + public void setBy(String by) { + this.at = by; + } + + public String getBy() { + return at; + } + @Override + public String toString(){ + return "[" + "D" + "]" + super.toString() + " (at: "+ at +")"; + } +} \ No newline at end of file diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 1ed036f39d..cfc7029b03 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,7 +1,6 @@ public class Task { protected String description; protected boolean isDone; - public Task(String description) { this.description = description; this.isDone = false; @@ -9,14 +8,15 @@ public Task(String description) { public Task(String description,boolean isDone) { this.description = description; this.isDone = isDone; - } - public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols } - public Task completeTask(){ - return new Task(this.description, true); + return new Task(this.description,true); } -} \ No newline at end of file + @Override + public String toString(){ + return"["+this.getStatusIcon()+"] "+this.description; + } +} \ No newline at end of file diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java new file mode 100644 index 0000000000..9dcf1bfc32 --- /dev/null +++ b/src/main/java/ToDo.java @@ -0,0 +1,10 @@ +public class ToDo extends Task { + + public ToDo(String description) { + super(description.substring(5)); + } + @Override + public String toString() { + return "["+"T"+"]"+ super.toString(); + } +} \ No newline at end of file From 16687ef524bc459e968f893d690ee5e65ed29a41 Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Thu, 3 Sep 2020 16:45:25 +0800 Subject: [PATCH 06/12] fix a formatting issue --- src/main/java/Duke.java | 2 +- src/main/java/Task.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index ffb09053e1..0cfd2f17cb 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -22,7 +22,7 @@ public static void main(String[] args) { }else if(line.startsWith("done")){ System.out.println(HORIZONTAL_LINE); int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; - tasks.set(taskNumberCompleted,tasks.get(taskNumberCompleted).completeTask()); + tasks.get(taskNumberCompleted).completeTask(); System.out.println(" Nice! I've marked this task as done:"); System.out.println(" "+":"+tasks.get(taskNumberCompleted)); System.out.println(HORIZONTAL_LINE); diff --git a/src/main/java/Task.java b/src/main/java/Task.java index cfc7029b03..936e5f77bc 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -12,8 +12,8 @@ public Task(String description,boolean isDone) { public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols } - public Task completeTask(){ - return new Task(this.description,true); + public void completeTask(){ + isDone = true; } @Override public String toString(){ From 13c931a0acf37265d9eab69ebfac4a3778e3a08b Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Sun, 6 Sep 2020 23:27:57 +0800 Subject: [PATCH 07/12] Level-5 --- src/main/java/Deadline.java | 7 +-- src/main/java/Duke.java | 91 +++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index f8f2079674..c5efd52ff1 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,10 +1,11 @@ public class Deadline extends Task { - + public static int DEADLINE_LENGTH = 9; + public static int BY_LENGTH = 4; protected String by; public Deadline(String line) { - super(line.substring(9,(line.indexOf("/")-1))); - this.by = line.substring((line.indexOf("/")+4)); + super(line.substring(DEADLINE_LENGTH,(line.indexOf("/")-1))); + this.by = line.substring((line.indexOf("/") + BY_LENGTH)); } public void setBy(String by) { diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 0cfd2f17cb..8152380b77 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,5 @@ -import java.util.*; +import java.util.Scanner; +import java.util.ArrayList; public class Duke { public static void main(String[] args) { @@ -12,49 +13,61 @@ public static void main(String[] args) { System.out.println(HORIZONTAL_LINE); line = sc.nextLine(); while (!line.equals("bye")){ - if(line.equals("list")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i++) { - System.out.println(" " + (i+1) +":" + tasks.get(i)); + try { + if(line.equals("list")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println(" " + (i+1) +":" + tasks.get(i)); + } + System.out.println(HORIZONTAL_LINE); + }else if(line.startsWith("done")){ + System.out.println(HORIZONTAL_LINE); + int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; + tasks.get(taskNumberCompleted).completeTask(); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" "+":"+tasks.get(taskNumberCompleted)); + System.out.println(HORIZONTAL_LINE); + System.out.println(); + } else if(line.startsWith("todo")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new ToDo(line)); + System.out.println(" " + new ToDo(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } else if(line.startsWith("deadline")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Deadline(line)); + System.out.println(" " + new Deadline(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } else if(line.startsWith("event")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Event(line)); + System.out.println(" " + new Event(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } else if (line.isEmpty()) { + System.out.println(HORIZONTAL_LINE); + System.out.println(" ☹ OOPS!!! The description of a task cannot be empty."); + System.out.println(HORIZONTAL_LINE); + } else { + System.out.println(HORIZONTAL_LINE); + System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + System.out.println(HORIZONTAL_LINE); + System.out.println(); } + } catch (StringIndexOutOfBoundsException e) { System.out.println(HORIZONTAL_LINE); - }else if(line.startsWith("done")){ - System.out.println(HORIZONTAL_LINE); - int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; - tasks.get(taskNumberCompleted).completeTask(); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" "+":"+tasks.get(taskNumberCompleted)); - System.out.println(HORIZONTAL_LINE); - System.out.println(); - } else if(line.startsWith("todo")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Got it. I've added this task:"); - tasks.add(new ToDo(line)); - System.out.println(" " + new ToDo(line)); - System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); - } else if(line.startsWith("deadline")){ + System.out.println("The task you input has missing fields!"); System.out.println(HORIZONTAL_LINE); - System.out.println(" Got it. I've added this task:"); - tasks.add(new Deadline(line)); - System.out.println(" " + new Deadline(line)); - System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); - } - else if(line.startsWith("event")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Got it. I've added this task:"); - tasks.add(new Event(line)); - System.out.println(" " + new Event(line)); - System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); - } - else { + } catch (Exception e) { System.out.println(HORIZONTAL_LINE); - System.out.println(" Invalid input." + System.lineSeparator() + " Type bye to exit."); + System.out.println("Something went wrong!"); System.out.println(HORIZONTAL_LINE); - System.out.println(); } line = sc.nextLine(); } From 250902ab114a3f9450205dd1f8aa10aa19ebb9e2 Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Sun, 6 Sep 2020 23:32:27 +0800 Subject: [PATCH 08/12] coding standard --- src/main/java/Duke.java | 49 ++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8152380b77..e16f85aa50 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -6,73 +6,76 @@ public static void main(String[] args) { String line; ArrayList tasks = new ArrayList<>(); Scanner sc = new Scanner(System.in); - final String HORIZONTAL_LINE = " ____________________________________________________________"; - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" Hello! I'm Duke"); System.out.println(" What can I do for you?"); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); line = sc.nextLine(); while (!line.equals("bye")){ try { if(line.equals("list")){ - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { System.out.println(" " + (i+1) +":" + tasks.get(i)); } - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); }else if(line.startsWith("done")){ - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; tasks.get(taskNumberCompleted).completeTask(); System.out.println(" Nice! I've marked this task as done:"); System.out.println(" "+":"+tasks.get(taskNumberCompleted)); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(); } else if(line.startsWith("todo")){ - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" Got it. I've added this task:"); tasks.add(new ToDo(line)); System.out.println(" " + new ToDo(line)); System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); } else if(line.startsWith("deadline")){ - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" Got it. I've added this task:"); tasks.add(new Deadline(line)); System.out.println(" " + new Deadline(line)); System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); } else if(line.startsWith("event")){ - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" Got it. I've added this task:"); tasks.add(new Event(line)); System.out.println(" " + new Event(line)); System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); } else if (line.isEmpty()) { - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" ☹ OOPS!!! The description of a task cannot be empty."); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); } else { - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(); } } catch (StringIndexOutOfBoundsException e) { - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println("The task you input has missing fields!"); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); } catch (Exception e) { - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println("Something went wrong!"); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); } line = sc.nextLine(); } - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); System.out.println(" Bye. Hope to see you again soon!"); - System.out.println(HORIZONTAL_LINE); + printHorizontalLine(); + } + + private static void printHorizontalLine () { + System.out.println(" ____________________________________________________________"); } } \ No newline at end of file From 6a435ac041d94b507fe795bc3e58b2eed10dfc7d Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Mon, 7 Sep 2020 23:27:29 +0800 Subject: [PATCH 09/12] fix some errors --- src/main/java/Duke.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 8152380b77..53aca59933 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -30,9 +30,9 @@ public static void main(String[] args) { System.out.println(HORIZONTAL_LINE); System.out.println(); } else if(line.startsWith("todo")){ + tasks.add(new ToDo(line)); System.out.println(HORIZONTAL_LINE); System.out.println(" Got it. I've added this task:"); - tasks.add(new ToDo(line)); System.out.println(" " + new ToDo(line)); System.out.println(" Now you have " + tasks.size() + " tasks in the list."); System.out.println(HORIZONTAL_LINE); @@ -62,11 +62,11 @@ public static void main(String[] args) { } } catch (StringIndexOutOfBoundsException e) { System.out.println(HORIZONTAL_LINE); - System.out.println("The task you input has missing fields!"); + System.out.println(" The task you input has missing fields!"); System.out.println(HORIZONTAL_LINE); } catch (Exception e) { System.out.println(HORIZONTAL_LINE); - System.out.println("Something went wrong!"); + System.out.println(" Something went wrong!"); System.out.println(HORIZONTAL_LINE); } line = sc.nextLine(); From a3ed678022dbd5702fd85ca27573a900a980d439 Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Mon, 7 Sep 2020 23:37:31 +0800 Subject: [PATCH 10/12] fix output format errors --- src/main/java/Duke.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 53aca59933..2642dfb97a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -37,16 +37,16 @@ public static void main(String[] args) { System.out.println(" Now you have " + tasks.size() + " tasks in the list."); System.out.println(HORIZONTAL_LINE); } else if(line.startsWith("deadline")){ + tasks.add(new Deadline(line)); System.out.println(HORIZONTAL_LINE); System.out.println(" Got it. I've added this task:"); - tasks.add(new Deadline(line)); System.out.println(" " + new Deadline(line)); System.out.println(" Now you have " + tasks.size() + " tasks in the list."); System.out.println(HORIZONTAL_LINE); } else if(line.startsWith("event")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Got it. I've added this task:"); tasks.add(new Event(line)); + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); System.out.println(" " + new Event(line)); System.out.println(" Now you have " + tasks.size() + " tasks in the list."); System.out.println(HORIZONTAL_LINE); From a8820b93a784c892e33c21c10b60272d503f1566 Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Thu, 1 Oct 2020 13:39:10 +0800 Subject: [PATCH 11/12] Revert "coding standard" This reverts commit 250902ab114a3f9450205dd1f8aa10aa19ebb9e2. --- src/main/java/Duke.java | 74 +++++++ src/main/java/{ => duke}/Commands.java | 0 src/main/java/duke/Duke.java | 205 ++++++++++++++++++ .../{ => duke}/ImportExportFileHandler.java | 0 src/main/java/{ => duke}/tasks/Deadline.java | 0 src/main/java/{ => duke}/tasks/Event.java | 0 src/main/java/{ => duke}/tasks/Task.java | 0 src/main/java/{ => duke}/tasks/Todo.java | 0 8 files changed, 279 insertions(+) rename src/main/java/{ => duke}/Commands.java (100%) create mode 100644 src/main/java/duke/Duke.java rename src/main/java/{ => duke}/ImportExportFileHandler.java (100%) rename src/main/java/{ => duke}/tasks/Deadline.java (100%) rename src/main/java/{ => duke}/tasks/Event.java (100%) rename src/main/java/{ => duke}/tasks/Task.java (100%) rename src/main/java/{ => duke}/tasks/Todo.java (100%) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a657303635..1f929286e3 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -15,6 +15,7 @@ enum TaskType { private static ImportExportFileHandler fileHandler = new ImportExportFileHandler(tasks); public static void main(String[] args) { +<<<<<<< HEAD printLogo(); greet(); echo(); @@ -148,12 +149,80 @@ private static void executeTaskAddingCommand(String userInputSentence) throws Em System.out.println("Please specify the task type."); System.out.println(SEPARATOR); } +======= + String line; + ArrayList tasks = new ArrayList<>(); + Scanner sc = new Scanner(System.in); + final String HORIZONTAL_LINE = " ____________________________________________________________"; + System.out.println(HORIZONTAL_LINE); + System.out.println(" Hello! I'm Duke"); + System.out.println(" What can I do for you?"); + System.out.println(HORIZONTAL_LINE); + line = sc.nextLine(); + while (!line.equals("bye")){ + try { + if(line.equals("list")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println(" " + (i+1) +":" + tasks.get(i)); + } + System.out.println(HORIZONTAL_LINE); + }else if(line.startsWith("done")){ + System.out.println(HORIZONTAL_LINE); + int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; + tasks.get(taskNumberCompleted).completeTask(); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" "+":"+tasks.get(taskNumberCompleted)); + System.out.println(HORIZONTAL_LINE); + System.out.println(); + } else if(line.startsWith("todo")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new ToDo(line)); + System.out.println(" " + new ToDo(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } else if(line.startsWith("deadline")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Deadline(line)); + System.out.println(" " + new Deadline(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } else if(line.startsWith("event")){ + System.out.println(HORIZONTAL_LINE); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Event(line)); + System.out.println(" " + new Event(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + System.out.println(HORIZONTAL_LINE); + } else if (line.isEmpty()) { + System.out.println(HORIZONTAL_LINE); + System.out.println(" ☹ OOPS!!! The description of a task cannot be empty."); + System.out.println(HORIZONTAL_LINE); + } else { + System.out.println(HORIZONTAL_LINE); + System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + System.out.println(HORIZONTAL_LINE); + System.out.println(); + } + } catch (StringIndexOutOfBoundsException e) { + System.out.println(HORIZONTAL_LINE); + System.out.println("The task you input has missing fields!"); + System.out.println(HORIZONTAL_LINE); + } catch (Exception e) { + System.out.println(HORIZONTAL_LINE); + System.out.println("Something went wrong!"); + System.out.println(HORIZONTAL_LINE); +>>>>>>> parent of 250902a... coding standard } } catch (IndexOutOfBoundsException e) { System.out.println(SEPARATOR); System.out.println("Wrong syntax for commands."); System.out.println(SEPARATOR); } +<<<<<<< HEAD fileHandler.UpdateFile(tasks); } @@ -212,5 +281,10 @@ protected static void printTaskList() { } System.out.println(SEPARATOR); +======= + System.out.println(HORIZONTAL_LINE); + System.out.println(" Bye. Hope to see you again soon!"); + System.out.println(HORIZONTAL_LINE); +>>>>>>> parent of 250902a... coding standard } } diff --git a/src/main/java/Commands.java b/src/main/java/duke/Commands.java similarity index 100% rename from src/main/java/Commands.java rename to src/main/java/duke/Commands.java diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 0000000000..e577ee6bac --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,205 @@ +import ImportExportFileHandler; +import exceptions.*; +import tasks.*; +import java.lang.System; +import java.util.ArrayList; +import java.util.Scanner; + +public class Duke { + private static final String SEPARATOR = "____________________________________________________________"; + private static ArrayList tasks = new ArrayList<>(); + private static int inputStoredIndex = 0; + enum TaskType { + TODO, DEADLINE, EVENT, TASK; + } + private static ImportExportFileHandler fileHandler = new ImportExportFileHandler(tasks); + + public static void main(String[] args) { + printLogo(); + greet(); + echo(); + } + + public static void printLogo(){ + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + } + + public static void greet(){ + String greeting = "Hello! I'm Duke\nWhat can I do for you?"; + + System.out.println(SEPARATOR); + System.out.println(greeting); + System.out.println(SEPARATOR); + } + + public static void echo(){ + Scanner scanner = new Scanner(System.in); + String userInputSentence; + + while (true) { + userInputSentence = scanner.nextLine(); + executeCommand(userInputSentence); + } + } + + private static void executeCommand(String userInputSentence) { + boolean isEqualToTERMIATION = userInputSentence.toLowerCase().equals(Commands.TERMINATION); + boolean isEqualToLIST_PRINT = userInputSentence.toLowerCase().equals(Commands.LIST_PRINT); + boolean isEqualToSET_TASK_DONE = userInputSentence.length() >= Commands.SET_TASK_DONE.length() && + userInputSentence.substring(0, Commands.SET_TASK_DONE_COMMAND.length()).equals(Commands.SET_TASK_DONE); + boolean isEqualToDELETE = userInputSentence.startsWith(Commands.DELETE); + + if (isEqualToTERMIATION){ + exit(); + } else if (isEqualToLIST_PRINT) { + printTaskList(); + } else if (isEqualToSET_TASK_DONE) { + String[] splitCommand = userInputSentence.split(" "); + Integer setTaskId = Integer.valueOf(splitCommand[1]); + + setTaskDone(setTaskId - 1); + } else if (isEqualToDELETE) { + DeleteTask(userInputSentence); + } + } + } + + private static void DeleteTask(String userInputSentence) { + int taskId = Integer.parseInt(userInputSentence.split(" ")[1]); + try { + Task task = tasks.get(taskId-1); + tasks.remove(taskId - 1); + + System.out.println(SEPARATOR); + System.out.println("Noted. I've removed this task: "); + System.out.println(task); + System.out.printf("Now you have %d tasks in the list.\n", tasks.size()); + System.out.println(SEPARATOR); + } catch (IndexOutOfBoundsException e) { + System.out.printf("There is no task with the index %d", taskId); + } + + fileHandler.UpdateFile(tasks); + } + + private static void executeTaskAddingCommand(String userInputSentence) throws EmptyContentForTodoCommandException, UnknownCommandException{ + boolean isAddTodo = userInputSentence.length() >= Commands.ADD_TODO_COMMAND.length() && + userInputSentence.substring(0, Commands.ADD_TODO_COMMAND.length()).equals(Commands.ADD_TODO_COMMAND); + boolean isAddDeadline = userInputSentence.length() >= Commands.ADD_DEADLINE_COMMAND.length() && + userInputSentence.substring(0, Commands.ADD_DEADLINE_COMMAND.length()).equals(Commands.ADD_DEADLINE_COMMAND); + boolean isAddEvent = userInputSentence.length() >= Commands.ADD_EVENT_COMMAND.length() && + userInputSentence.substring(0, Commands.ADD_EVENT_COMMAND.length()).equals(Commands.ADD_EVENT_COMMAND); + + TaskType taskTypeToAdd = TaskType.TASK; + if (isAddTodo) { + taskTypeToAdd = TaskType.TODO; + } else if (isAddDeadline) { + taskTypeToAdd = TaskType.DEADLINE; + } else if (isAddEvent) { + taskTypeToAdd = TaskType.EVENT; + } else { + throw new UnknownCommandException(); + } + try { + switch (taskTypeToAdd) { + case TODO: { + String description = userInputSentence.substring(Commands.ADD_TODO_COMMAND.length()); + if (description == null || description.equals("")) { + throw new EmptyContentForTodoCommandException(); + } + tasks.add(new Todo(description)); + printSuccessfulAddedMessage(); + inputStoredIndex++; + break; + } + case DEADLINE: { + String[] splitCommands = userInputSentence.substring(Commands.ADD_DEADLINE_COMMAND.length()).split("/by "); + tasks.add(new Deadline(splitCommands[0], splitCommands[1])); + printSuccessfulAddedMessage(); + inputStoredIndex++; + break; + } + case EVENT: { + String[] splitCommands = userInputSentence.substring(Commands.ADD_EVENT_COMMAND.length()).split("/at "); + tasks.add(new Event(splitCommands[0], splitCommands[1])); + printSuccessfulAddedMessage(); + inputStoredIndex++; + break; + } + default: { + /* If the user doesn't specify the task type to add, the reminder will be given. */ + System.out.println(SEPARATOR); + System.out.println("Please specify the task type."); + System.out.println(SEPARATOR); + } + } + } catch (IndexOutOfBoundsException e) { + System.out.println(SEPARATOR); + System.out.println("Wrong syntax for commands."); + System.out.println(SEPARATOR); + } + + fileHandler.UpdateFile(tasks); + } + + private static void setTaskDone(Integer setTaskId) { + try { + Task targetTask = tasks.get(setTaskId); + targetTask.setDone(); + + System.out.println(SEPARATOR); + System.out.printf("Nice! I've marked this task as done: \n[%s] %s\n", + targetTask.getStatusIcon(),targetTask.getDescription()); + System.out.println(SEPARATOR); + } + catch (NullPointerException e) { + System.out.printf("duke.tasks.Task %d does not exist.\n", setTaskId); + } + + fileHandler.UpdateFile(tasks); + } + + private static void printSuccessfulAddedMessage() { + System.out.println(SEPARATOR); + System.out.println("Got it. I've added this task: "); + System.out.println(tasks.get(inputStoredIndex)); + System.out.printf("Now you have %d tasks in the list.\n", Task.getNumTotalTasks()); + System.out.println(SEPARATOR); + } + + public static void sayGoodbye(){ + String goodbyeWords = "Bye. Hope to see you again soon!"; + + System.out.println(SEPARATOR); + System.out.println(goodbyeWords); + System.out.println(SEPARATOR); + } + + protected static void exit() { + sayGoodbye(); + System.exit(0); + } + + protected static void printTaskList() { + int taskId = 1; + + System.out.println(SEPARATOR); + System.out.println("Here are the tasks in your list:"); + + for (Task task: tasks) { + if (task==null) { + break; + } else { + System.out.printf("%d.%s\n", taskId, task); + taskId++; + } + } + + System.out.println(SEPARATOR); + } +} diff --git a/src/main/java/ImportExportFileHandler.java b/src/main/java/duke/ImportExportFileHandler.java similarity index 100% rename from src/main/java/ImportExportFileHandler.java rename to src/main/java/duke/ImportExportFileHandler.java diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java similarity index 100% rename from src/main/java/tasks/Deadline.java rename to src/main/java/duke/tasks/Deadline.java diff --git a/src/main/java/tasks/Event.java b/src/main/java/duke/tasks/Event.java similarity index 100% rename from src/main/java/tasks/Event.java rename to src/main/java/duke/tasks/Event.java diff --git a/src/main/java/tasks/Task.java b/src/main/java/duke/tasks/Task.java similarity index 100% rename from src/main/java/tasks/Task.java rename to src/main/java/duke/tasks/Task.java diff --git a/src/main/java/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java similarity index 100% rename from src/main/java/tasks/Todo.java rename to src/main/java/duke/tasks/Todo.java From dbcc35cafe023a8f7f5598bdb9a0e23062137e37 Mon Sep 17 00:00:00 2001 From: Wang Annan <66814452+AnnanWangDaniel@users.noreply.github.com> Date: Thu, 1 Oct 2020 13:49:39 +0800 Subject: [PATCH 12/12] fixing --- src/main/java/Duke.java | 290 ------------------ src/main/java/duke/Commands.java | 9 - src/main/java/duke/Duke.java | 260 ++++------------ .../java/duke/ImportExportFileHandler.java | 66 ---- 4 files changed, 68 insertions(+), 557 deletions(-) delete mode 100644 src/main/java/Duke.java delete mode 100644 src/main/java/duke/Commands.java delete mode 100644 src/main/java/duke/ImportExportFileHandler.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 1f929286e3..0000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,290 +0,0 @@ -import ImportExportFileHandler; -import exceptions.*; -import tasks.*; -import java.lang.System; -import java.util.ArrayList; -import java.util.Scanner; - -public class Duke { - private static final String SEPARATOR = "____________________________________________________________"; - private static ArrayList tasks = new ArrayList<>(); - private static int inputStoredIndex = 0; - enum TaskType { - TODO, DEADLINE, EVENT, TASK; - } - private static ImportExportFileHandler fileHandler = new ImportExportFileHandler(tasks); - - public static void main(String[] args) { -<<<<<<< HEAD - printLogo(); - greet(); - echo(); - } - - public static void printLogo(){ - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } - - public static void greet(){ - String greeting = "Hello! I'm Duke\nWhat can I do for you?"; - - System.out.println(SEPARATOR); - System.out.println(greeting); - System.out.println(SEPARATOR); - } - - public static void echo(){ - Scanner scanner = new Scanner(System.in); - String userInputSentence; - - while (true) { - userInputSentence = scanner.nextLine(); - executeCommand(userInputSentence); - } - } - - private static void executeCommand(String userInputSentence) { - boolean isEqualToTERMIATION_SIGNAL = userInputSentence.toLowerCase().equals(Commands.TERMINATION_SIGNAL); - boolean isEqualToLIST_PRINT_COMMAND = userInputSentence.toLowerCase().equals(Commands.LIST_PRINT_COMMAND); - boolean isEqualToSET_TASK_DONE_COMMAND = userInputSentence.length() >= Commands.SET_TASK_DONE_COMMAND.length() && - userInputSentence.substring(0, Commands.SET_TASK_DONE_COMMAND.length()).equals(Commands.SET_TASK_DONE_COMMAND); - boolean isEqualToDELETE_COMMAND = userInputSentence.startsWith(Commands.DELETE_COMMAND); - - if (isEqualToTERMIATION_SIGNAL){ - exit(); - } else if (isEqualToLIST_PRINT_COMMAND) { - printTaskList(); - } else if (isEqualToSET_TASK_DONE_COMMAND) { - String[] splitCommand = userInputSentence.split(" "); - Integer setTaskId = Integer.valueOf(splitCommand[1]); - - setTaskDone(setTaskId - 1); // -1 because array's index starts from 0 - } else if (isEqualToDELETE_COMMAND) { - DeleteTask(userInputSentence); - } else { - try { - executeTaskAddingCommand(userInputSentence); - } catch (EmptyContentForTodoCommandException e) { - System.out.println(SEPARATOR); - System.out.println("☹ OOPS!!! The description of a todo cannot be empty."); - System.out.println(SEPARATOR); - } catch (UnknownCommandException e) { - System.out.println(SEPARATOR); - System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); - System.out.println(SEPARATOR); - } - } - } - - private static void DeleteTask(String userInputSentence) { - int taskId = Integer.parseInt(userInputSentence.split(" ")[1]); - try { - Task task = tasks.get(taskId-1); - tasks.remove(taskId - 1); - - System.out.println(SEPARATOR); - System.out.println("Noted. I've removed this task: "); - System.out.println(task); - System.out.printf("Now you have %d tasks in the list.\n", tasks.size()); - System.out.println(SEPARATOR); - } catch (IndexOutOfBoundsException e) { - System.out.printf("There is no task with the index %d", taskId); - } - - fileHandler.UpdateFile(tasks); - } - - private static void executeTaskAddingCommand(String userInputSentence) throws EmptyContentForTodoCommandException, UnknownCommandException{ - boolean isAddTodo = userInputSentence.length() >= Commands.ADD_TODO_COMMAND.length() && - userInputSentence.substring(0, Commands.ADD_TODO_COMMAND.length()).equals(Commands.ADD_TODO_COMMAND); - boolean isAddDeadline = userInputSentence.length() >= Commands.ADD_DEADLINE_COMMAND.length() && - userInputSentence.substring(0, Commands.ADD_DEADLINE_COMMAND.length()).equals(Commands.ADD_DEADLINE_COMMAND); - boolean isAddEvent = userInputSentence.length() >= Commands.ADD_EVENT_COMMAND.length() && - userInputSentence.substring(0, Commands.ADD_EVENT_COMMAND.length()).equals(Commands.ADD_EVENT_COMMAND); - - TaskType taskTypeToAdd = TaskType.TASK; - if (isAddTodo) { - taskTypeToAdd = TaskType.TODO; - } else if (isAddDeadline) { - taskTypeToAdd = TaskType.DEADLINE; - } else if (isAddEvent) { - taskTypeToAdd = TaskType.EVENT; - } else { - throw new UnknownCommandException(); - } - try { - switch (taskTypeToAdd) { - case TODO: { - String description = userInputSentence.substring(Commands.ADD_TODO_COMMAND.length()); - if (description == null || description.equals("")) { - throw new EmptyContentForTodoCommandException(); - } - tasks.add(new Todo(description)); - printSuccessfulAddedMessage(); - inputStoredIndex++; - break; - } - case DEADLINE: { - String[] splitCommands = userInputSentence.substring(Commands.ADD_DEADLINE_COMMAND.length()).split("/by "); - tasks.add(new Deadline(splitCommands[0], splitCommands[1])); - printSuccessfulAddedMessage(); - inputStoredIndex++; - break; - } - case EVENT: { - String[] splitCommands = userInputSentence.substring(Commands.ADD_EVENT_COMMAND.length()).split("/at "); - tasks.add(new Event(splitCommands[0], splitCommands[1])); - printSuccessfulAddedMessage(); - inputStoredIndex++; - break; - } - default: { - /* If the user doesn't specify the task type to add, the reminder will be given. */ - System.out.println(SEPARATOR); - System.out.println("Please specify the task type."); - System.out.println(SEPARATOR); - } -======= - String line; - ArrayList tasks = new ArrayList<>(); - Scanner sc = new Scanner(System.in); - final String HORIZONTAL_LINE = " ____________________________________________________________"; - System.out.println(HORIZONTAL_LINE); - System.out.println(" Hello! I'm Duke"); - System.out.println(" What can I do for you?"); - System.out.println(HORIZONTAL_LINE); - line = sc.nextLine(); - while (!line.equals("bye")){ - try { - if(line.equals("list")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Here are the tasks in your list:"); - for (int i = 0; i < tasks.size(); i++) { - System.out.println(" " + (i+1) +":" + tasks.get(i)); - } - System.out.println(HORIZONTAL_LINE); - }else if(line.startsWith("done")){ - System.out.println(HORIZONTAL_LINE); - int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+",""))-1; - tasks.get(taskNumberCompleted).completeTask(); - System.out.println(" Nice! I've marked this task as done:"); - System.out.println(" "+":"+tasks.get(taskNumberCompleted)); - System.out.println(HORIZONTAL_LINE); - System.out.println(); - } else if(line.startsWith("todo")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Got it. I've added this task:"); - tasks.add(new ToDo(line)); - System.out.println(" " + new ToDo(line)); - System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); - } else if(line.startsWith("deadline")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Got it. I've added this task:"); - tasks.add(new Deadline(line)); - System.out.println(" " + new Deadline(line)); - System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); - } else if(line.startsWith("event")){ - System.out.println(HORIZONTAL_LINE); - System.out.println(" Got it. I've added this task:"); - tasks.add(new Event(line)); - System.out.println(" " + new Event(line)); - System.out.println(" Now you have " + tasks.size() + " tasks in the list."); - System.out.println(HORIZONTAL_LINE); - } else if (line.isEmpty()) { - System.out.println(HORIZONTAL_LINE); - System.out.println(" ☹ OOPS!!! The description of a task cannot be empty."); - System.out.println(HORIZONTAL_LINE); - } else { - System.out.println(HORIZONTAL_LINE); - System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); - System.out.println(HORIZONTAL_LINE); - System.out.println(); - } - } catch (StringIndexOutOfBoundsException e) { - System.out.println(HORIZONTAL_LINE); - System.out.println("The task you input has missing fields!"); - System.out.println(HORIZONTAL_LINE); - } catch (Exception e) { - System.out.println(HORIZONTAL_LINE); - System.out.println("Something went wrong!"); - System.out.println(HORIZONTAL_LINE); ->>>>>>> parent of 250902a... coding standard - } - } catch (IndexOutOfBoundsException e) { - System.out.println(SEPARATOR); - System.out.println("Wrong syntax for commands."); - System.out.println(SEPARATOR); - } -<<<<<<< HEAD - - fileHandler.UpdateFile(tasks); - } - - private static void setTaskDone(Integer setTaskId) { - try { - Task targetTask = tasks.get(setTaskId); - targetTask.setDone(); - - System.out.println(SEPARATOR); - System.out.printf("Nice! I've marked this task as done: \n[%s] %s\n", - targetTask.getStatusIcon(),targetTask.getDescription()); - System.out.println(SEPARATOR); - } - catch (NullPointerException e) { - System.out.printf("duke.tasks.Task %d does not exist.\n", setTaskId); - } - - fileHandler.UpdateFile(tasks); - } - - private static void printSuccessfulAddedMessage() { - System.out.println(SEPARATOR); - System.out.println("Got it. I've added this task: "); - System.out.println(tasks.get(inputStoredIndex)); - System.out.printf("Now you have %d tasks in the list.\n", Task.getNumTotalTasks()); - System.out.println(SEPARATOR); - } - - public static void sayGoodbye(){ - String goodbyeWords = "Bye. Hope to see you again soon!"; - - System.out.println(SEPARATOR); - System.out.println(goodbyeWords); - System.out.println(SEPARATOR); - } - - protected static void exit() { - sayGoodbye(); - System.exit(0); - } - - protected static void printTaskList() { - int taskId = 1; - - System.out.println(SEPARATOR); - System.out.println("Here are the tasks in your list:"); - - for (Task task: tasks) { - if (task==null) { - break; - } else { - System.out.printf("%d.%s\n", taskId, task); - taskId++; - } - } - - System.out.println(SEPARATOR); -======= - System.out.println(HORIZONTAL_LINE); - System.out.println(" Bye. Hope to see you again soon!"); - System.out.println(HORIZONTAL_LINE); ->>>>>>> parent of 250902a... coding standard - } -} diff --git a/src/main/java/duke/Commands.java b/src/main/java/duke/Commands.java deleted file mode 100644 index 8c2e87e8b5..0000000000 --- a/src/main/java/duke/Commands.java +++ /dev/null @@ -1,9 +0,0 @@ -public class Commands { - final static String TERMINATION_SIGNAL = "bye"; - final static String LIST_PRINT = "list"; - final static String SET_TASK_DONE = "done"; - final static String ADD_TODO = "todo"; - final static String ADD_DEADLINE = "deadline"; - final static String ADD_EVENT = "event"; - final static String DELETE = "delete"; -} diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index e577ee6bac..7f43369d16 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -1,205 +1,81 @@ -import ImportExportFileHandler; -import exceptions.*; -import tasks.*; -import java.lang.System; import java.util.ArrayList; import java.util.Scanner; public class Duke { - private static final String SEPARATOR = "____________________________________________________________"; - private static ArrayList tasks = new ArrayList<>(); - private static int inputStoredIndex = 0; - enum TaskType { - TODO, DEADLINE, EVENT, TASK; - } - private static ImportExportFileHandler fileHandler = new ImportExportFileHandler(tasks); - public static void main(String[] args) { - printLogo(); - greet(); - echo(); - } - - public static void printLogo(){ - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } - - public static void greet(){ - String greeting = "Hello! I'm Duke\nWhat can I do for you?"; - - System.out.println(SEPARATOR); - System.out.println(greeting); - System.out.println(SEPARATOR); - } - - public static void echo(){ - Scanner scanner = new Scanner(System.in); - String userInputSentence; - - while (true) { - userInputSentence = scanner.nextLine(); - executeCommand(userInputSentence); - } - } - - private static void executeCommand(String userInputSentence) { - boolean isEqualToTERMIATION = userInputSentence.toLowerCase().equals(Commands.TERMINATION); - boolean isEqualToLIST_PRINT = userInputSentence.toLowerCase().equals(Commands.LIST_PRINT); - boolean isEqualToSET_TASK_DONE = userInputSentence.length() >= Commands.SET_TASK_DONE.length() && - userInputSentence.substring(0, Commands.SET_TASK_DONE_COMMAND.length()).equals(Commands.SET_TASK_DONE); - boolean isEqualToDELETE = userInputSentence.startsWith(Commands.DELETE); - - if (isEqualToTERMIATION){ - exit(); - } else if (isEqualToLIST_PRINT) { - printTaskList(); - } else if (isEqualToSET_TASK_DONE) { - String[] splitCommand = userInputSentence.split(" "); - Integer setTaskId = Integer.valueOf(splitCommand[1]); - - setTaskDone(setTaskId - 1); - } else if (isEqualToDELETE) { - DeleteTask(userInputSentence); - } - } - } - - private static void DeleteTask(String userInputSentence) { - int taskId = Integer.parseInt(userInputSentence.split(" ")[1]); - try { - Task task = tasks.get(taskId-1); - tasks.remove(taskId - 1); - - System.out.println(SEPARATOR); - System.out.println("Noted. I've removed this task: "); - System.out.println(task); - System.out.printf("Now you have %d tasks in the list.\n", tasks.size()); - System.out.println(SEPARATOR); - } catch (IndexOutOfBoundsException e) { - System.out.printf("There is no task with the index %d", taskId); - } - - fileHandler.UpdateFile(tasks); - } - - private static void executeTaskAddingCommand(String userInputSentence) throws EmptyContentForTodoCommandException, UnknownCommandException{ - boolean isAddTodo = userInputSentence.length() >= Commands.ADD_TODO_COMMAND.length() && - userInputSentence.substring(0, Commands.ADD_TODO_COMMAND.length()).equals(Commands.ADD_TODO_COMMAND); - boolean isAddDeadline = userInputSentence.length() >= Commands.ADD_DEADLINE_COMMAND.length() && - userInputSentence.substring(0, Commands.ADD_DEADLINE_COMMAND.length()).equals(Commands.ADD_DEADLINE_COMMAND); - boolean isAddEvent = userInputSentence.length() >= Commands.ADD_EVENT_COMMAND.length() && - userInputSentence.substring(0, Commands.ADD_EVENT_COMMAND.length()).equals(Commands.ADD_EVENT_COMMAND); - - TaskType taskTypeToAdd = TaskType.TASK; - if (isAddTodo) { - taskTypeToAdd = TaskType.TODO; - } else if (isAddDeadline) { - taskTypeToAdd = TaskType.DEADLINE; - } else if (isAddEvent) { - taskTypeToAdd = TaskType.EVENT; - } else { - throw new UnknownCommandException(); - } - try { - switch (taskTypeToAdd) { - case TODO: { - String description = userInputSentence.substring(Commands.ADD_TODO_COMMAND.length()); - if (description == null || description.equals("")) { - throw new EmptyContentForTodoCommandException(); + String line; + ArrayList tasks = new ArrayList<>(); + Scanner sc = new Scanner(System.in); + printHorizontalLine(); + System.out.println(" Hello! I'm Duke"); + System.out.println(" What can I do for you?"); + printHorizontalLine(); + line = sc.nextLine(); + while (!line.equals("bye")) { + try { + if (line.equals("list")) { + printHorizontalLine(); + System.out.println(" Here are the tasks in your list:"); + for (int i = 0; i < tasks.size(); i++) { + System.out.println(" " + (i + 1) + ":" + tasks.get(i)); } - tasks.add(new Todo(description)); - printSuccessfulAddedMessage(); - inputStoredIndex++; - break; - } - case DEADLINE: { - String[] splitCommands = userInputSentence.substring(Commands.ADD_DEADLINE_COMMAND.length()).split("/by "); - tasks.add(new Deadline(splitCommands[0], splitCommands[1])); - printSuccessfulAddedMessage(); - inputStoredIndex++; - break; - } - case EVENT: { - String[] splitCommands = userInputSentence.substring(Commands.ADD_EVENT_COMMAND.length()).split("/at "); - tasks.add(new Event(splitCommands[0], splitCommands[1])); - printSuccessfulAddedMessage(); - inputStoredIndex++; - break; - } - default: { - /* If the user doesn't specify the task type to add, the reminder will be given. */ - System.out.println(SEPARATOR); - System.out.println("Please specify the task type."); - System.out.println(SEPARATOR); + printHorizontalLine(); + } else if (line.startsWith("done")) { + printHorizontalLine(); + int taskNumberCompleted = Integer.parseInt(line.replaceAll("\\D+", "")) - 1; + tasks.get(taskNumberCompleted).completeTask(); + System.out.println(" Nice! I've marked this task as done:"); + System.out.println(" " + ":" + tasks.get(taskNumberCompleted)); + printHorizontalLine(); + System.out.println(); + } else if (line.startsWith("todo")) { + printHorizontalLine(); + System.out.println(" Got it. I've added this task:"); + tasks.add(new ToDo(line)); + System.out.println(" " + new ToDo(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + printHorizontalLine(); + } else if (line.startsWith("deadline")) { + printHorizontalLine(); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Deadline(line)); + System.out.println(" " + new Deadline(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + printHorizontalLine(); + } else if (line.startsWith("event")) { + printHorizontalLine(); + System.out.println(" Got it. I've added this task:"); + tasks.add(new Event(line)); + System.out.println(" " + new Event(line)); + System.out.println(" Now you have " + tasks.size() + " tasks in the list."); + printHorizontalLine(); + } else if (line.isEmpty()) { + printHorizontalLine(); + System.out.println(" ☹ OOPS!!! The description of a task cannot be empty."); + printHorizontalLine(); + } else { + printHorizontalLine(); + System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + printHorizontalLine(); + System.out.println(); } + } catch (StringIndexOutOfBoundsException e) { + printHorizontalLine(); + System.out.println("The task you input has missing fields!"); + printHorizontalLine(); + } catch (Exception e) { + printHorizontalLine(); + System.out.println("Something went wrong!"); + printHorizontalLine(); } - } catch (IndexOutOfBoundsException e) { - System.out.println(SEPARATOR); - System.out.println("Wrong syntax for commands."); - System.out.println(SEPARATOR); - } - - fileHandler.UpdateFile(tasks); - } - - private static void setTaskDone(Integer setTaskId) { - try { - Task targetTask = tasks.get(setTaskId); - targetTask.setDone(); - - System.out.println(SEPARATOR); - System.out.printf("Nice! I've marked this task as done: \n[%s] %s\n", - targetTask.getStatusIcon(),targetTask.getDescription()); - System.out.println(SEPARATOR); + line = sc.nextLine(); } - catch (NullPointerException e) { - System.out.printf("duke.tasks.Task %d does not exist.\n", setTaskId); - } - - fileHandler.UpdateFile(tasks); + printHorizontalLine(); + System.out.println(" Bye. Hope to see you again soon!"); + printHorizontalLine(); } - private static void printSuccessfulAddedMessage() { - System.out.println(SEPARATOR); - System.out.println("Got it. I've added this task: "); - System.out.println(tasks.get(inputStoredIndex)); - System.out.printf("Now you have %d tasks in the list.\n", Task.getNumTotalTasks()); - System.out.println(SEPARATOR); - } - - public static void sayGoodbye(){ - String goodbyeWords = "Bye. Hope to see you again soon!"; - - System.out.println(SEPARATOR); - System.out.println(goodbyeWords); - System.out.println(SEPARATOR); - } - - protected static void exit() { - sayGoodbye(); - System.exit(0); - } - - protected static void printTaskList() { - int taskId = 1; - - System.out.println(SEPARATOR); - System.out.println("Here are the tasks in your list:"); - - for (Task task: tasks) { - if (task==null) { - break; - } else { - System.out.printf("%d.%s\n", taskId, task); - taskId++; - } - } - - System.out.println(SEPARATOR); + private static void printHorizontalLine() { + System.out.println(" ____________________________________________________________"); } } diff --git a/src/main/java/duke/ImportExportFileHandler.java b/src/main/java/duke/ImportExportFileHandler.java deleted file mode 100644 index 9568d2f5ea..0000000000 --- a/src/main/java/duke/ImportExportFileHandler.java +++ /dev/null @@ -1,66 +0,0 @@ -import tasks.*; -import java.io.IOException; -import java.io.File; -import java.io.FileWriter; -import java.util.ArrayList; -import java.util.Scanner; - -public class ImportExportFileHandler { - FileWriter fileWriter; - boolean isExistBefore; - File file; - - public ImportExportFileHandler(ArrayList tasks) { - try { - this.file = new File("files/duke.txt"); - - if (this.file.exists()) { - this.isExistBefore = true; - this.ImportFile(tasks); - } else { - this.isExistBefore = false; - this.file.getParentFile().mkdirs(); - this.file.createNewFile(); - } - } catch (IOException e) { - System.out.println("Failure: the file cannot be initialized."); - } - - } - - public void ImportFile(ArrayList tasks) { - try { - Scanner scanner = new Scanner(this.file); - String buffer; - while (scanner.hasNext()) { - buffer = scanner.nextLine(); - Task.parseTask(buffer, tasks); - } - } catch (IOException e) { - System.out.println("Error: file reading failure."); - } - - } - - public void UpdateFile(ArrayList tasks) { - try { - this.fileWriter = new FileWriter(this.file, false); - } catch (IOException e) { - System.out.println("Error: file writing failure."); - } - for (Task task: tasks) { - try { - this.fileWriter.write(task.toString() + '\n'); - - } catch (IOException e) { - System.out.println("Error: file writing failure."); - } - } - try { - this.fileWriter.close(); - } catch (IOException e) { - System.out.println("Error: file updating failure."); - } - - } -}