From e873b5657d0ce4e45cdeda7099b28474a3123d54 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 15:40:32 +0800 Subject: [PATCH 01/26] Level-1: Greet , Echo , Exit [Completed] --- src/main/java/Duke.java | 19 +++++++++++++++++++ src/main/java/myduke/MyDuke.java | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/myduke/MyDuke.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 5d313334..322e0ffb 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,7 @@ +import myduke.MyDuke; + +import java.util.Scanner; + public class Duke { public static void main(String[] args) { String logo = " ____ _ \n" @@ -6,5 +10,20 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + + MyDuke myduke = new MyDuke(); + myduke.greet(); + Scanner scan = new Scanner(System.in); + String description = scan.nextLine(); + + while(true){ + if(description.equals("bye")){ + myduke.exit(); + break; + }else{ + myduke.echo(description); + description = scan.nextLine(); + } + } } } diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java new file mode 100644 index 00000000..62061786 --- /dev/null +++ b/src/main/java/myduke/MyDuke.java @@ -0,0 +1,19 @@ +package myduke; + +public class MyDuke { + private String description; + + public void greet(){ + System.out.println("Hello! I'm Duke\nWhat can I do for you?"); + } + + public void echo(String description){ + this.description = description; + System.out.println(description); + + } + + public void exit(){ + System.out.println("Bye. Hope to see you again soon!"); + } +} From 1549455656a2a03eb4d0191c00e8f4b81c378140 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 15:55:38 +0800 Subject: [PATCH 02/26] Level-2: Add , List [Completed] --- src/main/java/Duke.java | 3 +++ src/main/java/myduke/MyDuke.java | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 322e0ffb..3f5c0d97 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -20,6 +20,9 @@ public static void main(String[] args) { if(description.equals("bye")){ myduke.exit(); break; + }else if (description.equals("list")){ + myduke.showTaskBox(); + description = scan.nextLine(); }else{ myduke.echo(description); description = scan.nextLine(); diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index 62061786..1210bb44 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -1,19 +1,30 @@ package myduke; +import java.util.ArrayList; + public class MyDuke { private String description; + ArrayList taskBox = new ArrayList(); public void greet(){ System.out.println("Hello! I'm Duke\nWhat can I do for you?"); } public void echo(String description){ - this.description = description; - System.out.println(description); + System.out.println("added: " + description); + taskBox.add(description); } public void exit(){ System.out.println("Bye. Hope to see you again soon!"); } + + public void showTaskBox() { + int counter = 1; + for(String list:taskBox){ + System.out.println(counter + "." + list); + counter++; + } + } } From e96fb56dedfedcce8d3802faa2e24a64cc2d09ea Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 16:32:48 +0800 Subject: [PATCH 03/26] Level-3: Mark as Done + A-Classes [Completed] --- src/main/java/Duke.java | 35 +++++++++++++++++++++----------- src/main/java/myduke/MyDuke.java | 13 ------------ src/main/java/myduke/Task.java | 23 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 src/main/java/myduke/Task.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3f5c0d97..a1ee44d8 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,5 +1,6 @@ -import myduke.MyDuke; +import myduke.Task; +import java.util.ArrayList; import java.util.Scanner; public class Duke { @@ -11,22 +12,32 @@ public static void main(String[] args) { + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - MyDuke myduke = new MyDuke(); - myduke.greet(); + ArrayList taskBox = new ArrayList(); Scanner scan = new Scanner(System.in); - String description = scan.nextLine(); - + System.out.println("Hello! I'm Duke\nWhat can I do for you?"); while(true){ - if(description.equals("bye")){ - myduke.exit(); + String description = scan.nextLine(); + if (description.equals("bye")){ break; - }else if (description.equals("list")){ - myduke.showTaskBox(); - description = scan.nextLine(); + }else if(description.equals("list")){ + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskBox.size(); i++) { + System.out.println((i + 1) + ". " + taskBox.get(i)); + } + + }else if(description.startsWith("done")){ + int index = Integer.parseInt(description.split(" ")[1]); + Task target = taskBox.get(index - 1); + target.markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(taskBox.get(index - 1)); }else{ - myduke.echo(description); - description = scan.nextLine(); + System.out.println("added: " + description); + taskBox.add(new Task(description)); } + + } + System.out.println("Bye. Hope to see you again soon!"); } } diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index 1210bb44..96220c15 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -1,10 +1,6 @@ package myduke; - -import java.util.ArrayList; - public class MyDuke { private String description; - ArrayList taskBox = new ArrayList(); public void greet(){ System.out.println("Hello! I'm Duke\nWhat can I do for you?"); @@ -12,19 +8,10 @@ public void greet(){ public void echo(String description){ System.out.println("added: " + description); - taskBox.add(description); - } public void exit(){ System.out.println("Bye. Hope to see you again soon!"); } - public void showTaskBox() { - int counter = 1; - for(String list:taskBox){ - System.out.println(counter + "." + list); - counter++; - } - } } diff --git a/src/main/java/myduke/Task.java b/src/main/java/myduke/Task.java new file mode 100644 index 00000000..07714ea6 --- /dev/null +++ b/src/main/java/myduke/Task.java @@ -0,0 +1,23 @@ +package myduke; + +public class Task{ + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public String getStatusIcon() { + return (isDone ? "\u2713" : "\u2718"); + } + + public void markAsDone() { + this.isDone = true; + } + + public String toString() { + return "[" + this.getStatusIcon() + "] " + this.description; + } +} From 6d44d5038c377499e95599a8c830a72becc46230 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 16:42:56 +0800 Subject: [PATCH 04/26] Level-3: Improvement on code organization by introducing a new class "MyDuke" that consist of all the methods/abilities of Duke --- src/main/java/Duke.java | 35 ++++---------------------------- src/main/java/myduke/MyDuke.java | 34 ++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a1ee44d8..b15ca965 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,4 @@ -import myduke.Task; - -import java.util.ArrayList; -import java.util.Scanner; +import myduke.MyDuke; public class Duke { public static void main(String[] args) { @@ -12,32 +9,8 @@ public static void main(String[] args) { + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); - ArrayList taskBox = new ArrayList(); - Scanner scan = new Scanner(System.in); - System.out.println("Hello! I'm Duke\nWhat can I do for you?"); - while(true){ - String description = scan.nextLine(); - if (description.equals("bye")){ - break; - }else if(description.equals("list")){ - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskBox.size(); i++) { - System.out.println((i + 1) + ". " + taskBox.get(i)); - } - - }else if(description.startsWith("done")){ - int index = Integer.parseInt(description.split(" ")[1]); - Task target = taskBox.get(index - 1); - target.markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(taskBox.get(index - 1)); - }else{ - System.out.println("added: " + description); - taskBox.add(new Task(description)); - } - - - } - System.out.println("Bye. Hope to see you again soon!"); + MyDuke duke = new MyDuke(); + duke.greet(); + duke.run(); } } diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index 96220c15..0c8a7cf8 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -1,17 +1,45 @@ package myduke; +import java.util.ArrayList; +import java.util.Scanner; + public class MyDuke { - private String description; + Scanner scan = new Scanner(System.in); + ArrayList taskBox = new ArrayList(); public void greet(){ System.out.println("Hello! I'm Duke\nWhat can I do for you?"); } - public void echo(String description){ - System.out.println("added: " + description); + public void echo(String inputs){ + System.out.println("added: " + inputs); + taskBox.add(new Task(inputs)); } public void exit(){ System.out.println("Bye. Hope to see you again soon!"); } + public void run(){ + while(true){ + String description = scan.nextLine(); + if (description.equals("bye")){ + this.exit(); + break; + }else if(description.equals("list")){ + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskBox.size(); i++) { + System.out.println((i + 1) + ". " + taskBox.get(i)); + } + + }else if(description.startsWith("done")){ + int index = Integer.parseInt(description.split(" ")[1]); + Task target = taskBox.get(index - 1); + target.markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(taskBox.get(index - 1)); + }else{ + this.echo(description); + } + } + } } From d3b4e149c88a3cab74a10cb1391199cdc18008fe Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 16:54:36 +0800 Subject: [PATCH 05/26] Level-4: ToDos, Events, Deadlines [Completed] --- src/main/java/myduke/Deadline.java | 14 +++++++ src/main/java/myduke/Event.java | 12 ++++++ src/main/java/myduke/MyDuke.java | 65 +++++++++++++++++++++++++----- src/main/java/myduke/ToDo.java | 11 +++++ 4 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 src/main/java/myduke/Deadline.java create mode 100644 src/main/java/myduke/Event.java create mode 100644 src/main/java/myduke/ToDo.java diff --git a/src/main/java/myduke/Deadline.java b/src/main/java/myduke/Deadline.java new file mode 100644 index 00000000..e7fbe45a --- /dev/null +++ b/src/main/java/myduke/Deadline.java @@ -0,0 +1,14 @@ +package myduke; + +public class Deadline extends Task { + protected String by; + + public Deadline(String description , String by) { + super(description); + this.by = by; + } + + public String toString() { + return "[D]" + super.toString() + "(by:" + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/myduke/Event.java b/src/main/java/myduke/Event.java new file mode 100644 index 00000000..8765757e --- /dev/null +++ b/src/main/java/myduke/Event.java @@ -0,0 +1,12 @@ +package myduke; + +public class Event extends Task { + protected String at; + public Event(String description , String at) { + super(description); + this.at = at; + } + public String toString() { + return "[E]" + super.toString() + "(at:" + this.at + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index 0c8a7cf8..7ca0ee35 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -1,4 +1,5 @@ package myduke; + import java.util.ArrayList; import java.util.Scanner; @@ -10,36 +11,78 @@ public void greet(){ System.out.println("Hello! I'm Duke\nWhat can I do for you?"); } - public void echo(String inputs){ - System.out.println("added: " + inputs); - taskBox.add(new Task(inputs)); + public void echo(String description){ + System.out.println("added: " + description); + taskBox.add(new Task(description)); } public void exit(){ System.out.println("Bye. Hope to see you again soon!"); } - public void run(){ + public void run() { while(true){ - String description = scan.nextLine(); - if (description.equals("bye")){ + String inputs = scan.nextLine(); + if (inputs.equals("bye")){ this.exit(); break; - }else if(description.equals("list")){ + }else if(inputs.equals("list")){ System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskBox.size(); i++) { System.out.println((i + 1) + ". " + taskBox.get(i)); } - - }else if(description.startsWith("done")){ - int index = Integer.parseInt(description.split(" ")[1]); + }else if(inputs.startsWith("done")){ + int index = Integer.parseInt(inputs.split(" ")[1]); Task target = taskBox.get(index - 1); target.markAsDone(); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskBox.get(index - 1)); + + }else if(inputs.startsWith("todo")){ + String description = inputs.substring(5); + runToDo(description); + + }else if(inputs.startsWith("deadline")){ + String info = inputs.substring(9); + String description = info.split("/by")[0]; //Description + String by = info.split("/by")[1]; // Day + runDeadline(description , by); + + }else if(inputs.startsWith("event")){ + String info = inputs.substring(6); + String description = info.split("/at")[0]; //Description + String at = info.split("/at")[1]; // Day + runEvent(description , at); + }else{ - this.echo(description); + this.echo(inputs); } } } + + public void runToDo(String description) { + System.out.println("Got it. I've added this task:"); + Task toDoTask = new ToDo(description); + taskBox.add(toDoTask); + System.out.println(toDoTask); + System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + } + + public void runDeadline(String description , String by) { + System.out.println("Got it. I've added this task:"); + Task deadlineTask = new Deadline(description , by); + taskBox.add(deadlineTask); + System.out.println(deadlineTask); + System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + + } + + public void runEvent(String description , String at) { + System.out.println("Got it. I've added this task:"); + Task eventTask = new Event(description , at); + taskBox.add(eventTask); + System.out.println(eventTask); + System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + } + } diff --git a/src/main/java/myduke/ToDo.java b/src/main/java/myduke/ToDo.java new file mode 100644 index 00000000..2df6fa8e --- /dev/null +++ b/src/main/java/myduke/ToDo.java @@ -0,0 +1,11 @@ +package myduke; + +public class ToDo extends Task { + public ToDo(String description) { + super(description); + } + + public String toString() { + return "[T]" + super.toString(); + } +} From c59e1b61c577fab32433578b182e9fc446863a15 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 16:54:36 +0800 Subject: [PATCH 06/26] Level-4: ToDos, Events, Deadlines + A-Inheritance [Completed] --- src/main/java/myduke/Deadline.java | 14 +++++++ src/main/java/myduke/Event.java | 12 ++++++ src/main/java/myduke/MyDuke.java | 65 +++++++++++++++++++++++++----- src/main/java/myduke/ToDo.java | 11 +++++ 4 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 src/main/java/myduke/Deadline.java create mode 100644 src/main/java/myduke/Event.java create mode 100644 src/main/java/myduke/ToDo.java diff --git a/src/main/java/myduke/Deadline.java b/src/main/java/myduke/Deadline.java new file mode 100644 index 00000000..e7fbe45a --- /dev/null +++ b/src/main/java/myduke/Deadline.java @@ -0,0 +1,14 @@ +package myduke; + +public class Deadline extends Task { + protected String by; + + public Deadline(String description , String by) { + super(description); + this.by = by; + } + + public String toString() { + return "[D]" + super.toString() + "(by:" + by + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/myduke/Event.java b/src/main/java/myduke/Event.java new file mode 100644 index 00000000..8765757e --- /dev/null +++ b/src/main/java/myduke/Event.java @@ -0,0 +1,12 @@ +package myduke; + +public class Event extends Task { + protected String at; + public Event(String description , String at) { + super(description); + this.at = at; + } + public String toString() { + return "[E]" + super.toString() + "(at:" + this.at + ")"; + } +} \ No newline at end of file diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index 0c8a7cf8..7ca0ee35 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -1,4 +1,5 @@ package myduke; + import java.util.ArrayList; import java.util.Scanner; @@ -10,36 +11,78 @@ public void greet(){ System.out.println("Hello! I'm Duke\nWhat can I do for you?"); } - public void echo(String inputs){ - System.out.println("added: " + inputs); - taskBox.add(new Task(inputs)); + public void echo(String description){ + System.out.println("added: " + description); + taskBox.add(new Task(description)); } public void exit(){ System.out.println("Bye. Hope to see you again soon!"); } - public void run(){ + public void run() { while(true){ - String description = scan.nextLine(); - if (description.equals("bye")){ + String inputs = scan.nextLine(); + if (inputs.equals("bye")){ this.exit(); break; - }else if(description.equals("list")){ + }else if(inputs.equals("list")){ System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskBox.size(); i++) { System.out.println((i + 1) + ". " + taskBox.get(i)); } - - }else if(description.startsWith("done")){ - int index = Integer.parseInt(description.split(" ")[1]); + }else if(inputs.startsWith("done")){ + int index = Integer.parseInt(inputs.split(" ")[1]); Task target = taskBox.get(index - 1); target.markAsDone(); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskBox.get(index - 1)); + + }else if(inputs.startsWith("todo")){ + String description = inputs.substring(5); + runToDo(description); + + }else if(inputs.startsWith("deadline")){ + String info = inputs.substring(9); + String description = info.split("/by")[0]; //Description + String by = info.split("/by")[1]; // Day + runDeadline(description , by); + + }else if(inputs.startsWith("event")){ + String info = inputs.substring(6); + String description = info.split("/at")[0]; //Description + String at = info.split("/at")[1]; // Day + runEvent(description , at); + }else{ - this.echo(description); + this.echo(inputs); } } } + + public void runToDo(String description) { + System.out.println("Got it. I've added this task:"); + Task toDoTask = new ToDo(description); + taskBox.add(toDoTask); + System.out.println(toDoTask); + System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + } + + public void runDeadline(String description , String by) { + System.out.println("Got it. I've added this task:"); + Task deadlineTask = new Deadline(description , by); + taskBox.add(deadlineTask); + System.out.println(deadlineTask); + System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + + } + + public void runEvent(String description , String at) { + System.out.println("Got it. I've added this task:"); + Task eventTask = new Event(description , at); + taskBox.add(eventTask); + System.out.println(eventTask); + System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + } + } diff --git a/src/main/java/myduke/ToDo.java b/src/main/java/myduke/ToDo.java new file mode 100644 index 00000000..2df6fa8e --- /dev/null +++ b/src/main/java/myduke/ToDo.java @@ -0,0 +1,11 @@ +package myduke; + +public class ToDo extends Task { + public ToDo(String description) { + super(description); + } + + public String toString() { + return "[T]" + super.toString(); + } +} From cd3c533467354a5673227546c968b065cab37344 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 17:05:53 +0800 Subject: [PATCH 07/26] Level-5: Handle Errors (Minimal Requirements) [Completed] --- src/main/java/myduke/DukeException.java | 8 ++++++++ src/main/java/myduke/MyDuke.java | 26 ++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/main/java/myduke/DukeException.java diff --git a/src/main/java/myduke/DukeException.java b/src/main/java/myduke/DukeException.java new file mode 100644 index 00000000..c6c79a42 --- /dev/null +++ b/src/main/java/myduke/DukeException.java @@ -0,0 +1,8 @@ +package myduke; + +public class DukeException extends Exception { + public DukeException(){} + public DukeException(String msg){ + super(msg); + } +} \ No newline at end of file diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index 7ca0ee35..cee08cb2 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -21,16 +21,17 @@ public void exit(){ } public void run() { + while(true){ String inputs = scan.nextLine(); if (inputs.equals("bye")){ - this.exit(); break; }else if(inputs.equals("list")){ System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskBox.size(); i++) { System.out.println((i + 1) + ". " + taskBox.get(i)); } + }else if(inputs.startsWith("done")){ int index = Integer.parseInt(inputs.split(" ")[1]); Task target = taskBox.get(index - 1); @@ -39,8 +40,17 @@ public void run() { System.out.println(taskBox.get(index - 1)); }else if(inputs.startsWith("todo")){ - String description = inputs.substring(5); - runToDo(description); + try { + String description = inputs; + if(inputs.length() < 5){ + throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); + }else{ + description = inputs.substring(5); + runToDo(description); + } + } catch (DukeException e) { + e.printStackTrace(); + } }else if(inputs.startsWith("deadline")){ String info = inputs.substring(9); @@ -55,17 +65,23 @@ public void run() { runEvent(description , at); }else{ - this.echo(inputs); + try{ + throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"); + }catch(DukeException e){ + e.printStackTrace(); + } } } + this.exit(); } - public void runToDo(String description) { + public void runToDo(String description){ System.out.println("Got it. I've added this task:"); Task toDoTask = new ToDo(description); taskBox.add(toDoTask); System.out.println(toDoTask); System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + } public void runDeadline(String description , String by) { From 05b03be00e3b7786f7ae136e251903047b3dba62 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 17:22:36 +0800 Subject: [PATCH 08/26] Level-7: Save [Completed] --- src/main/java/data/duke.txt | 3 + src/main/java/myduke/Deadline.java | 14 --- src/main/java/myduke/Event.java | 12 --- src/main/java/myduke/MyDuke.java | 122 +++++++++++++++++++--- src/main/java/myduke/task/Deadline.java | 19 ++++ src/main/java/myduke/task/Event.java | 20 ++++ src/main/java/myduke/{ => task}/Task.java | 4 +- src/main/java/myduke/{ => task}/ToDo.java | 5 +- 8 files changed, 157 insertions(+), 42 deletions(-) create mode 100644 src/main/java/data/duke.txt delete mode 100644 src/main/java/myduke/Deadline.java delete mode 100644 src/main/java/myduke/Event.java create mode 100644 src/main/java/myduke/task/Deadline.java create mode 100644 src/main/java/myduke/task/Event.java rename src/main/java/myduke/{ => task}/Task.java (90%) rename src/main/java/myduke/{ => task}/ToDo.java (52%) diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt new file mode 100644 index 00000000..6467b7b4 --- /dev/null +++ b/src/main/java/data/duke.txt @@ -0,0 +1,3 @@ +[T][✓] read book +[D][✓] deadline story book (by: June 6th) +[E][✓] event project meeting (at: Aug 6th 2-4pm) diff --git a/src/main/java/myduke/Deadline.java b/src/main/java/myduke/Deadline.java deleted file mode 100644 index e7fbe45a..00000000 --- a/src/main/java/myduke/Deadline.java +++ /dev/null @@ -1,14 +0,0 @@ -package myduke; - -public class Deadline extends Task { - protected String by; - - public Deadline(String description , String by) { - super(description); - this.by = by; - } - - public String toString() { - return "[D]" + super.toString() + "(by:" + by + ")"; - } -} \ No newline at end of file diff --git a/src/main/java/myduke/Event.java b/src/main/java/myduke/Event.java deleted file mode 100644 index 8765757e..00000000 --- a/src/main/java/myduke/Event.java +++ /dev/null @@ -1,12 +0,0 @@ -package myduke; - -public class Event extends Task { - protected String at; - public Event(String description , String at) { - super(description); - this.at = at; - } - public String toString() { - return "[E]" + super.toString() + "(at:" + this.at + ")"; - } -} \ No newline at end of file diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index cee08cb2..b8ccc457 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -1,5 +1,15 @@ package myduke; +import myduke.task.Deadline; +import myduke.task.Event; +import myduke.task.Task; +import myduke.task.ToDo; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; @@ -7,6 +17,85 @@ public class MyDuke { Scanner scan = new Scanner(System.in); ArrayList taskBox = new ArrayList(); + public void loadFile () { + FileReader fileReader = null; + BufferedReader bufferedReader = null; + try { + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + fileReader = new FileReader(file); + bufferedReader = new BufferedReader(fileReader); + String value = bufferedReader.readLine(); + + while (value != null) { + String description = value.substring(7).trim(); + if (value.charAt(1) == 'T') { + ToDo todo = new ToDo(description); + if (value.charAt(4) == '\u2713') { + todo.markAsDone(); + } + taskBox.add(todo); + }else if (value.charAt(1) == 'D') { + Deadline deadline = new Deadline(description); + if (value.charAt(4) == '\u2713') { + deadline.markAsDone(); + } + taskBox.add(deadline); + }else { + Event event = new Event(description); + if (value.charAt(4) == '\u2713') { + event.markAsDone(); + } + taskBox.add(event); + } + value = bufferedReader.readLine(); + } + + + + } catch (IOException e) { + e.printStackTrace(); + }finally{ + try { + if (fileReader != null){ + fileReader.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedReader != null){ + bufferedReader.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public void writeToFile () { + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + FileWriter fileWriter = null; + try { + fileWriter = new FileWriter(file); + for (Task task : taskBox) { + fileWriter.write(task.toString() + "\n"); + } + } catch (IOException e) { + e.printStackTrace(); + }finally { + if (fileWriter != null) { + try { + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + } + public void greet(){ System.out.println("Hello! I'm Duke\nWhat can I do for you?"); } @@ -21,10 +110,12 @@ public void exit(){ } public void run() { + loadFile(); while(true){ String inputs = scan.nextLine(); if (inputs.equals("bye")){ + this.exit(); break; }else if(inputs.equals("list")){ System.out.println("Here are the tasks in your list:"); @@ -36,6 +127,7 @@ public void run() { int index = Integer.parseInt(inputs.split(" ")[1]); Task target = taskBox.get(index - 1); target.markAsDone(); + writeToFile(); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskBox.get(index - 1)); @@ -53,16 +145,16 @@ public void run() { } }else if(inputs.startsWith("deadline")){ - String info = inputs.substring(9); - String description = info.split("/by")[0]; //Description - String by = info.split("/by")[1]; // Day - runDeadline(description , by); + String[] temp = inputs.split("/"); + String description = temp[0]; + String time = temp[1]; + runDeadline(description , time); }else if(inputs.startsWith("event")){ - String info = inputs.substring(6); - String description = info.split("/at")[0]; //Description - String at = info.split("/at")[1]; // Day - runEvent(description , at); + String [] temp = inputs.split("/"); + String description = temp[0]; + String time = temp[1]; + runEvent(description , time); }else{ try{ @@ -72,31 +164,35 @@ public void run() { } } } - this.exit(); } public void runToDo(String description){ System.out.println("Got it. I've added this task:"); Task toDoTask = new ToDo(description); taskBox.add(toDoTask); + writeToFile(); System.out.println(toDoTask); System.out.println("Now you have " + taskBox.size() + " tasks in the list."); } - public void runDeadline(String description , String by) { + public void runDeadline(String description , String time) { System.out.println("Got it. I've added this task:"); - Task deadlineTask = new Deadline(description , by); + Deadline deadlineTask = new Deadline(description); + deadlineTask.setTime(time); taskBox.add(deadlineTask); + writeToFile(); System.out.println(deadlineTask); System.out.println("Now you have " + taskBox.size() + " tasks in the list."); } - public void runEvent(String description , String at) { + public void runEvent(String description , String time) { System.out.println("Got it. I've added this task:"); - Task eventTask = new Event(description , at); + Event eventTask = new Event(description); taskBox.add(eventTask); + eventTask.setTime(time); + writeToFile(); System.out.println(eventTask); System.out.println("Now you have " + taskBox.size() + " tasks in the list."); } diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/myduke/task/Deadline.java new file mode 100644 index 00000000..7530fb23 --- /dev/null +++ b/src/main/java/myduke/task/Deadline.java @@ -0,0 +1,19 @@ +package myduke.task; + +public class Deadline extends Task { + protected String time; + + public Deadline(String description) { + super(description); + this.type = "D"; + this.time = ""; + } + + public void setTime(String time) { + this.time = " (by: " + time.substring(3) + ")"; + } + + public String toString() { + return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + this.time; + } +} diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java new file mode 100644 index 00000000..b7e0e408 --- /dev/null +++ b/src/main/java/myduke/task/Event.java @@ -0,0 +1,20 @@ +package myduke.task; + +public class Event extends Task { + protected String time; + public Event(String description) { + super(description); + this.type = "E"; + this.time = ""; + } + + public void setTime (String time) { + this.time = " (at: " + time.substring(3) + ")"; + } + + public String toString() { + return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + this.time; + } + + +} diff --git a/src/main/java/myduke/Task.java b/src/main/java/myduke/task/Task.java similarity index 90% rename from src/main/java/myduke/Task.java rename to src/main/java/myduke/task/Task.java index 07714ea6..748d294d 100644 --- a/src/main/java/myduke/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -1,8 +1,9 @@ -package myduke; +package myduke.task; public class Task{ protected String description; protected boolean isDone; + protected String type; public Task(String description) { this.description = description; @@ -21,3 +22,4 @@ public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } } + diff --git a/src/main/java/myduke/ToDo.java b/src/main/java/myduke/task/ToDo.java similarity index 52% rename from src/main/java/myduke/ToDo.java rename to src/main/java/myduke/task/ToDo.java index 2df6fa8e..f3bfcc6a 100644 --- a/src/main/java/myduke/ToDo.java +++ b/src/main/java/myduke/task/ToDo.java @@ -1,11 +1,12 @@ -package myduke; +package myduke.task; public class ToDo extends Task { public ToDo(String description) { super(description); + this.type = "T"; } public String toString() { - return "[T]" + super.toString(); + return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description; } } From f86d020138054a3e65ee84bc7de713520a8dc2ac Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 20:06:46 +0800 Subject: [PATCH 09/26] Level 7: Save [Bugfixed as output will show deadline and event in the description] --- src/main/java/data/duke.txt | 13 +++++++++++++ src/main/java/myduke/MyDuke.java | 19 ++++++++++++------- src/main/java/myduke/task/Deadline.java | 2 +- src/main/java/myduke/task/Event.java | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 6467b7b4..41eb6d7f 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,3 +1,16 @@ [T][✓] read book [D][✓] deadline story book (by: June 6th) [E][✓] event project meeting (at: Aug 6th 2-4pm) +[D][✘] deadline read book (by: Sunday) +[D][✘] deadline read book (by: nday) +[D][✘] deadline read book (by: nday) +[D][✘] deadline read book (by: nday) +[D][✘] deadline read book (by: nday) +[D][✘] read book (by: nday) +[D][✘] read book (by: nday) +[D][✘] read book (by: day) +[D][✘] read book (by: day) +[D][✘] read book (by: Sunday) +[E][✓] event project meeting (at: Mon 2-4pm) +[E][✘] project meeting (at: Mon 2-4pm) +[E][✘] hello (at: Mon 2-4pm) diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index b8ccc457..55d3bae6 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -30,7 +30,7 @@ public void loadFile () { String description = value.substring(7).trim(); if (value.charAt(1) == 'T') { ToDo todo = new ToDo(description); - if (value.charAt(4) == '\u2713') { + if (value.charAt(4) == '\u2713') { // if it is a tick todo.markAsDone(); } taskBox.add(todo); @@ -145,15 +145,20 @@ public void run() { } }else if(inputs.startsWith("deadline")){ - String[] temp = inputs.split("/"); - String description = temp[0]; - String time = temp[1]; + String newInput = inputs.substring(9); + //System.out.println(newInput); + String[] temp = newInput.split("/by "); + String description = temp[0].trim(); + String time = temp[1].trim(); runDeadline(description , time); }else if(inputs.startsWith("event")){ - String [] temp = inputs.split("/"); - String description = temp[0]; - String time = temp[1]; + String newInput = inputs.substring(6); + String [] temp = newInput.split("/at"); + String description = temp[0].trim(); + String time = temp[1].trim(); + System.out.println(description); + System.out.println(time); runEvent(description , time); }else{ diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/myduke/task/Deadline.java index 7530fb23..98728600 100644 --- a/src/main/java/myduke/task/Deadline.java +++ b/src/main/java/myduke/task/Deadline.java @@ -10,7 +10,7 @@ public Deadline(String description) { } public void setTime(String time) { - this.time = " (by: " + time.substring(3) + ")"; + this.time = " (by: " + time + ")"; } public String toString() { diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java index b7e0e408..7e2d56f7 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/myduke/task/Event.java @@ -9,7 +9,7 @@ public Event(String description) { } public void setTime (String time) { - this.time = " (at: " + time.substring(3) + ")"; + this.time = " (at: " + time + ")"; } public String toString() { From df5d8458d323a5dabfc053762397eeb47b3de036 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 28 Aug 2019 23:01:00 +0800 Subject: [PATCH 10/26] Level-8: Dates And Times [Bug unsolved] --- src/main/java/Duke.java | 4 ++- src/main/java/data/duke.txt | 37 +++++++++++++++---------- src/main/java/myduke/MyDuke.java | 13 +++++---- src/main/java/myduke/task/Deadline.java | 13 +++++---- src/main/java/myduke/task/Event.java | 13 +++++---- src/main/java/myduke/task/Task.java | 2 +- src/main/java/myduke/task/ToDo.java | 2 +- 7 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b15ca965..2c304104 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,7 +1,9 @@ import myduke.MyDuke; +import java.text.ParseException; + public class Duke { - public static void main(String[] args) { + public static void main(String[] args) throws ParseException { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 41eb6d7f..a775d626 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,16 +1,23 @@ [T][✓] read book -[D][✓] deadline story book (by: June 6th) -[E][✓] event project meeting (at: Aug 6th 2-4pm) -[D][✘] deadline read book (by: Sunday) -[D][✘] deadline read book (by: nday) -[D][✘] deadline read book (by: nday) -[D][✘] deadline read book (by: nday) -[D][✘] deadline read book (by: nday) -[D][✘] read book (by: nday) -[D][✘] read book (by: nday) -[D][✘] read book (by: day) -[D][✘] read book (by: day) -[D][✘] read book (by: Sunday) -[E][✓] event project meeting (at: Mon 2-4pm) -[E][✘] project meeting (at: Mon 2-4pm) -[E][✘] hello (at: Mon 2-4pm) +[D][✓] deadline story book (by: June 6th)null null null null null +[E][✓] event project meeting (at: Aug 6th 2-4pm)null null null null null +[D][✘] deadline read book (by: Sunday)null null null null null +[D][✘] deadline read book (by: nday)null null null null null +[D][✘] deadline read book (by: nday)null null null null null +[D][✘] deadline read book (by: nday)null null null null null +[D][✘] deadline read book (by: nday)null null null null null +[D][✘] read book (by: nday)null null null null null +[D][✘] read book (by: nday)null null null null null +[D][✘] read book (by: day)null null null null null +[D][✘] read book (by: day)null null null null null +[D][✘] read book (by: Sunday)null null null null null +[E][✓] event project meeting (at: Mon 2-4pm)null null null null null +[E][✘] project meeting (at: Mon 2-4pm)null null null null null +[E][✘] hello (at: Mon 2-4pm)null null null null null +[D][✘] return bookMon Dec 02 18:00:00 SGT 2019 null null null null +[D][✘] return bookMon Dec 02 18:00:00 SGT 2019 null null null null +[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null null null +[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null null +[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null +[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null +[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index 55d3bae6..c24932f8 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -10,6 +10,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.text.ParseException; import java.util.ArrayList; import java.util.Scanner; @@ -27,7 +28,7 @@ public void loadFile () { String value = bufferedReader.readLine(); while (value != null) { - String description = value.substring(7).trim(); + String description = value.substring(7); if (value.charAt(1) == 'T') { ToDo todo = new ToDo(description); if (value.charAt(4) == '\u2713') { // if it is a tick @@ -109,7 +110,7 @@ public void exit(){ System.out.println("Bye. Hope to see you again soon!"); } - public void run() { + public void run() throws ParseException { loadFile(); while(true){ @@ -181,10 +182,10 @@ public void runToDo(String description){ } - public void runDeadline(String description , String time) { + public void runDeadline(String description , String time) throws ParseException { System.out.println("Got it. I've added this task:"); Deadline deadlineTask = new Deadline(description); - deadlineTask.setTime(time); + deadlineTask.parseTime(time); taskBox.add(deadlineTask); writeToFile(); System.out.println(deadlineTask); @@ -192,11 +193,11 @@ public void runDeadline(String description , String time) { } - public void runEvent(String description , String time) { + public void runEvent(String description , String time) throws ParseException { System.out.println("Got it. I've added this task:"); Event eventTask = new Event(description); taskBox.add(eventTask); - eventTask.setTime(time); + eventTask.parseTime(time); writeToFile(); System.out.println(eventTask); System.out.println("Now you have " + taskBox.size() + " tasks in the list."); diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/myduke/task/Deadline.java index 98728600..f052cafa 100644 --- a/src/main/java/myduke/task/Deadline.java +++ b/src/main/java/myduke/task/Deadline.java @@ -1,19 +1,22 @@ package myduke.task; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + public class Deadline extends Task { - protected String time; + protected Date date; public Deadline(String description) { super(description); this.type = "D"; - this.time = ""; } - public void setTime(String time) { - this.time = " (by: " + time + ")"; + public void parseTime(String time) throws ParseException { + this.date = new SimpleDateFormat("dd/MM/yyyy hhmm").parse(time); } public String toString() { - return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + this.time; + return ("[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + " " + this.date).trim(); } } diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java index 7e2d56f7..19b2b7c2 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/myduke/task/Event.java @@ -1,19 +1,22 @@ package myduke.task; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + public class Event extends Task { - protected String time; + protected Date date; public Event(String description) { super(description); this.type = "E"; - this.time = ""; } - public void setTime (String time) { - this.time = " (at: " + time + ")"; + public void parseTime (String time) throws ParseException { + this.date = new SimpleDateFormat("dd/MM/yyyy hhmm").parse(time); } public String toString() { - return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + this.time; + return ("[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + " " + this.date).trim(); } diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index 748d294d..30294216 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -19,7 +19,7 @@ public void markAsDone() { } public String toString() { - return "[" + this.getStatusIcon() + "] " + this.description; + return ("[" + this.getStatusIcon() + "] " + this.description).trim(); } } diff --git a/src/main/java/myduke/task/ToDo.java b/src/main/java/myduke/task/ToDo.java index f3bfcc6a..08813049 100644 --- a/src/main/java/myduke/task/ToDo.java +++ b/src/main/java/myduke/task/ToDo.java @@ -7,6 +7,6 @@ public ToDo(String description) { } public String toString() { - return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description; + return ("[" + this.type + "][" + this.getStatusIcon() + "] " + this.description).trim(); } } From bc5bcd3400b7f1854cd3e884708fb47205db7220 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Sat, 31 Aug 2019 07:41:28 +0800 Subject: [PATCH 11/26] Reorganised Code & Implementation logic until [Level-4 : ToDos, Events, Deadlines] , together with the usage of switch statement and enum --- src/main/java/Duke.java | 6 +- src/main/java/myduke/Commands.java | 5 + src/main/java/myduke/MyDuke.java | 205 ------------------------ src/main/java/myduke/task/Deadline.java | 17 +- src/main/java/myduke/task/Event.java | 14 +- src/main/java/myduke/task/MyDuke.java | 122 ++++++++++++++ src/main/java/myduke/task/Task.java | 1 - src/main/java/myduke/task/ToDo.java | 3 +- 8 files changed, 147 insertions(+), 226 deletions(-) create mode 100644 src/main/java/myduke/Commands.java delete mode 100644 src/main/java/myduke/MyDuke.java create mode 100644 src/main/java/myduke/task/MyDuke.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b15ca965..62473c1f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,4 @@ -import myduke.MyDuke; +import myduke.task.MyDuke; public class Duke { public static void main(String[] args) { @@ -8,9 +8,9 @@ public static void main(String[] args) { + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + System.out.println("Hello! I'm Duke\nWhat can I do for you?"); MyDuke duke = new MyDuke(); - duke.greet(); - duke.run(); + duke.runDuke(); } } diff --git a/src/main/java/myduke/Commands.java b/src/main/java/myduke/Commands.java new file mode 100644 index 00000000..afe206cb --- /dev/null +++ b/src/main/java/myduke/Commands.java @@ -0,0 +1,5 @@ +package myduke; + +public enum Commands { + list , done , todo , deadline , event ; +} diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java deleted file mode 100644 index 55d3bae6..00000000 --- a/src/main/java/myduke/MyDuke.java +++ /dev/null @@ -1,205 +0,0 @@ -package myduke; - -import myduke.task.Deadline; -import myduke.task.Event; -import myduke.task.Task; -import myduke.task.ToDo; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Scanner; - -public class MyDuke { - Scanner scan = new Scanner(System.in); - ArrayList taskBox = new ArrayList(); - - public void loadFile () { - FileReader fileReader = null; - BufferedReader bufferedReader = null; - try { - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - fileReader = new FileReader(file); - bufferedReader = new BufferedReader(fileReader); - String value = bufferedReader.readLine(); - - while (value != null) { - String description = value.substring(7).trim(); - if (value.charAt(1) == 'T') { - ToDo todo = new ToDo(description); - if (value.charAt(4) == '\u2713') { // if it is a tick - todo.markAsDone(); - } - taskBox.add(todo); - }else if (value.charAt(1) == 'D') { - Deadline deadline = new Deadline(description); - if (value.charAt(4) == '\u2713') { - deadline.markAsDone(); - } - taskBox.add(deadline); - }else { - Event event = new Event(description); - if (value.charAt(4) == '\u2713') { - event.markAsDone(); - } - taskBox.add(event); - } - value = bufferedReader.readLine(); - } - - - - } catch (IOException e) { - e.printStackTrace(); - }finally{ - try { - if (fileReader != null){ - fileReader.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedReader != null){ - bufferedReader.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - } - - public void writeToFile () { - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - FileWriter fileWriter = null; - try { - fileWriter = new FileWriter(file); - for (Task task : taskBox) { - fileWriter.write(task.toString() + "\n"); - } - } catch (IOException e) { - e.printStackTrace(); - }finally { - if (fileWriter != null) { - try { - fileWriter.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - - } - - public void greet(){ - System.out.println("Hello! I'm Duke\nWhat can I do for you?"); - } - - public void echo(String description){ - System.out.println("added: " + description); - taskBox.add(new Task(description)); - } - - public void exit(){ - System.out.println("Bye. Hope to see you again soon!"); - } - - public void run() { - loadFile(); - - while(true){ - String inputs = scan.nextLine(); - if (inputs.equals("bye")){ - this.exit(); - break; - }else if(inputs.equals("list")){ - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskBox.size(); i++) { - System.out.println((i + 1) + ". " + taskBox.get(i)); - } - - }else if(inputs.startsWith("done")){ - int index = Integer.parseInt(inputs.split(" ")[1]); - Task target = taskBox.get(index - 1); - target.markAsDone(); - writeToFile(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(taskBox.get(index - 1)); - - }else if(inputs.startsWith("todo")){ - try { - String description = inputs; - if(inputs.length() < 5){ - throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); - }else{ - description = inputs.substring(5); - runToDo(description); - } - } catch (DukeException e) { - e.printStackTrace(); - } - - }else if(inputs.startsWith("deadline")){ - String newInput = inputs.substring(9); - //System.out.println(newInput); - String[] temp = newInput.split("/by "); - String description = temp[0].trim(); - String time = temp[1].trim(); - runDeadline(description , time); - - }else if(inputs.startsWith("event")){ - String newInput = inputs.substring(6); - String [] temp = newInput.split("/at"); - String description = temp[0].trim(); - String time = temp[1].trim(); - System.out.println(description); - System.out.println(time); - runEvent(description , time); - - }else{ - try{ - throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"); - }catch(DukeException e){ - e.printStackTrace(); - } - } - } - } - - public void runToDo(String description){ - System.out.println("Got it. I've added this task:"); - Task toDoTask = new ToDo(description); - taskBox.add(toDoTask); - writeToFile(); - System.out.println(toDoTask); - System.out.println("Now you have " + taskBox.size() + " tasks in the list."); - - } - - public void runDeadline(String description , String time) { - System.out.println("Got it. I've added this task:"); - Deadline deadlineTask = new Deadline(description); - deadlineTask.setTime(time); - taskBox.add(deadlineTask); - writeToFile(); - System.out.println(deadlineTask); - System.out.println("Now you have " + taskBox.size() + " tasks in the list."); - - } - - public void runEvent(String description , String time) { - System.out.println("Got it. I've added this task:"); - Event eventTask = new Event(description); - taskBox.add(eventTask); - eventTask.setTime(time); - writeToFile(); - System.out.println(eventTask); - System.out.println("Now you have " + taskBox.size() + " tasks in the list."); - } - -} diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/myduke/task/Deadline.java index 98728600..926702de 100644 --- a/src/main/java/myduke/task/Deadline.java +++ b/src/main/java/myduke/task/Deadline.java @@ -3,17 +3,18 @@ public class Deadline extends Task { protected String time; - public Deadline(String description) { + public Deadline(String description , String time) { super(description); - this.type = "D"; - this.time = ""; - } - - public void setTime(String time) { - this.time = " (by: " + time + ")"; + this.time = time; +// this.type = "D"; +// this.time = ""; } +// +// public void setTime(String time) { +// this.time = " (by: " + time + ")"; +// } public String toString() { - return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + this.time; + return "[D][" + this.getStatusIcon() + "] " + this.description + " (by: " + time + ")"; } } diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java index 7e2d56f7..ec70d0aa 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/myduke/task/Event.java @@ -2,18 +2,18 @@ public class Event extends Task { protected String time; - public Event(String description) { + + public Event(String description , String time) { super(description); - this.type = "E"; - this.time = ""; + this.time = time; } - public void setTime (String time) { - this.time = " (at: " + time + ")"; - } +// public void setTime (String time) { +// this.time = " (at: " + time + ")"; +// } public String toString() { - return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + this.time; + return "[E][" + this.getStatusIcon() + "] " + this.description + " (by: " + time + ")"; } diff --git a/src/main/java/myduke/task/MyDuke.java b/src/main/java/myduke/task/MyDuke.java new file mode 100644 index 00000000..59468776 --- /dev/null +++ b/src/main/java/myduke/task/MyDuke.java @@ -0,0 +1,122 @@ +package myduke.task; +import myduke.Commands; + +import java.util.ArrayList; +import java.util.Scanner; + +public class MyDuke { + ArrayList taskList = new ArrayList<>(); + Scanner inputs = new Scanner(System.in); + + private static final String WHITESPACE = " "; + + public void runDuke() { + String newString; + String[] secondBox; + String description; + String time; + + String userInput = inputs.nextLine().trim(); + while(!userInput.equals("bye")) { + String[] firstBox = firstFilter(userInput); // return a string array with commands and description +// System.out.println("commands = " + firstBox[0]); +// System.out.println("description = " + firstBox[1]); + + String firstWord = firstBox[0]; + Commands commands = Commands.valueOf(firstWord); + + switch (commands) { + case list: + showList(); + break; + case done: + runDone(userInput); + break; + case todo: + description = firstBox[1]; + runToDo(description); + break; + case deadline: + newString = firstBox[1]; + secondBox = secondFilter(newString , commands); + description = secondBox[0]; + time = secondBox[1]; + runDeadline(description , time); + break; + case event: + newString = firstBox[1]; + secondBox = secondFilter(newString , commands); + description = secondBox[0]; + time = secondBox[1]; + runEvent(description , time); + break; + } + userInput = inputs.nextLine().trim(); + + } + System.out.println("Bye. Hope to see you again soon!"); + } + + public void runDone(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + Task chosenTask = taskList.get(index - 1); + chosenTask.markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(taskList.get(index - 1)); + } + + public void runToDo(String description) { + System.out.println("Got it. I've added this task:"); + Task toDoTask = new ToDo(description); + taskList.add(toDoTask); + System.out.println(toDoTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void runDeadline(String description , String time) { + System.out.println("Got it. I've added this task:"); + Task deadlineTask = new Deadline(description , time); + taskList.add(deadlineTask); + System.out.println(deadlineTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void runEvent(String description , String time) { + System.out.println("Got it. I've added this task:"); + Task eventTask = new Event(description , time); + taskList.add(eventTask); + System.out.println(eventTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public String[] firstFilter(String userInput) { + String[] filter = userInput.split(" "); + String firstWord = filter[0]; + StringBuilder str = new StringBuilder(); + for (int i = 1 ; i < filter.length ; i++) { + str.append(filter[i]); + str.append(WHITESPACE); + } + String[] result = {firstWord , str.toString().trim()}; + + return result; + } + + public String[] secondFilter(String newString , Commands commands) { + String[] filter; + if (commands.equals(Commands.deadline)) { + filter = newString.split(" /by "); + }else{ + filter = newString.split(" /at "); + } + return filter; + } + + public void showList() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.println((i + 1) + ". " + taskList.get(i)); + } + } + +} diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index 748d294d..21d0c009 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -3,7 +3,6 @@ public class Task{ protected String description; protected boolean isDone; - protected String type; public Task(String description) { this.description = description; diff --git a/src/main/java/myduke/task/ToDo.java b/src/main/java/myduke/task/ToDo.java index f3bfcc6a..1a6196e3 100644 --- a/src/main/java/myduke/task/ToDo.java +++ b/src/main/java/myduke/task/ToDo.java @@ -3,10 +3,9 @@ public class ToDo extends Task { public ToDo(String description) { super(description); - this.type = "T"; } public String toString() { - return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description; + return "[T][" + this.getStatusIcon() + "] " + this.description; } } From 8deb4a422f0be45e1e41e1442d55b9cace8c551e Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Sat, 31 Aug 2019 11:31:45 +0800 Subject: [PATCH 12/26] Continue on re-implementation on Level-5: Handle Errors. Removed enum implementation due to problems during exception handling on enum --- src/main/java/Duke.java | 1 + src/main/java/myduke/Commands.java | 5 -- src/main/java/myduke/task/MyDuke.java | 80 ++++++++++++++++----------- 3 files changed, 50 insertions(+), 36 deletions(-) delete mode 100644 src/main/java/myduke/Commands.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 62473c1f..56f90b68 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,4 @@ +import myduke.DukeException; import myduke.task.MyDuke; public class Duke { diff --git a/src/main/java/myduke/Commands.java b/src/main/java/myduke/Commands.java deleted file mode 100644 index afe206cb..00000000 --- a/src/main/java/myduke/Commands.java +++ /dev/null @@ -1,5 +0,0 @@ -package myduke; - -public enum Commands { - list , done , todo , deadline , event ; -} diff --git a/src/main/java/myduke/task/MyDuke.java b/src/main/java/myduke/task/MyDuke.java index 59468776..328663da 100644 --- a/src/main/java/myduke/task/MyDuke.java +++ b/src/main/java/myduke/task/MyDuke.java @@ -1,5 +1,6 @@ package myduke.task; -import myduke.Commands; + +import myduke.DukeException; import java.util.ArrayList; import java.util.Scanner; @@ -23,35 +24,52 @@ public void runDuke() { // System.out.println("description = " + firstBox[1]); String firstWord = firstBox[0]; - Commands commands = Commands.valueOf(firstWord); - - switch (commands) { - case list: - showList(); - break; - case done: - runDone(userInput); - break; - case todo: - description = firstBox[1]; - runToDo(description); - break; - case deadline: - newString = firstBox[1]; - secondBox = secondFilter(newString , commands); - description = secondBox[0]; - time = secondBox[1]; - runDeadline(description , time); - break; - case event: - newString = firstBox[1]; - secondBox = secondFilter(newString , commands); - description = secondBox[0]; - time = secondBox[1]; - runEvent(description , time); - break; + try { + switch (firstWord) { + case "list": + showList(); + break; + case "done": + runDone(userInput); + break; + case "todo": + try { + if (firstBox[1].isBlank()){ + throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); + }else { + description = firstBox[1]; + runToDo(description); + } + }catch(DukeException e) { + e.printStackTrace(); + }finally{ + break; + } + + case "deadline": + newString = firstBox[1]; + secondBox = secondFilter(newString , "deadline"); + description = secondBox[0]; + time = secondBox[1]; + runDeadline(description , time); + break; + case "event": + newString = firstBox[1]; + secondBox = secondFilter(newString , "event"); + description = secondBox[0]; + time = secondBox[1]; + runEvent(description , time); + break; + default: + throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } + + + }catch(DukeException e) { + e.printStackTrace(); + }finally { + userInput = inputs.nextLine().trim(); } - userInput = inputs.nextLine().trim(); } System.out.println("Bye. Hope to see you again soon!"); @@ -102,9 +120,9 @@ public String[] firstFilter(String userInput) { return result; } - public String[] secondFilter(String newString , Commands commands) { + public String[] secondFilter(String newString , String firstWord) { String[] filter; - if (commands.equals(Commands.deadline)) { + if (firstWord.equals("deadline")) { filter = newString.split(" /by "); }else{ filter = newString.split(" /at "); From 3ce4531684e0bc0101eb7d983326b3e0acf84c45 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Sat, 31 Aug 2019 11:59:08 +0800 Subject: [PATCH 13/26] Introduce DukeTools class to consolidate all duke's function --- src/main/java/Duke.java | 1 - src/main/java/myduke/DukeTools.java | 77 +++++++++++++++++++++++ src/main/java/myduke/task/MyDuke.java | 90 ++++----------------------- 3 files changed, 89 insertions(+), 79 deletions(-) create mode 100644 src/main/java/myduke/DukeTools.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 56f90b68..62473c1f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,4 +1,3 @@ -import myduke.DukeException; import myduke.task.MyDuke; public class Duke { diff --git a/src/main/java/myduke/DukeTools.java b/src/main/java/myduke/DukeTools.java new file mode 100644 index 00000000..e8fc1c8e --- /dev/null +++ b/src/main/java/myduke/DukeTools.java @@ -0,0 +1,77 @@ +package myduke; + +import myduke.task.Deadline; +import myduke.task.Event; +import myduke.task.Task; +import myduke.task.ToDo; + +import java.util.ArrayList; + +public class DukeTools { + + ArrayList taskList = new ArrayList<>(); + + public void runDone(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + Task chosenTask = taskList.get(index - 1); + chosenTask.markAsDone(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(taskList.get(index - 1)); + } + + public void runToDo(String description) { + System.out.println("Got it. I've added this task:"); + Task toDoTask = new ToDo(description); + taskList.add(toDoTask); + System.out.println(toDoTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void runDeadline(String description , String time) { + System.out.println("Got it. I've added this task:"); + Task deadlineTask = new Deadline(description , time); + taskList.add(deadlineTask); + System.out.println(deadlineTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void runEvent(String description , String time) { + System.out.println("Got it. I've added this task:"); + Task eventTask = new Event(description , time); + taskList.add(eventTask); + System.out.println(eventTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void showList() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.println((i + 1) + ". " + taskList.get(i)); + } + } + + private static final String WHITESPACE = " "; + + public String[] firstFilter(String userInput) { + String[] filter = userInput.split(" "); + String firstWord = filter[0]; + StringBuilder str = new StringBuilder(); + for (int i = 1 ; i < filter.length ; i++) { + str.append(filter[i]); + str.append(WHITESPACE); + } + String[] result = {firstWord , str.toString().trim()}; + + return result; + } + + public String[] secondFilter(String newString , String firstWord) { + String[] filter; + if (firstWord.equals("deadline")) { + filter = newString.split(" /by "); + }else{ + filter = newString.split(" /at "); + } + return filter; + } +} diff --git a/src/main/java/myduke/task/MyDuke.java b/src/main/java/myduke/task/MyDuke.java index 328663da..ff1e5c0e 100644 --- a/src/main/java/myduke/task/MyDuke.java +++ b/src/main/java/myduke/task/MyDuke.java @@ -1,16 +1,13 @@ package myduke.task; import myduke.DukeException; - -import java.util.ArrayList; +import myduke.DukeTools; import java.util.Scanner; public class MyDuke { - ArrayList taskList = new ArrayList<>(); + DukeTools dukeTools = new DukeTools(); Scanner inputs = new Scanner(System.in); - private static final String WHITESPACE = " "; - public void runDuke() { String newString; String[] secondBox; @@ -19,18 +16,15 @@ public void runDuke() { String userInput = inputs.nextLine().trim(); while(!userInput.equals("bye")) { - String[] firstBox = firstFilter(userInput); // return a string array with commands and description -// System.out.println("commands = " + firstBox[0]); -// System.out.println("description = " + firstBox[1]); - - String firstWord = firstBox[0]; + String[] firstBox = dukeTools.firstFilter(userInput); // return a string array with commands and description + String commands = firstBox[0]; try { - switch (firstWord) { + switch (commands) { case "list": - showList(); + dukeTools.showList(); break; case "done": - runDone(userInput); + dukeTools.runDone(userInput); break; case "todo": try { @@ -38,7 +32,7 @@ public void runDuke() { throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); }else { description = firstBox[1]; - runToDo(description); + dukeTools.runToDo(description); } }catch(DukeException e) { e.printStackTrace(); @@ -48,17 +42,17 @@ public void runDuke() { case "deadline": newString = firstBox[1]; - secondBox = secondFilter(newString , "deadline"); + secondBox = dukeTools.secondFilter(newString , "deadline"); description = secondBox[0]; time = secondBox[1]; - runDeadline(description , time); + dukeTools.runDeadline(description , time); break; case "event": newString = firstBox[1]; - secondBox = secondFilter(newString , "event"); + secondBox = dukeTools.secondFilter(newString , "event"); description = secondBox[0]; time = secondBox[1]; - runEvent(description , time); + dukeTools.runEvent(description , time); break; default: throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); @@ -75,66 +69,6 @@ public void runDuke() { System.out.println("Bye. Hope to see you again soon!"); } - public void runDone(String userInput) { - int index = Integer.parseInt(userInput.split(" ")[1]); - Task chosenTask = taskList.get(index - 1); - chosenTask.markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(taskList.get(index - 1)); - } - - public void runToDo(String description) { - System.out.println("Got it. I've added this task:"); - Task toDoTask = new ToDo(description); - taskList.add(toDoTask); - System.out.println(toDoTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - } - - public void runDeadline(String description , String time) { - System.out.println("Got it. I've added this task:"); - Task deadlineTask = new Deadline(description , time); - taskList.add(deadlineTask); - System.out.println(deadlineTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - } - - public void runEvent(String description , String time) { - System.out.println("Got it. I've added this task:"); - Task eventTask = new Event(description , time); - taskList.add(eventTask); - System.out.println(eventTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - } - public String[] firstFilter(String userInput) { - String[] filter = userInput.split(" "); - String firstWord = filter[0]; - StringBuilder str = new StringBuilder(); - for (int i = 1 ; i < filter.length ; i++) { - str.append(filter[i]); - str.append(WHITESPACE); - } - String[] result = {firstWord , str.toString().trim()}; - - return result; - } - - public String[] secondFilter(String newString , String firstWord) { - String[] filter; - if (firstWord.equals("deadline")) { - filter = newString.split(" /by "); - }else{ - filter = newString.split(" /at "); - } - return filter; - } - - public void showList() { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskList.size(); i++) { - System.out.println((i + 1) + ". " + taskList.get(i)); - } - } } From 26c2bd6274e00930299c0e798178c75b08d885a4 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Sat, 31 Aug 2019 14:43:38 +0800 Subject: [PATCH 14/26] Continue on re-implementation on Level-7: Save(write file done) and added DukeFileOperation class for file related operations --- src/main/java/data/duke.txt | 19 +--- src/main/java/myduke/DukeFileOperation.java | 108 ++++++++++++++++++++ src/main/java/myduke/DukeTools.java | 22 +++- src/main/java/myduke/task/Deadline.java | 12 ++- src/main/java/myduke/task/Event.java | 7 +- src/main/java/myduke/task/Task.java | 21 ++++ src/main/java/myduke/task/ToDo.java | 1 + 7 files changed, 167 insertions(+), 23 deletions(-) create mode 100644 src/main/java/myduke/DukeFileOperation.java diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 41eb6d7f..2695750c 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,16 +1,3 @@ -[T][✓] read book -[D][✓] deadline story book (by: June 6th) -[E][✓] event project meeting (at: Aug 6th 2-4pm) -[D][✘] deadline read book (by: Sunday) -[D][✘] deadline read book (by: nday) -[D][✘] deadline read book (by: nday) -[D][✘] deadline read book (by: nday) -[D][✘] deadline read book (by: nday) -[D][✘] read book (by: nday) -[D][✘] read book (by: nday) -[D][✘] read book (by: day) -[D][✘] read book (by: day) -[D][✘] read book (by: Sunday) -[E][✓] event project meeting (at: Mon 2-4pm) -[E][✘] project meeting (at: Mon 2-4pm) -[E][✘] hello (at: Mon 2-4pm) +T | ✘ | borrow book +D | ✘ | read book | Sunday +E | ✘ | read book | Aug 6th 2-4pm diff --git a/src/main/java/myduke/DukeFileOperation.java b/src/main/java/myduke/DukeFileOperation.java new file mode 100644 index 00000000..3e0d82c8 --- /dev/null +++ b/src/main/java/myduke/DukeFileOperation.java @@ -0,0 +1,108 @@ +package myduke; + +import myduke.task.Task; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; + +public class DukeFileOperation { + private ArrayList taskList; + //DukeTools dukeTools; + public DukeFileOperation (ArrayList taskList) { + this.taskList = taskList; + //dukeTools = new DukeTools(); + } + + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + + +// public ArrayList loadFile() { +// FileReader fileReader = null; +// BufferedReader bufferedReader = null; +// try { +// fileReader = new FileReader(file); +// bufferedReader = new BufferedReader(fileReader); +// +// String inputs = bufferedReader.readLine(); +// +// while (inputs != null) { +// +// +// } +// +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } catch (IOException e) { +// e.printStackTrace(); +// }finally { +// try { +// if (fileReader != null) { +// fileReader.close(); +// } +// }catch (IOException e) { +// e.printStackTrace(); +// } +// try { +// if (bufferedReader != null) { +// bufferedReader.close(); +// } +// }catch (IOException e) { +// e.printStackTrace(); +// } +// +// } +// +// +// } + + + public void writeToFile () { + FileWriter fileWriter = null; + BufferedWriter bufferedWriter = null; + try{ + fileWriter = new FileWriter(file); + bufferedWriter = new BufferedWriter(fileWriter); + for (Task task : taskList) { + String taskType = task.getType(); + String taskStatus = task.getStatusIcon(); + String taskDescription = task.getDescription(); + String taskTime = task.getTime(); + if (taskType.equals("T")) { + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); + bufferedWriter.newLine(); + + }else{ + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); + bufferedWriter.newLine(); + } + + bufferedWriter.flush(); + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + } + + } +} diff --git a/src/main/java/myduke/DukeTools.java b/src/main/java/myduke/DukeTools.java index e8fc1c8e..61b0f9de 100644 --- a/src/main/java/myduke/DukeTools.java +++ b/src/main/java/myduke/DukeTools.java @@ -10,11 +10,14 @@ public class DukeTools { ArrayList taskList = new ArrayList<>(); + DukeFileOperation dfo = new DukeFileOperation(taskList); + private static final String WHITESPACE = " "; public void runDone(String userInput) { int index = Integer.parseInt(userInput.split(" ")[1]); Task chosenTask = taskList.get(index - 1); chosenTask.markAsDone(); + dfo.writeToFile(); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskList.get(index - 1)); } @@ -23,6 +26,7 @@ public void runToDo(String description) { System.out.println("Got it. I've added this task:"); Task toDoTask = new ToDo(description); taskList.add(toDoTask); + dfo.writeToFile(); System.out.println(toDoTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); } @@ -31,6 +35,7 @@ public void runDeadline(String description , String time) { System.out.println("Got it. I've added this task:"); Task deadlineTask = new Deadline(description , time); taskList.add(deadlineTask); + dfo.writeToFile(); System.out.println(deadlineTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); } @@ -39,6 +44,7 @@ public void runEvent(String description , String time) { System.out.println("Got it. I've added this task:"); Task eventTask = new Event(description , time); taskList.add(eventTask); + dfo.writeToFile(); System.out.println(eventTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); } @@ -50,8 +56,6 @@ public void showList() { } } - private static final String WHITESPACE = " "; - public String[] firstFilter(String userInput) { String[] filter = userInput.split(" "); String firstWord = filter[0]; @@ -74,4 +78,18 @@ public String[] secondFilter(String newString , String firstWord) { } return filter; } + +// public String writeFileFormatter(Task taskOutput) { +// String taskType = taskOutput.getType(); +// String taskStatus = taskOutput.getStatusIcon(); +// String taskDescription = taskOutput.getDescription(); +// String taskTime = taskOutput.getTime(); +// if (taskType.equals("T")) { +// return taskType + " | " + taskStatus + " | " + taskDescription; +// }else{ +// return taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime; +// } +// } + + } diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/myduke/task/Deadline.java index 926702de..c3e4f368 100644 --- a/src/main/java/myduke/task/Deadline.java +++ b/src/main/java/myduke/task/Deadline.java @@ -1,15 +1,19 @@ package myduke.task; public class Deadline extends Task { - protected String time; + //protected String time; public Deadline(String description , String time) { super(description); this.time = time; -// this.type = "D"; -// this.time = ""; + this.type = "D"; } -// + + public String getTime() { + return this.time; + } + + // // public void setTime(String time) { // this.time = " (by: " + time + ")"; // } diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java index ec70d0aa..a7c74ce4 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/myduke/task/Event.java @@ -1,11 +1,16 @@ package myduke.task; public class Event extends Task { - protected String time; + //protected String time; public Event(String description , String time) { super(description); this.time = time; + this.type = "E"; + } + + public String getTime() { + return this.time; } // public void setTime (String time) { diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index 21d0c009..ceef761b 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -1,18 +1,39 @@ package myduke.task; +import myduke.DukeTools; + public class Task{ + + protected DukeTools dukeTools = new DukeTools(); + protected String description; protected boolean isDone; + protected String type; + protected String time; public Task(String description) { this.description = description; this.isDone = false; + this.type = null; + this.time = null; } public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); } + public String getType() { + return this.type; + } + + public String getDescription() { + return this.description; + } + + public String getTime() { + return this.time; + } + public void markAsDone() { this.isDone = true; } diff --git a/src/main/java/myduke/task/ToDo.java b/src/main/java/myduke/task/ToDo.java index 1a6196e3..c7627d58 100644 --- a/src/main/java/myduke/task/ToDo.java +++ b/src/main/java/myduke/task/ToDo.java @@ -3,6 +3,7 @@ public class ToDo extends Task { public ToDo(String description) { super(description); + this.type = "T"; } public String toString() { From 305d8c21f90f7e436781d939ea86c9c1bee45a44 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Sat, 31 Aug 2019 17:27:58 +0800 Subject: [PATCH 15/26] Manage to load data from the duke.txt file correctly. --- src/main/java/data/duke.txt | 6 +- src/main/java/myduke/DukeFileOperation.java | 143 ++++++++++++++------ src/main/java/myduke/DukeTools.java | 23 +--- src/main/java/myduke/task/Event.java | 2 +- src/main/java/myduke/task/MyDuke.java | 9 +- 5 files changed, 116 insertions(+), 67 deletions(-) diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 2695750c..dea21409 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,3 +1,3 @@ -T | ✘ | borrow book -D | ✘ | read book | Sunday -E | ✘ | read book | Aug 6th 2-4pm +T | ✓ | borrow book +D | ✓ | read book | sunday +E | ✓ | not sure | 6pm diff --git a/src/main/java/myduke/DukeFileOperation.java b/src/main/java/myduke/DukeFileOperation.java index 3e0d82c8..ffc938af 100644 --- a/src/main/java/myduke/DukeFileOperation.java +++ b/src/main/java/myduke/DukeFileOperation.java @@ -1,6 +1,9 @@ package myduke; +import myduke.task.Deadline; +import myduke.task.Event; import myduke.task.Task; +import myduke.task.ToDo; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -12,57 +15,111 @@ import java.util.ArrayList; public class DukeFileOperation { - private ArrayList taskList; - //DukeTools dukeTools; - public DukeFileOperation (ArrayList taskList) { - this.taskList = taskList; - //dukeTools = new DukeTools(); + public DukeFileOperation() {} + public DukeFileOperation(ArrayList taskLst) { + this.taskLoader = taskLst; } - + ArrayList taskLoader = new ArrayList<>(); File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + public long getFileLength () { + return this.file.length(); + } -// public ArrayList loadFile() { -// FileReader fileReader = null; -// BufferedReader bufferedReader = null; -// try { -// fileReader = new FileReader(file); -// bufferedReader = new BufferedReader(fileReader); -// -// String inputs = bufferedReader.readLine(); -// -// while (inputs != null) { -// -// -// } + + public ArrayList loadFile() { + FileReader fileReader = null; + BufferedReader bufferedReader = null; + try { + fileReader = new FileReader(file); + bufferedReader = new BufferedReader(fileReader); + + String input = bufferedReader.readLine(); + + while (input != null) { + String taskType = input.split(" \\| ")[0].trim(); + String taskStatus = input.split(" \\| ")[1].trim(); + String taskDescription = input.split("\\|")[2].trim(); +// System.out.println(taskDescription); +// System.out.println(taskType); +// System.out.println(taskStatus); // -// } catch (FileNotFoundException e) { -// e.printStackTrace(); -// } catch (IOException e) { -// e.printStackTrace(); -// }finally { -// try { -// if (fileReader != null) { -// fileReader.close(); +// if (taskType.equals("T")) { +// Task todo = new ToDo(taskDescription); +// if (taskStatus.equals('\u2713')) { +// todo.markAsDone(); +// } +// taskLoader.add(todo); +// }else if (taskType.equals("D")) { +// String taskTime = input.split(" \\| ")[3]; +// Deadline deadline = new Deadline(taskDescription , taskTime); +// if (taskStatus.equals('\u2713')) { +// deadline.markAsDone(); +// } +// taskLoader.add(deadline); +// }else { +// String taskTime = input.split(" \\| ")[3]; +// Event event = new Event(taskDescription , taskTime); +// if (taskStatus.equals('\u2713')) { +// event.markAsDone(); +// } +// taskLoader.add(event); // } -// }catch (IOException e) { -// e.printStackTrace(); -// } -// try { -// if (bufferedReader != null) { -// bufferedReader.close(); -// } -// }catch (IOException e) { -// e.printStackTrace(); -// } -// -// } -// -// -// } + + switch (taskType) { + case "T" : + Task todo = new ToDo(taskDescription); + if (taskStatus.equals('\u2713')) { + todo.markAsDone(); + } + taskLoader.add(todo); + break; + case "D" : + String taskTime = input.split(" \\| ")[3]; + Task deadline = new Deadline(taskDescription , taskTime); + if (taskStatus.equals('\u2713')) { + deadline.markAsDone(); + } + taskLoader.add(deadline); + break; + case "E" : + taskTime = input.split(" \\| ")[3]; + Task event = new Event(taskDescription , taskTime); + if (taskStatus.equals('\u2713')) { + event.markAsDone(); + } + taskLoader.add(event); + break; + } + input = bufferedReader.readLine(); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }finally { + try { + if (fileReader != null) { + fileReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + return taskLoader; + + } + } - public void writeToFile () { + public void writeToFile (ArrayList taskList) { FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try{ diff --git a/src/main/java/myduke/DukeTools.java b/src/main/java/myduke/DukeTools.java index 61b0f9de..5e967edc 100644 --- a/src/main/java/myduke/DukeTools.java +++ b/src/main/java/myduke/DukeTools.java @@ -8,7 +8,6 @@ import java.util.ArrayList; public class DukeTools { - ArrayList taskList = new ArrayList<>(); DukeFileOperation dfo = new DukeFileOperation(taskList); private static final String WHITESPACE = " "; @@ -17,7 +16,7 @@ public void runDone(String userInput) { int index = Integer.parseInt(userInput.split(" ")[1]); Task chosenTask = taskList.get(index - 1); chosenTask.markAsDone(); - dfo.writeToFile(); + dfo.writeToFile(taskList); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskList.get(index - 1)); } @@ -26,7 +25,7 @@ public void runToDo(String description) { System.out.println("Got it. I've added this task:"); Task toDoTask = new ToDo(description); taskList.add(toDoTask); - dfo.writeToFile(); + dfo.writeToFile(taskList); System.out.println(toDoTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); } @@ -35,7 +34,7 @@ public void runDeadline(String description , String time) { System.out.println("Got it. I've added this task:"); Task deadlineTask = new Deadline(description , time); taskList.add(deadlineTask); - dfo.writeToFile(); + dfo.writeToFile(taskList); System.out.println(deadlineTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); } @@ -44,7 +43,7 @@ public void runEvent(String description , String time) { System.out.println("Got it. I've added this task:"); Task eventTask = new Event(description , time); taskList.add(eventTask); - dfo.writeToFile(); + dfo.writeToFile(taskList); System.out.println(eventTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); } @@ -78,18 +77,4 @@ public String[] secondFilter(String newString , String firstWord) { } return filter; } - -// public String writeFileFormatter(Task taskOutput) { -// String taskType = taskOutput.getType(); -// String taskStatus = taskOutput.getStatusIcon(); -// String taskDescription = taskOutput.getDescription(); -// String taskTime = taskOutput.getTime(); -// if (taskType.equals("T")) { -// return taskType + " | " + taskStatus + " | " + taskDescription; -// }else{ -// return taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime; -// } -// } - - } diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java index a7c74ce4..87bb6bed 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/myduke/task/Event.java @@ -18,7 +18,7 @@ public String getTime() { // } public String toString() { - return "[E][" + this.getStatusIcon() + "] " + this.description + " (by: " + time + ")"; + return "[E][" + this.getStatusIcon() + "] " + this.description + " (at: " + time + ")"; } diff --git a/src/main/java/myduke/task/MyDuke.java b/src/main/java/myduke/task/MyDuke.java index ff1e5c0e..bbb463c6 100644 --- a/src/main/java/myduke/task/MyDuke.java +++ b/src/main/java/myduke/task/MyDuke.java @@ -1,20 +1,27 @@ package myduke.task; import myduke.DukeException; +import myduke.DukeFileOperation; import myduke.DukeTools; + +import java.io.File; +import java.util.ArrayList; import java.util.Scanner; public class MyDuke { DukeTools dukeTools = new DukeTools(); Scanner inputs = new Scanner(System.in); + DukeFileOperation dfo = new DukeFileOperation(); + public void runDuke() { + //ArrayList userInput = dfo.loadFile(); String newString; String[] secondBox; String description; String time; + String userInput = inputs.nextLine(); - String userInput = inputs.nextLine().trim(); while(!userInput.equals("bye")) { String[] firstBox = dukeTools.firstFilter(userInput); // return a string array with commands and description String commands = firstBox[0]; From a9deb34eb2113d0be325eab1c0afba4d7b552a95 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Sun, 1 Sep 2019 12:42:58 +0800 Subject: [PATCH 16/26] Re-implemented on Level-7: Save [Completed] , Now able to save and load file in the right way --- src/main/java/Duke.java | 7 + src/main/java/data/duke.txt | 3 +- src/main/java/myduke/DukeFileOperation.java | 165 ---------------- src/main/java/myduke/DukeTools.java | 80 -------- src/main/java/myduke/task/Deadline.java | 12 +- src/main/java/myduke/task/Event.java | 12 +- src/main/java/myduke/task/MyDuke.java | 209 ++++++++++++++++++-- src/main/java/myduke/task/Task.java | 9 +- src/main/java/myduke/task/ToDo.java | 2 +- 9 files changed, 209 insertions(+), 290 deletions(-) delete mode 100644 src/main/java/myduke/DukeFileOperation.java delete mode 100644 src/main/java/myduke/DukeTools.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 62473c1f..f1923f94 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,6 +1,12 @@ +import myduke.task.Deadline; +import myduke.task.Event; import myduke.task.MyDuke; +import myduke.task.ToDo; + +import java.io.*; public class Duke { + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -11,6 +17,7 @@ public static void main(String[] args) { System.out.println("Hello! I'm Duke\nWhat can I do for you?"); MyDuke duke = new MyDuke(); + duke.runDuke(); } } diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index dea21409..0466f869 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,3 +1,2 @@ T | ✓ | borrow book -D | ✓ | read book | sunday -E | ✓ | not sure | 6pm +D | ✓ | hello | Sunday diff --git a/src/main/java/myduke/DukeFileOperation.java b/src/main/java/myduke/DukeFileOperation.java deleted file mode 100644 index ffc938af..00000000 --- a/src/main/java/myduke/DukeFileOperation.java +++ /dev/null @@ -1,165 +0,0 @@ -package myduke; - -import myduke.task.Deadline; -import myduke.task.Event; -import myduke.task.Task; -import myduke.task.ToDo; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; - -public class DukeFileOperation { - public DukeFileOperation() {} - public DukeFileOperation(ArrayList taskLst) { - this.taskLoader = taskLst; - } - ArrayList taskLoader = new ArrayList<>(); - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - - public long getFileLength () { - return this.file.length(); - } - - - public ArrayList loadFile() { - FileReader fileReader = null; - BufferedReader bufferedReader = null; - try { - fileReader = new FileReader(file); - bufferedReader = new BufferedReader(fileReader); - - String input = bufferedReader.readLine(); - - while (input != null) { - String taskType = input.split(" \\| ")[0].trim(); - String taskStatus = input.split(" \\| ")[1].trim(); - String taskDescription = input.split("\\|")[2].trim(); -// System.out.println(taskDescription); -// System.out.println(taskType); -// System.out.println(taskStatus); -// -// if (taskType.equals("T")) { -// Task todo = new ToDo(taskDescription); -// if (taskStatus.equals('\u2713')) { -// todo.markAsDone(); -// } -// taskLoader.add(todo); -// }else if (taskType.equals("D")) { -// String taskTime = input.split(" \\| ")[3]; -// Deadline deadline = new Deadline(taskDescription , taskTime); -// if (taskStatus.equals('\u2713')) { -// deadline.markAsDone(); -// } -// taskLoader.add(deadline); -// }else { -// String taskTime = input.split(" \\| ")[3]; -// Event event = new Event(taskDescription , taskTime); -// if (taskStatus.equals('\u2713')) { -// event.markAsDone(); -// } -// taskLoader.add(event); -// } - - switch (taskType) { - case "T" : - Task todo = new ToDo(taskDescription); - if (taskStatus.equals('\u2713')) { - todo.markAsDone(); - } - taskLoader.add(todo); - break; - case "D" : - String taskTime = input.split(" \\| ")[3]; - Task deadline = new Deadline(taskDescription , taskTime); - if (taskStatus.equals('\u2713')) { - deadline.markAsDone(); - } - taskLoader.add(deadline); - break; - case "E" : - taskTime = input.split(" \\| ")[3]; - Task event = new Event(taskDescription , taskTime); - if (taskStatus.equals('\u2713')) { - event.markAsDone(); - } - taskLoader.add(event); - break; - } - input = bufferedReader.readLine(); - } - - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - }finally { - try { - if (fileReader != null) { - fileReader.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedReader != null) { - bufferedReader.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - return taskLoader; - - } - } - - - public void writeToFile (ArrayList taskList) { - FileWriter fileWriter = null; - BufferedWriter bufferedWriter = null; - try{ - fileWriter = new FileWriter(file); - bufferedWriter = new BufferedWriter(fileWriter); - for (Task task : taskList) { - String taskType = task.getType(); - String taskStatus = task.getStatusIcon(); - String taskDescription = task.getDescription(); - String taskTime = task.getTime(); - if (taskType.equals("T")) { - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); - bufferedWriter.newLine(); - - }else{ - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); - bufferedWriter.newLine(); - } - - bufferedWriter.flush(); - } - - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (fileWriter != null) { - fileWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedWriter != null) { - bufferedWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - } - - } -} diff --git a/src/main/java/myduke/DukeTools.java b/src/main/java/myduke/DukeTools.java deleted file mode 100644 index 5e967edc..00000000 --- a/src/main/java/myduke/DukeTools.java +++ /dev/null @@ -1,80 +0,0 @@ -package myduke; - -import myduke.task.Deadline; -import myduke.task.Event; -import myduke.task.Task; -import myduke.task.ToDo; - -import java.util.ArrayList; - -public class DukeTools { - ArrayList taskList = new ArrayList<>(); - DukeFileOperation dfo = new DukeFileOperation(taskList); - private static final String WHITESPACE = " "; - - public void runDone(String userInput) { - int index = Integer.parseInt(userInput.split(" ")[1]); - Task chosenTask = taskList.get(index - 1); - chosenTask.markAsDone(); - dfo.writeToFile(taskList); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(taskList.get(index - 1)); - } - - public void runToDo(String description) { - System.out.println("Got it. I've added this task:"); - Task toDoTask = new ToDo(description); - taskList.add(toDoTask); - dfo.writeToFile(taskList); - System.out.println(toDoTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - } - - public void runDeadline(String description , String time) { - System.out.println("Got it. I've added this task:"); - Task deadlineTask = new Deadline(description , time); - taskList.add(deadlineTask); - dfo.writeToFile(taskList); - System.out.println(deadlineTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - } - - public void runEvent(String description , String time) { - System.out.println("Got it. I've added this task:"); - Task eventTask = new Event(description , time); - taskList.add(eventTask); - dfo.writeToFile(taskList); - System.out.println(eventTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - } - - public void showList() { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskList.size(); i++) { - System.out.println((i + 1) + ". " + taskList.get(i)); - } - } - - public String[] firstFilter(String userInput) { - String[] filter = userInput.split(" "); - String firstWord = filter[0]; - StringBuilder str = new StringBuilder(); - for (int i = 1 ; i < filter.length ; i++) { - str.append(filter[i]); - str.append(WHITESPACE); - } - String[] result = {firstWord , str.toString().trim()}; - - return result; - } - - public String[] secondFilter(String newString , String firstWord) { - String[] filter; - if (firstWord.equals("deadline")) { - filter = newString.split(" /by "); - }else{ - filter = newString.split(" /at "); - } - return filter; - } -} diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/myduke/task/Deadline.java index c3e4f368..1ff51d42 100644 --- a/src/main/java/myduke/task/Deadline.java +++ b/src/main/java/myduke/task/Deadline.java @@ -1,7 +1,6 @@ package myduke.task; public class Deadline extends Task { - //protected String time; public Deadline(String description , String time) { super(description); @@ -9,16 +8,7 @@ public Deadline(String description , String time) { this.type = "D"; } - public String getTime() { - return this.time; - } - - // -// public void setTime(String time) { -// this.time = " (by: " + time + ")"; -// } - public String toString() { - return "[D][" + this.getStatusIcon() + "] " + this.description + " (by: " + time + ")"; + return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + " (by: " + time + ")"; } } diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java index 87bb6bed..295717df 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/myduke/task/Event.java @@ -1,24 +1,14 @@ package myduke.task; public class Event extends Task { - //protected String time; public Event(String description , String time) { super(description); this.time = time; this.type = "E"; } - - public String getTime() { - return this.time; - } - -// public void setTime (String time) { -// this.time = " (at: " + time + ")"; -// } - public String toString() { - return "[E][" + this.getStatusIcon() + "] " + this.description + " (at: " + time + ")"; + return "[" + this.type +"][" + this.getStatusIcon() + "] " + this.description + " (at: " + time + ")"; } diff --git a/src/main/java/myduke/task/MyDuke.java b/src/main/java/myduke/task/MyDuke.java index bbb463c6..39e33c83 100644 --- a/src/main/java/myduke/task/MyDuke.java +++ b/src/main/java/myduke/task/MyDuke.java @@ -1,21 +1,132 @@ package myduke.task; import myduke.DukeException; -import myduke.DukeFileOperation; -import myduke.DukeTools; -import java.io.File; +import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class MyDuke { - DukeTools dukeTools = new DukeTools(); + private static final String WHITESPACE = " "; Scanner inputs = new Scanner(System.in); - DukeFileOperation dfo = new DukeFileOperation(); + ArrayList taskList = new ArrayList<>(100); + + public void loadFile() { + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + FileReader fileReader = null; + BufferedReader bufferedReader = null; + try { + fileReader = new FileReader(file); + bufferedReader = new BufferedReader(fileReader); + + String input = bufferedReader.readLine(); + + while (input != null) { + String[] arr = input.split(" \\| "); + String taskType = arr[0].trim(); + String taskStatus = arr[1].trim(); + String taskDescription = arr[2].trim(); + String taskTime = ""; + + if (taskType.equals("T")) { + Task todo = new ToDo(taskDescription); + if (taskStatus.equals("\u2713")) { + todo.markAsDone(); + } + taskList.add(todo); + }else if (taskType.equals("D")) { + taskTime = arr[3].trim(); + Task deadline = new Deadline(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { + deadline.markAsDone(); + } + taskList.add(deadline); + }else { + taskTime = arr[3].trim(); + Task event = new Event(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { + event.markAsDone(); + } + taskList.add(event); + } + input = bufferedReader.readLine(); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }finally { + try { + if (fileReader != null) { + fileReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + + } + } + + public void writeToFile () { + FileWriter fileWriter = null; + BufferedWriter bufferedWriter = null; + try{ + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + fileWriter = new FileWriter(file , false); + bufferedWriter = new BufferedWriter(fileWriter); + for (Task t : taskList) { + String taskType = t.getType(); + String taskStatus = t.getStatusIcon(); + String taskDescription = t.getDescription(); + String taskTime = t.getTime(); + + if (taskType.equals("T")) { + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); + bufferedWriter.newLine(); + + }else{ + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); + bufferedWriter.newLine(); + } + + + bufferedWriter.flush(); + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public void runDuke() { - //ArrayList userInput = dfo.loadFile(); + loadFile(); String newString; String[] secondBox; String description; @@ -23,15 +134,15 @@ public void runDuke() { String userInput = inputs.nextLine(); while(!userInput.equals("bye")) { - String[] firstBox = dukeTools.firstFilter(userInput); // return a string array with commands and description + String[] firstBox = firstFilter(userInput); // return a string array with commands and description String commands = firstBox[0]; try { switch (commands) { case "list": - dukeTools.showList(); + showList(); break; case "done": - dukeTools.runDone(userInput); + runDone(userInput); break; case "todo": try { @@ -39,7 +150,7 @@ public void runDuke() { throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); }else { description = firstBox[1]; - dukeTools.runToDo(description); + runToDo(description); } }catch(DukeException e) { e.printStackTrace(); @@ -49,17 +160,17 @@ public void runDuke() { case "deadline": newString = firstBox[1]; - secondBox = dukeTools.secondFilter(newString , "deadline"); + secondBox = secondFilter(newString , "deadline"); description = secondBox[0]; time = secondBox[1]; - dukeTools.runDeadline(description , time); + runDeadline(description , time); break; case "event": newString = firstBox[1]; - secondBox = dukeTools.secondFilter(newString , "event"); + secondBox = secondFilter(newString , "event"); description = secondBox[0]; time = secondBox[1]; - dukeTools.runEvent(description , time); + runEvent(description , time); break; default: throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); @@ -73,9 +184,79 @@ public void runDuke() { } } +// for (Task t : taskList) { +// System.out.println(t); +// } System.out.println("Bye. Hope to see you again soon!"); } + public void runDone(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + Task chosenTask = taskList.get(index - 1); + chosenTask.markAsDone(); + writeToFile(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(taskList.get(index - 1)); + } + + public void runToDo(String description) { + System.out.println("Got it. I've added this task:"); + Task toDoTask = new ToDo(description); + taskList.add(toDoTask); + writeToFile(); + System.out.println(toDoTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void runDeadline(String description , String time) { + System.out.println("Got it. I've added this task:"); + Task deadlineTask = new Deadline(description , time); + taskList.add(deadlineTask); + writeToFile(); + System.out.println(deadlineTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void runEvent(String description , String time) { + System.out.println("Got it. I've added this task:"); + Task eventTask = new Event(description , time); + taskList.add(eventTask); + writeToFile(); + System.out.println(eventTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void showList() { + //loadFile(); + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.println((i + 1) + ". " + taskList.get(i)); + } + } + + public String[] firstFilter(String userInput) { + String[] filter = userInput.split(" "); + String firstWord = filter[0]; + StringBuilder str = new StringBuilder(); + for (int i = 1 ; i < filter.length ; i++) { + str.append(filter[i]); + str.append(WHITESPACE); + } + String[] result = {firstWord , str.toString().trim()}; + + return result; + } + + public String[] secondFilter(String newString , String firstWord) { + String[] filter; + if (firstWord.equals("deadline")) { + filter = newString.split(" /by "); + }else{ + filter = newString.split(" /at "); + } + return filter; + } + } diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index ceef761b..a552aee6 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -1,11 +1,7 @@ package myduke.task; -import myduke.DukeTools; - public class Task{ - protected DukeTools dukeTools = new DukeTools(); - protected String description; protected boolean isDone; protected String type; @@ -14,8 +10,8 @@ public class Task{ public Task(String description) { this.description = description; this.isDone = false; - this.type = null; - this.time = null; + this.type = ""; + this.time = ""; } public String getStatusIcon() { @@ -34,6 +30,7 @@ public String getTime() { return this.time; } + public void markAsDone() { this.isDone = true; } diff --git a/src/main/java/myduke/task/ToDo.java b/src/main/java/myduke/task/ToDo.java index c7627d58..f3bfcc6a 100644 --- a/src/main/java/myduke/task/ToDo.java +++ b/src/main/java/myduke/task/ToDo.java @@ -7,6 +7,6 @@ public ToDo(String description) { } public String toString() { - return "[T][" + this.getStatusIcon() + "] " + this.description; + return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description; } } From e9e25e16805a27ef63980e0da64aac217644c1e2 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Mon, 2 Sep 2019 08:13:41 +0800 Subject: [PATCH 17/26] Level-8: Dates and Times [Completed] --- src/main/java/data/duke.txt | 10 ++++++ src/main/java/myduke/task/MyDuke.java | 46 ++++++++++++++++++++++----- src/main/java/myduke/task/Task.java | 13 ++++++++ 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 0466f869..0f4bbf4e 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,2 +1,12 @@ T | ✓ | borrow book D | ✓ | hello | Sunday +T | ✘ | borrow book +D | ✘ | return book | 2th of Dec 2019 , 06 PM +D | ✘ | return book | 2th of December 2019 , 6PM +D | ✘ | return book | 2nd of December 2019 , 6PM +D | ✓ | return book | 21st of December 2019 , 6PM +D | ✘ | return book | 31st of December 2019 , 6PM +D | ✘ | return book | 13nd of October 2019 , 9PM +D | ✘ | return book | 3nd of December 2019 , 6PM +D | ✘ | return book | 13nd of October 2019 , 9PM +D | ✘ | return book | 13rd of October 2019 , 9PM diff --git a/src/main/java/myduke/task/MyDuke.java b/src/main/java/myduke/task/MyDuke.java index 39e33c83..71a6099e 100644 --- a/src/main/java/myduke/task/MyDuke.java +++ b/src/main/java/myduke/task/MyDuke.java @@ -3,7 +3,11 @@ import myduke.DukeException; import java.io.*; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.Scanner; public class MyDuke { @@ -131,6 +135,7 @@ public void runDuke() { String[] secondBox; String description; String time; + String day; String userInput = inputs.nextLine(); while(!userInput.equals("bye")) { @@ -162,31 +167,34 @@ public void runDuke() { newString = firstBox[1]; secondBox = secondFilter(newString , "deadline"); description = secondBox[0]; - time = secondBox[1]; - runDeadline(description , time); + time = secondBox[1].trim(); + day = time.split("/")[0]; + System.out.println(day); + String timeInString = timeFormatter(time , day); + runDeadline(description , timeInString); break; case "event": newString = firstBox[1]; secondBox = secondFilter(newString , "event"); description = secondBox[0]; time = secondBox[1]; - runEvent(description , time); + day = time.split("/")[0]; + System.out.println(day); + timeInString = timeFormatter(time , day); + runEvent(description , timeInString); break; default: throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } - }catch(DukeException e) { + }catch(DukeException | ParseException e) { e.printStackTrace(); }finally { userInput = inputs.nextLine().trim(); } } -// for (Task t : taskList) { -// System.out.println(t); -// } System.out.println("Bye. Hope to see you again soon!"); } @@ -227,7 +235,6 @@ public void runEvent(String description , String time) { } public void showList() { - //loadFile(); System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskList.size(); i++) { System.out.println((i + 1) + ". " + taskList.get(i)); @@ -257,6 +264,29 @@ public String[] secondFilter(String newString , String firstWord) { return filter; } + public String timeFormatter(String dateInString , String day) throws ParseException { + DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); + DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha"); + DateFormat ndFormatter = new SimpleDateFormat("d'nd of' MMMM yyyy , ha"); + DateFormat rdFormatter = new SimpleDateFormat("d'rd of' MMMM yyyy , ha"); + DateFormat thFormatter = new SimpleDateFormat("d'th of' MMMM yyyy , ha"); + + String output; + + Date convertedDate = parser.parse(dateInString); + if (day.equals("1") || day.equals("11") || day.equals("21") || day.equals("31") ){ + output = stFormatter.format(convertedDate); + }else if (day.equals("2") || day.equals("12") || day.equals("22")) { + output = ndFormatter.format(convertedDate); + }else if (day.equals("3") || day.equals("13") || day.equals("23")) { + output = rdFormatter.format(convertedDate); + }else{ + output = thFormatter.format(convertedDate); + } + + return output; + } + } diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index a552aee6..5285a5a1 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -1,5 +1,10 @@ package myduke.task; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + public class Task{ protected String description; @@ -30,6 +35,14 @@ public String getTime() { return this.time; } +// public void setTime(String dateInString) throws ParseException { +// DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); +// DateFormat formatter = new SimpleDateFormat("d'th of' MMM yyyy , hh a "); +// Date convertedDate = parser.parse(dateInString); +// String output = formatter.format(convertedDate); +// this.time = output; +// } + public void markAsDone() { this.isDone = true; From 73495f026fcdb7c02178ad64254292f0e9659a81 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Mon, 2 Sep 2019 09:15:25 +0800 Subject: [PATCH 18/26] Merged Branch --- src/main/java/Duke.java | 11 +- src/main/java/data/duke.txt | 35 +-- src/main/java/myduke/MyDuke.java | 342 +++++++++++++++--------- src/main/java/myduke/task/Deadline.java | 14 +- src/main/java/myduke/task/Event.java | 16 +- src/main/java/myduke/task/Task.java | 32 ++- src/main/java/myduke/task/ToDo.java | 2 +- 7 files changed, 271 insertions(+), 181 deletions(-) diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 2c304104..d143f01d 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,18 +1,19 @@ -import myduke.MyDuke; -import java.text.ParseException; +import myduke.task.MyDuke; public class Duke { - public static void main(String[] args) throws ParseException { + + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; System.out.println("Hello from\n" + logo); + System.out.println("Hello! I'm Duke\nWhat can I do for you?"); MyDuke duke = new MyDuke(); - duke.greet(); - duke.run(); + + duke.runDuke(); } } diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index a775d626..0f4bbf4e 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,23 +1,12 @@ -[T][✓] read book -[D][✓] deadline story book (by: June 6th)null null null null null -[E][✓] event project meeting (at: Aug 6th 2-4pm)null null null null null -[D][✘] deadline read book (by: Sunday)null null null null null -[D][✘] deadline read book (by: nday)null null null null null -[D][✘] deadline read book (by: nday)null null null null null -[D][✘] deadline read book (by: nday)null null null null null -[D][✘] deadline read book (by: nday)null null null null null -[D][✘] read book (by: nday)null null null null null -[D][✘] read book (by: nday)null null null null null -[D][✘] read book (by: day)null null null null null -[D][✘] read book (by: day)null null null null null -[D][✘] read book (by: Sunday)null null null null null -[E][✓] event project meeting (at: Mon 2-4pm)null null null null null -[E][✘] project meeting (at: Mon 2-4pm)null null null null null -[E][✘] hello (at: Mon 2-4pm)null null null null null -[D][✘] return bookMon Dec 02 18:00:00 SGT 2019 null null null null -[D][✘] return bookMon Dec 02 18:00:00 SGT 2019 null null null null -[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null null null -[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null null -[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null -[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 null -[D][✘] return book Mon Dec 02 18:00:00 SGT 2019 +T | ✓ | borrow book +D | ✓ | hello | Sunday +T | ✘ | borrow book +D | ✘ | return book | 2th of Dec 2019 , 06 PM +D | ✘ | return book | 2th of December 2019 , 6PM +D | ✘ | return book | 2nd of December 2019 , 6PM +D | ✓ | return book | 21st of December 2019 , 6PM +D | ✘ | return book | 31st of December 2019 , 6PM +D | ✘ | return book | 13nd of October 2019 , 9PM +D | ✘ | return book | 3nd of December 2019 , 6PM +D | ✘ | return book | 13nd of October 2019 , 9PM +D | ✘ | return book | 13rd of October 2019 , 9PM diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index c24932f8..71a6099e 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -1,206 +1,292 @@ -package myduke; - -import myduke.task.Deadline; -import myduke.task.Event; -import myduke.task.Task; -import myduke.task.ToDo; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +package myduke.task; + +import myduke.DukeException; + +import java.io.*; +import java.text.DateFormat; import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.Scanner; public class MyDuke { - Scanner scan = new Scanner(System.in); - ArrayList taskBox = new ArrayList(); + private static final String WHITESPACE = " "; + Scanner inputs = new Scanner(System.in); + ArrayList taskList = new ArrayList<>(100); - public void loadFile () { + public void loadFile() { + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); FileReader fileReader = null; BufferedReader bufferedReader = null; try { - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); fileReader = new FileReader(file); bufferedReader = new BufferedReader(fileReader); - String value = bufferedReader.readLine(); - while (value != null) { - String description = value.substring(7); - if (value.charAt(1) == 'T') { - ToDo todo = new ToDo(description); - if (value.charAt(4) == '\u2713') { // if it is a tick + String input = bufferedReader.readLine(); + + while (input != null) { + String[] arr = input.split(" \\| "); + String taskType = arr[0].trim(); + String taskStatus = arr[1].trim(); + String taskDescription = arr[2].trim(); + String taskTime = ""; + + if (taskType.equals("T")) { + Task todo = new ToDo(taskDescription); + if (taskStatus.equals("\u2713")) { todo.markAsDone(); } - taskBox.add(todo); - }else if (value.charAt(1) == 'D') { - Deadline deadline = new Deadline(description); - if (value.charAt(4) == '\u2713') { + taskList.add(todo); + }else if (taskType.equals("D")) { + taskTime = arr[3].trim(); + Task deadline = new Deadline(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { deadline.markAsDone(); } - taskBox.add(deadline); + taskList.add(deadline); }else { - Event event = new Event(description); - if (value.charAt(4) == '\u2713') { + taskTime = arr[3].trim(); + Task event = new Event(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { event.markAsDone(); } - taskBox.add(event); + taskList.add(event); } - value = bufferedReader.readLine(); + input = bufferedReader.readLine(); } - - + } catch (FileNotFoundException e) { + e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); - }finally{ + }finally { try { - if (fileReader != null){ + if (fileReader != null) { fileReader.close(); } - } catch (IOException e) { + }catch (IOException e) { e.printStackTrace(); } try { - if (bufferedReader != null){ + if (bufferedReader != null) { bufferedReader.close(); } - } catch (IOException e) { + }catch (IOException e) { e.printStackTrace(); } - } + } } public void writeToFile () { - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); FileWriter fileWriter = null; - try { - fileWriter = new FileWriter(file); - for (Task task : taskBox) { - fileWriter.write(task.toString() + "\n"); + BufferedWriter bufferedWriter = null; + try{ + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + fileWriter = new FileWriter(file , false); + bufferedWriter = new BufferedWriter(fileWriter); + for (Task t : taskList) { + String taskType = t.getType(); + String taskStatus = t.getStatusIcon(); + String taskDescription = t.getDescription(); + String taskTime = t.getTime(); + + if (taskType.equals("T")) { + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); + bufferedWriter.newLine(); + + }else{ + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); + bufferedWriter.newLine(); + } + + + bufferedWriter.flush(); } + } catch (IOException e) { e.printStackTrace(); - }finally { - if (fileWriter != null) { - try { + } finally { + try { + if (fileWriter != null) { fileWriter.close(); - } catch (IOException e) { - e.printStackTrace(); } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); } } - } - public void greet(){ - System.out.println("Hello! I'm Duke\nWhat can I do for you?"); - } - public void echo(String description){ - System.out.println("added: " + description); - taskBox.add(new Task(description)); - } - public void exit(){ - System.out.println("Bye. Hope to see you again soon!"); - } - public void run() throws ParseException { + public void runDuke() { loadFile(); + String newString; + String[] secondBox; + String description; + String time; + String day; + String userInput = inputs.nextLine(); - while(true){ - String inputs = scan.nextLine(); - if (inputs.equals("bye")){ - this.exit(); - break; - }else if(inputs.equals("list")){ - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskBox.size(); i++) { - System.out.println((i + 1) + ". " + taskBox.get(i)); - } + while(!userInput.equals("bye")) { + String[] firstBox = firstFilter(userInput); // return a string array with commands and description + String commands = firstBox[0]; + try { + switch (commands) { + case "list": + showList(); + break; + case "done": + runDone(userInput); + break; + case "todo": + try { + if (firstBox[1].isBlank()){ + throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); + }else { + description = firstBox[1]; + runToDo(description); + } + }catch(DukeException e) { + e.printStackTrace(); + }finally{ + break; + } - }else if(inputs.startsWith("done")){ - int index = Integer.parseInt(inputs.split(" ")[1]); - Task target = taskBox.get(index - 1); - target.markAsDone(); - writeToFile(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(taskBox.get(index - 1)); - - }else if(inputs.startsWith("todo")){ - try { - String description = inputs; - if(inputs.length() < 5){ - throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); - }else{ - description = inputs.substring(5); - runToDo(description); - } - } catch (DukeException e) { - e.printStackTrace(); + case "deadline": + newString = firstBox[1]; + secondBox = secondFilter(newString , "deadline"); + description = secondBox[0]; + time = secondBox[1].trim(); + day = time.split("/")[0]; + System.out.println(day); + String timeInString = timeFormatter(time , day); + runDeadline(description , timeInString); + break; + case "event": + newString = firstBox[1]; + secondBox = secondFilter(newString , "event"); + description = secondBox[0]; + time = secondBox[1]; + day = time.split("/")[0]; + System.out.println(day); + timeInString = timeFormatter(time , day); + runEvent(description , timeInString); + break; + default: + throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } - }else if(inputs.startsWith("deadline")){ - String newInput = inputs.substring(9); - //System.out.println(newInput); - String[] temp = newInput.split("/by "); - String description = temp[0].trim(); - String time = temp[1].trim(); - runDeadline(description , time); - - }else if(inputs.startsWith("event")){ - String newInput = inputs.substring(6); - String [] temp = newInput.split("/at"); - String description = temp[0].trim(); - String time = temp[1].trim(); - System.out.println(description); - System.out.println(time); - runEvent(description , time); - - }else{ - try{ - throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"); - }catch(DukeException e){ - e.printStackTrace(); - } + + }catch(DukeException | ParseException e) { + e.printStackTrace(); + }finally { + userInput = inputs.nextLine().trim(); } + } + System.out.println("Bye. Hope to see you again soon!"); + } + + public void runDone(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + Task chosenTask = taskList.get(index - 1); + chosenTask.markAsDone(); + writeToFile(); + System.out.println("Nice! I've marked this task as done:"); + System.out.println(taskList.get(index - 1)); } - public void runToDo(String description){ + public void runToDo(String description) { System.out.println("Got it. I've added this task:"); Task toDoTask = new ToDo(description); - taskBox.add(toDoTask); + taskList.add(toDoTask); writeToFile(); System.out.println(toDoTask); - System.out.println("Now you have " + taskBox.size() + " tasks in the list."); - + System.out.println("Now you have " + taskList.size() + " tasks in the list."); } - public void runDeadline(String description , String time) throws ParseException { + public void runDeadline(String description , String time) { System.out.println("Got it. I've added this task:"); - Deadline deadlineTask = new Deadline(description); - deadlineTask.parseTime(time); - taskBox.add(deadlineTask); + Task deadlineTask = new Deadline(description , time); + taskList.add(deadlineTask); writeToFile(); System.out.println(deadlineTask); - System.out.println("Now you have " + taskBox.size() + " tasks in the list."); - + System.out.println("Now you have " + taskList.size() + " tasks in the list."); } - public void runEvent(String description , String time) throws ParseException { + public void runEvent(String description , String time) { System.out.println("Got it. I've added this task:"); - Event eventTask = new Event(description); - taskBox.add(eventTask); - eventTask.parseTime(time); + Task eventTask = new Event(description , time); + taskList.add(eventTask); writeToFile(); System.out.println(eventTask); - System.out.println("Now you have " + taskBox.size() + " tasks in the list."); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void showList() { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.println((i + 1) + ". " + taskList.get(i)); + } + } + + public String[] firstFilter(String userInput) { + String[] filter = userInput.split(" "); + String firstWord = filter[0]; + StringBuilder str = new StringBuilder(); + for (int i = 1 ; i < filter.length ; i++) { + str.append(filter[i]); + str.append(WHITESPACE); + } + String[] result = {firstWord , str.toString().trim()}; + + return result; + } + + public String[] secondFilter(String newString , String firstWord) { + String[] filter; + if (firstWord.equals("deadline")) { + filter = newString.split(" /by "); + }else{ + filter = newString.split(" /at "); + } + return filter; } + public String timeFormatter(String dateInString , String day) throws ParseException { + DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); + DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha"); + DateFormat ndFormatter = new SimpleDateFormat("d'nd of' MMMM yyyy , ha"); + DateFormat rdFormatter = new SimpleDateFormat("d'rd of' MMMM yyyy , ha"); + DateFormat thFormatter = new SimpleDateFormat("d'th of' MMMM yyyy , ha"); + + String output; + + Date convertedDate = parser.parse(dateInString); + if (day.equals("1") || day.equals("11") || day.equals("21") || day.equals("31") ){ + output = stFormatter.format(convertedDate); + }else if (day.equals("2") || day.equals("12") || day.equals("22")) { + output = ndFormatter.format(convertedDate); + }else if (day.equals("3") || day.equals("13") || day.equals("23")) { + output = rdFormatter.format(convertedDate); + }else{ + output = thFormatter.format(convertedDate); + } + + return output; + } + + + } diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/myduke/task/Deadline.java index f052cafa..1ff51d42 100644 --- a/src/main/java/myduke/task/Deadline.java +++ b/src/main/java/myduke/task/Deadline.java @@ -1,22 +1,14 @@ package myduke.task; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - public class Deadline extends Task { - protected Date date; - public Deadline(String description) { + public Deadline(String description , String time) { super(description); + this.time = time; this.type = "D"; } - public void parseTime(String time) throws ParseException { - this.date = new SimpleDateFormat("dd/MM/yyyy hhmm").parse(time); - } - public String toString() { - return ("[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + " " + this.date).trim(); + return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + " (by: " + time + ")"; } } diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/myduke/task/Event.java index 19b2b7c2..295717df 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/myduke/task/Event.java @@ -1,22 +1,14 @@ package myduke.task; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - public class Event extends Task { - protected Date date; - public Event(String description) { + + public Event(String description , String time) { super(description); + this.time = time; this.type = "E"; } - - public void parseTime (String time) throws ParseException { - this.date = new SimpleDateFormat("dd/MM/yyyy hhmm").parse(time); - } - public String toString() { - return ("[" + this.type + "][" + this.getStatusIcon() + "] " + this.description + " " + this.date).trim(); + return "[" + this.type +"][" + this.getStatusIcon() + "] " + this.description + " (at: " + time + ")"; } diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index 30294216..5285a5a1 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -1,25 +1,55 @@ package myduke.task; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + public class Task{ + protected String description; protected boolean isDone; protected String type; + protected String time; public Task(String description) { this.description = description; this.isDone = false; + this.type = ""; + this.time = ""; } public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); } + public String getType() { + return this.type; + } + + public String getDescription() { + return this.description; + } + + public String getTime() { + return this.time; + } + +// public void setTime(String dateInString) throws ParseException { +// DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); +// DateFormat formatter = new SimpleDateFormat("d'th of' MMM yyyy , hh a "); +// Date convertedDate = parser.parse(dateInString); +// String output = formatter.format(convertedDate); +// this.time = output; +// } + + public void markAsDone() { this.isDone = true; } public String toString() { - return ("[" + this.getStatusIcon() + "] " + this.description).trim(); + return "[" + this.getStatusIcon() + "] " + this.description; } } diff --git a/src/main/java/myduke/task/ToDo.java b/src/main/java/myduke/task/ToDo.java index 08813049..f3bfcc6a 100644 --- a/src/main/java/myduke/task/ToDo.java +++ b/src/main/java/myduke/task/ToDo.java @@ -7,6 +7,6 @@ public ToDo(String description) { } public String toString() { - return ("[" + this.type + "][" + this.getStatusIcon() + "] " + this.description).trim(); + return "[" + this.type + "][" + this.getStatusIcon() + "] " + this.description; } } From 9f0a5385b678f3fdcb2f32184280173180d3c916 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Mon, 2 Sep 2019 11:38:38 +0800 Subject: [PATCH 19/26] Level-6: Delete [Completed] --- src/main/java/data/duke.txt | 13 +------------ src/main/java/myduke/MyDuke.java | 23 +++++++++++++++++++---- src/main/java/myduke/task/Task.java | 15 --------------- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 0f4bbf4e..539981d0 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,12 +1 @@ -T | ✓ | borrow book -D | ✓ | hello | Sunday -T | ✘ | borrow book -D | ✘ | return book | 2th of Dec 2019 , 06 PM -D | ✘ | return book | 2th of December 2019 , 6PM -D | ✘ | return book | 2nd of December 2019 , 6PM -D | ✓ | return book | 21st of December 2019 , 6PM -D | ✘ | return book | 31st of December 2019 , 6PM -D | ✘ | return book | 13nd of October 2019 , 9PM -D | ✘ | return book | 3nd of December 2019 , 6PM -D | ✘ | return book | 13nd of October 2019 , 9PM -D | ✘ | return book | 13rd of October 2019 , 9PM +D | ✘ | nonono | 9th of November 1993 , 2PM diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index aecb6e0d..663e5390 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -152,6 +152,9 @@ public void runDuke() { case "done": runDone(userInput); break; + case "delete": + runDelete(userInput); + break; case "todo": try { if (firstBox[1].isBlank()){ @@ -205,36 +208,48 @@ public void runDone(String userInput) { int index = Integer.parseInt(userInput.split(" ")[1]); Task chosenTask = taskList.get(index - 1); chosenTask.markAsDone(); - writeToFile(); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskList.get(index - 1)); + writeToFile(); + } + + public void runDelete(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + System.out.println(index - 1); + System.out.println("Noted. I've removed this task:"); + System.out.println(taskList.get(index - 1)); + taskList.remove(index - 1); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + writeToFile(); } + + public void runToDo(String description) { System.out.println("Got it. I've added this task:"); Task toDoTask = new ToDo(description); taskList.add(toDoTask); - writeToFile(); System.out.println(toDoTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); + writeToFile(); } public void runDeadline(String description , String time) { System.out.println("Got it. I've added this task:"); Task deadlineTask = new Deadline(description , time); taskList.add(deadlineTask); - writeToFile(); System.out.println(deadlineTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); + writeToFile(); } public void runEvent(String description , String time) { System.out.println("Got it. I've added this task:"); Task eventTask = new Event(description , time); taskList.add(eventTask); - writeToFile(); System.out.println(eventTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); + writeToFile(); } public void showList() { diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index 5285a5a1..f48757b4 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -1,10 +1,4 @@ package myduke.task; - -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - public class Task{ protected String description; @@ -35,15 +29,6 @@ public String getTime() { return this.time; } -// public void setTime(String dateInString) throws ParseException { -// DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); -// DateFormat formatter = new SimpleDateFormat("d'th of' MMM yyyy , hh a "); -// Date convertedDate = parser.parse(dateInString); -// String output = formatter.format(convertedDate); -// this.time = output; -// } - - public void markAsDone() { this.isDone = true; } From 605e64d35043212c8c5fbdf052b3c1833d96a5eb Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Mon, 2 Sep 2019 13:23:43 +0800 Subject: [PATCH 20/26] Level-9: Find (Completed) --- src/main/java/data/duke.txt | 16 ++++------------ src/main/java/myduke/MyDuke.java | 23 +++++++++++++++++++++-- src/main/java/myduke/task/Task.java | 5 ----- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 0f4bbf4e..ef58f7c4 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,12 +1,4 @@ -T | ✓ | borrow book -D | ✓ | hello | Sunday -T | ✘ | borrow book -D | ✘ | return book | 2th of Dec 2019 , 06 PM -D | ✘ | return book | 2th of December 2019 , 6PM -D | ✘ | return book | 2nd of December 2019 , 6PM -D | ✓ | return book | 21st of December 2019 , 6PM -D | ✘ | return book | 31st of December 2019 , 6PM -D | ✘ | return book | 13nd of October 2019 , 9PM -D | ✘ | return book | 3nd of December 2019 , 6PM -D | ✘ | return book | 13nd of October 2019 , 9PM -D | ✘ | return book | 13rd of October 2019 , 9PM +D | ✘ | read book | 20th of November 1029 , 7PM +T | ✘ | buy book +T | ✓ | eat food +E | ✘ | cook food | 20th of October 2011 , 5PM diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java index aecb6e0d..6d7bf5c4 100644 --- a/src/main/java/myduke/MyDuke.java +++ b/src/main/java/myduke/MyDuke.java @@ -139,6 +139,7 @@ public void runDuke() { String description; String time; String day; + String userInput = inputs.nextLine(); while(!userInput.equals("bye")) { @@ -152,6 +153,9 @@ public void runDuke() { case "done": runDone(userInput); break; + case "find": + runFind(userInput); + break; case "todo": try { if (firstBox[1].isBlank()){ @@ -172,7 +176,6 @@ public void runDuke() { description = secondBox[0]; time = secondBox[1].trim(); day = time.split("/")[0]; - System.out.println(day); String timeInString = timeFormatter(time , day); runDeadline(description , timeInString); break; @@ -182,7 +185,6 @@ public void runDuke() { description = secondBox[0]; time = secondBox[1]; day = time.split("/")[0]; - System.out.println(day); timeInString = timeFormatter(time , day); runEvent(description , timeInString); break; @@ -210,6 +212,23 @@ public void runDone(String userInput) { System.out.println(taskList.get(index - 1)); } + public void runFind(String userInput) { + int index = 0 ; + ArrayList searchResults = new ArrayList<>(); + System.out.println("Here are the matching tasks in your list:"); + String keyword = userInput.split(" ")[1]; + for (Task task : taskList) { + String description = task.getDescription(); + if (description.contains(keyword)) { + searchResults.add(task); + } + } + for (Task results : searchResults) { + index++; + System.out.println(index + "." + results); + } + } + public void runToDo(String description) { System.out.println("Got it. I've added this task:"); Task toDoTask = new ToDo(description); diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/myduke/task/Task.java index 5285a5a1..6d23f251 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/myduke/task/Task.java @@ -1,10 +1,5 @@ package myduke.task; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - public class Task{ protected String description; From b5f70250b9df39abefaee13c8bf92ad6a4c4c2dd Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Tue, 3 Sep 2019 00:19:11 +0800 Subject: [PATCH 21/26] added Ui Class --- src/main/java/{myduke/task => }/Deadline.java | 2 - src/main/java/Duke.java | 317 ++++++++++++++++- src/main/java/{myduke => }/DukeException.java | 2 - src/main/java/{myduke/task => }/Event.java | 2 - src/main/java/{myduke/task => }/Task.java | 1 - src/main/java/{myduke/task => }/ToDo.java | 2 - src/main/java/Ui.java | 84 +++++ src/main/java/data/duke.txt | 9 +- src/main/java/myduke/MyDuke.java | 333 ------------------ 9 files changed, 393 insertions(+), 359 deletions(-) rename src/main/java/{myduke/task => }/Deadline.java (93%) rename src/main/java/{myduke => }/DukeException.java (89%) rename src/main/java/{myduke/task => }/Event.java (93%) rename src/main/java/{myduke/task => }/Task.java (97%) rename src/main/java/{myduke/task => }/ToDo.java (92%) create mode 100644 src/main/java/Ui.java delete mode 100644 src/main/java/myduke/MyDuke.java diff --git a/src/main/java/myduke/task/Deadline.java b/src/main/java/Deadline.java similarity index 93% rename from src/main/java/myduke/task/Deadline.java rename to src/main/java/Deadline.java index 1ff51d42..606d7f9b 100644 --- a/src/main/java/myduke/task/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,5 +1,3 @@ -package myduke.task; - public class Deadline extends Task { public Deadline(String description , String time) { diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 3120f004..75feb62f 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,17 +1,312 @@ -import myduke.MyDuke; +import java.io.*; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Scanner; + public class Duke { + private Ui ui; + + public Duke() { + ui = new Ui(); + } + private static final String WHITESPACE = " "; + Scanner inputs = new Scanner(System.in); + ArrayList taskList = new ArrayList<>(100); + + public void loadFile() { + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + FileReader fileReader = null; + BufferedReader bufferedReader = null; + try { + fileReader = new FileReader(file); + bufferedReader = new BufferedReader(fileReader); + + String input = bufferedReader.readLine(); + + while (input != null) { + String[] arr = input.split(" \\| "); + String taskType = arr[0].trim(); + String taskStatus = arr[1].trim(); + String taskDescription = arr[2].trim(); + String taskTime = ""; + + if (taskType.equals("T")) { + Task todo = new ToDo(taskDescription); + if (taskStatus.equals("\u2713")) { + todo.markAsDone(); + } + taskList.add(todo); + }else if (taskType.equals("D")) { + taskTime = arr[3].trim(); + Task deadline = new Deadline(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { + deadline.markAsDone(); + } + taskList.add(deadline); + }else { + taskTime = arr[3].trim(); + Task event = new Event(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { + event.markAsDone(); + } + taskList.add(event); + } + input = bufferedReader.readLine(); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }finally { + try { + if (fileReader != null) { + fileReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + + } + } + + public void writeToFile () { + FileWriter fileWriter = null; + BufferedWriter bufferedWriter = null; + try{ + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + fileWriter = new FileWriter(file , false); + bufferedWriter = new BufferedWriter(fileWriter); + for (Task t : taskList) { + String taskType = t.getType(); + String taskStatus = t.getStatusIcon(); + String taskDescription = t.getDescription(); + String taskTime = t.getTime(); + + if (taskType.equals("T")) { + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); + bufferedWriter.newLine(); + + }else{ + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); + bufferedWriter.newLine(); + } + + + bufferedWriter.flush(); + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public void run() { + ui.showWelcome(); + loadFile(); + String newString; + String[] secondBox; + String description; + String time; + String day; + + String userInput = inputs.nextLine(); + + while(!userInput.equals("bye")) { + String[] firstBox = firstFilter(userInput); // return a string array with commands and description + String commands = firstBox[0]; + try { + switch (commands) { + case "list": + ui.showTaskList(taskList); + break; + + case "done": + runDone(userInput); + break; + + case "delete": + runDelete(userInput); + break; + + case "find": + runFind(userInput); + break; + + case "todo": + try { + if (firstBox[1].isBlank()){ + throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); + }else { + description = firstBox[1]; + runToDo(description); + } + }catch(DukeException e) { + e.printStackTrace(); + }finally{ + break; + } + + case "deadline": + newString = firstBox[1]; + secondBox = secondFilter(newString , "deadline"); + description = secondBox[0]; + time = secondBox[1].trim(); + day = time.split("/")[0]; + String timeInString = timeFormatter(time , day); + runDeadline(description , timeInString); + break; + + case "event": + newString = firstBox[1]; + secondBox = secondFilter(newString , "event"); + description = secondBox[0]; + time = secondBox[1]; + day = time.split("/")[0]; + timeInString = timeFormatter(time , day); + runEvent(description , timeInString); + break; + + default: + throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + } - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - System.out.println("Hello! I'm Duke\nWhat can I do for you?"); - MyDuke duke = new MyDuke(); + }catch(DukeException | ParseException e) { + e.printStackTrace(); + }finally { + userInput = inputs.nextLine().trim(); + } + + } + ui.showExit(); + } + + public void runDone(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + Task chosenTask = taskList.get(index - 1); + chosenTask.markAsDone(); + ui.showDone(taskList , index); + writeToFile(); + } + + public void runDelete(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + ui.showDelete(taskList , index); + taskList.remove(index - 1); + ui.showTaskListSize(taskList); + writeToFile(); + } + + public void runFind(String userInput) { + int index = 0 ; + ArrayList searchResults = new ArrayList<>(); + String keyword = userInput.split(" ")[1]; + for (Task task : taskList) { + String description = task.getDescription(); + if (description.contains(keyword)) { + searchResults.add(task); + } + } + ui.showMatchTasks(taskList , index); + } + + public void runToDo(String description) { + Task toDoTask = new ToDo(description); + taskList.add(toDoTask); + writeToFile(); + } + + public void runDeadline(String description , String time) { + + Task deadlineTask = new Deadline(description , time); + taskList.add(deadlineTask); + ui.showAddDeadlineTask(taskList , deadlineTask); + writeToFile(); + } + + public void runEvent(String description , String time) { + Task eventTask = new Event(description , time); + taskList.add(eventTask); + ui.showAddEventTask(taskList , eventTask); + writeToFile(); + } + + public String[] firstFilter(String userInput) { + String[] filter = userInput.split(" "); + String firstWord = filter[0]; + StringBuilder str = new StringBuilder(); + for (int i = 1 ; i < filter.length ; i++) { + str.append(filter[i]); + str.append(WHITESPACE); + } + String[] result = {firstWord , str.toString().trim()}; + + return result; + } + + public String[] secondFilter(String newString , String firstWord) { + String[] filter; + if (firstWord.equals("deadline")) { + filter = newString.split(" /by "); + }else{ + filter = newString.split(" /at "); + } + return filter; + } + + public String timeFormatter(String dateInString , String day) throws ParseException { + DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); + DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha"); + DateFormat ndFormatter = new SimpleDateFormat("d'nd of' MMMM yyyy , ha"); + DateFormat rdFormatter = new SimpleDateFormat("d'rd of' MMMM yyyy , ha"); + DateFormat thFormatter = new SimpleDateFormat("d'th of' MMMM yyyy , ha"); + + String output; + + Date convertedDate = parser.parse(dateInString); + if (day.equals("1")){ + output = stFormatter.format(convertedDate); + }else if (day.equals("2")) { + output = ndFormatter.format(convertedDate); + }else if (day.equals("3")) { + output = rdFormatter.format(convertedDate); + }else{ + output = thFormatter.format(convertedDate); + } + + return output; + } + + + public static void main(String[] args) { + new Duke().run(); - duke.runDuke(); } } diff --git a/src/main/java/myduke/DukeException.java b/src/main/java/DukeException.java similarity index 89% rename from src/main/java/myduke/DukeException.java rename to src/main/java/DukeException.java index c6c79a42..8f852484 100644 --- a/src/main/java/myduke/DukeException.java +++ b/src/main/java/DukeException.java @@ -1,5 +1,3 @@ -package myduke; - public class DukeException extends Exception { public DukeException(){} public DukeException(String msg){ diff --git a/src/main/java/myduke/task/Event.java b/src/main/java/Event.java similarity index 93% rename from src/main/java/myduke/task/Event.java rename to src/main/java/Event.java index 295717df..2b70e37a 100644 --- a/src/main/java/myduke/task/Event.java +++ b/src/main/java/Event.java @@ -1,5 +1,3 @@ -package myduke.task; - public class Event extends Task { public Event(String description , String time) { diff --git a/src/main/java/myduke/task/Task.java b/src/main/java/Task.java similarity index 97% rename from src/main/java/myduke/task/Task.java rename to src/main/java/Task.java index f48757b4..13186ada 100644 --- a/src/main/java/myduke/task/Task.java +++ b/src/main/java/Task.java @@ -1,4 +1,3 @@ -package myduke.task; public class Task{ protected String description; diff --git a/src/main/java/myduke/task/ToDo.java b/src/main/java/ToDo.java similarity index 92% rename from src/main/java/myduke/task/ToDo.java rename to src/main/java/ToDo.java index f3bfcc6a..e401f61d 100644 --- a/src/main/java/myduke/task/ToDo.java +++ b/src/main/java/ToDo.java @@ -1,5 +1,3 @@ -package myduke.task; - public class ToDo extends Task { public ToDo(String description) { super(description); diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 00000000..e445f6c2 --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,84 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Ui { + private Scanner scanner; + + public Ui() { + scanner = new Scanner(System.in); + } + + public String readUserInput() { + return scanner.nextLine(); + } + + public void showWelcome() { + String logo = " ____ _ \n" + + "| _ \\ _ _| | _____ \n" + + "| | | | | | | |/ / _ \\\n" + + "| |_| | |_| | < __/\n" + + "|____/ \\__,_|_|\\_\\___|\n"; + System.out.println("Hello from\n" + logo); + System.out.println("Hello! I'm Duke\nWhat can I do for you?"); + } + + public void showTaskList(ArrayList taskList) { + System.out.println("Here are the tasks in your list:"); + for (int i = 0; i < taskList.size(); i++) { + System.out.println((i + 1) + ". " + taskList.get(i)); + } + } + + public void showExit() { + System.out.println("Bye. Hope to see you again soon!"); + } + + public void showDone(ArrayList taskList , int index) { + System.out.println("Nice! I've marked this task as done:"); + System.out.println(taskList.get(index - 1)); + } + + public void showDelete(ArrayList taskList , int index) { + System.out.println("Noted. I've removed this task:"); + System.out.println(taskList.get(index - 1)); + } + + public void showTaskListSize(ArrayList taskList) { + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void showMatchTasks(ArrayList searchResults , int index) { + System.out.println("Here are the matching tasks in your list:"); + for (Task results : searchResults) { + index++; + System.out.println(index + "." + results); + } + } + + public void showAddTodoTask(ArrayList taskList , Task todoTask) { + System.out.println("Got it. I've added this task:"); + System.out.println(todoTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void showAddDeadlineTask(ArrayList taskList , Task deadlineTask) { + System.out.println("Got it. I've added this task:"); + System.out.println(deadlineTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void showAddEventTask(ArrayList taskList , Task eventTask) { + System.out.println("Got it. I've added this task:"); + System.out.println(eventTask); + System.out.println("Now you have " + taskList.size() + " tasks in the list."); + } + + public void showError (String message) { + System.out.println("☹ OOPS!!!" + message); + } + + public void showLine() { + System.out.println("____________________________________________________________"); + } + +} diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index beffb288..94f8fb0d 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,6 +1,3 @@ -D | ✘ | read book | 20th of November 1029 , 7PM -T | ✓ | buy book -E | ✘ | cook food | 20th of October 2011 , 5PM -E | ✘ | hello | 12nd of November 2019 , 7PM -T | ✘ | hello -D | ✘ | jjjj | 12nd of December 2012 , 12PM +D | ✓ | read book | 20th of November 1029 , 7PM +E | ✓ | cook food | 20th of October 2011 , 5PM +D | ✘ | jogging | 12nd of December 2012 , 7PM diff --git a/src/main/java/myduke/MyDuke.java b/src/main/java/myduke/MyDuke.java deleted file mode 100644 index 7d91784e..00000000 --- a/src/main/java/myduke/MyDuke.java +++ /dev/null @@ -1,333 +0,0 @@ -package myduke; - -import myduke.task.Deadline; -import myduke.task.Event; -import myduke.task.Task; -import myduke.task.ToDo; - -import java.io.*; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.Scanner; - -public class MyDuke { - private static final String WHITESPACE = " "; - Scanner inputs = new Scanner(System.in); - ArrayList taskList = new ArrayList<>(100); - - public void loadFile() { - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - FileReader fileReader = null; - BufferedReader bufferedReader = null; - try { - fileReader = new FileReader(file); - bufferedReader = new BufferedReader(fileReader); - - String input = bufferedReader.readLine(); - - while (input != null) { - String[] arr = input.split(" \\| "); - String taskType = arr[0].trim(); - String taskStatus = arr[1].trim(); - String taskDescription = arr[2].trim(); - String taskTime = ""; - - if (taskType.equals("T")) { - Task todo = new ToDo(taskDescription); - if (taskStatus.equals("\u2713")) { - todo.markAsDone(); - } - taskList.add(todo); - }else if (taskType.equals("D")) { - taskTime = arr[3].trim(); - Task deadline = new Deadline(taskDescription , taskTime); - if (taskStatus.equals("\u2713")) { - deadline.markAsDone(); - } - taskList.add(deadline); - }else { - taskTime = arr[3].trim(); - Task event = new Event(taskDescription , taskTime); - if (taskStatus.equals("\u2713")) { - event.markAsDone(); - } - taskList.add(event); - } - input = bufferedReader.readLine(); - } - - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - }finally { - try { - if (fileReader != null) { - fileReader.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedReader != null) { - bufferedReader.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - - } - } - - public void writeToFile () { - FileWriter fileWriter = null; - BufferedWriter bufferedWriter = null; - try{ - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - fileWriter = new FileWriter(file , false); - bufferedWriter = new BufferedWriter(fileWriter); - for (Task t : taskList) { - String taskType = t.getType(); - String taskStatus = t.getStatusIcon(); - String taskDescription = t.getDescription(); - String taskTime = t.getTime(); - - if (taskType.equals("T")) { - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); - bufferedWriter.newLine(); - - }else{ - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); - bufferedWriter.newLine(); - } - - - bufferedWriter.flush(); - } - - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (fileWriter != null) { - fileWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedWriter != null) { - bufferedWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - } - - } - - - - - public void runDuke() { - loadFile(); - String newString; - String[] secondBox; - String description; - String time; - String day; - - String userInput = inputs.nextLine(); - - while(!userInput.equals("bye")) { - String[] firstBox = firstFilter(userInput); // return a string array with commands and description - String commands = firstBox[0]; - try { - switch (commands) { - case "list": - showList(); - break; - - case "done": - runDone(userInput); - break; - - case "delete": - runDelete(userInput); - break; - - case "find": - runFind(userInput); - break; - - case "todo": - try { - if (firstBox[1].isBlank()){ - throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); - }else { - description = firstBox[1]; - runToDo(description); - } - }catch(DukeException e) { - e.printStackTrace(); - }finally{ - break; - } - - case "deadline": - newString = firstBox[1]; - secondBox = secondFilter(newString , "deadline"); - description = secondBox[0]; - time = secondBox[1].trim(); - day = time.split("/")[0]; - String timeInString = timeFormatter(time , day); - runDeadline(description , timeInString); - break; - - case "event": - newString = firstBox[1]; - secondBox = secondFilter(newString , "event"); - description = secondBox[0]; - time = secondBox[1]; - day = time.split("/")[0]; - timeInString = timeFormatter(time , day); - runEvent(description , timeInString); - break; - - default: - throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); - } - - - }catch(DukeException | ParseException e) { - e.printStackTrace(); - }finally { - userInput = inputs.nextLine().trim(); - } - - } - System.out.println("Bye. Hope to see you again soon!"); - } - - public void runDone(String userInput) { - int index = Integer.parseInt(userInput.split(" ")[1]); - Task chosenTask = taskList.get(index - 1); - chosenTask.markAsDone(); - System.out.println("Nice! I've marked this task as done:"); - System.out.println(taskList.get(index - 1)); - writeToFile(); - } - - public void runDelete(String userInput) { - int index = Integer.parseInt(userInput.split(" ")[1]); - System.out.println(index - 1); - System.out.println("Noted. I've removed this task:"); - System.out.println(taskList.get(index - 1)); - taskList.remove(index - 1); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - writeToFile(); - } - - public void runFind(String userInput) { - int index = 0 ; - ArrayList searchResults = new ArrayList<>(); - System.out.println("Here are the matching tasks in your list:"); - String keyword = userInput.split(" ")[1]; - for (Task task : taskList) { - String description = task.getDescription(); - if (description.contains(keyword)) { - searchResults.add(task); - } - } - for (Task results : searchResults) { - index++; - System.out.println(index + "." + results); - } - } - - public void runToDo(String description) { - System.out.println("Got it. I've added this task:"); - Task toDoTask = new ToDo(description); - taskList.add(toDoTask); - System.out.println(toDoTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - writeToFile(); - } - - public void runDeadline(String description , String time) { - System.out.println("Got it. I've added this task:"); - Task deadlineTask = new Deadline(description , time); - taskList.add(deadlineTask); - System.out.println(deadlineTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - writeToFile(); - } - - public void runEvent(String description , String time) { - System.out.println("Got it. I've added this task:"); - Task eventTask = new Event(description , time); - taskList.add(eventTask); - System.out.println(eventTask); - System.out.println("Now you have " + taskList.size() + " tasks in the list."); - writeToFile(); - } - - public void showList() { - System.out.println("Here are the tasks in your list:"); - for (int i = 0; i < taskList.size(); i++) { - System.out.println((i + 1) + ". " + taskList.get(i)); - } - } - - public String[] firstFilter(String userInput) { - String[] filter = userInput.split(" "); - String firstWord = filter[0]; - StringBuilder str = new StringBuilder(); - for (int i = 1 ; i < filter.length ; i++) { - str.append(filter[i]); - str.append(WHITESPACE); - } - String[] result = {firstWord , str.toString().trim()}; - - return result; - } - - public String[] secondFilter(String newString , String firstWord) { - String[] filter; - if (firstWord.equals("deadline")) { - filter = newString.split(" /by "); - }else{ - filter = newString.split(" /at "); - } - return filter; - } - - public String timeFormatter(String dateInString , String day) throws ParseException { - DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); - DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha"); - DateFormat ndFormatter = new SimpleDateFormat("d'nd of' MMMM yyyy , ha"); - DateFormat rdFormatter = new SimpleDateFormat("d'rd of' MMMM yyyy , ha"); - DateFormat thFormatter = new SimpleDateFormat("d'th of' MMMM yyyy , ha"); - - String output; - - Date convertedDate = parser.parse(dateInString); - if (day.equals("1") || day.equals("11") || day.equals("21") || day.equals("31") ){ - output = stFormatter.format(convertedDate); - }else if (day.equals("2") || day.equals("12") || day.equals("22")) { - output = ndFormatter.format(convertedDate); - }else if (day.equals("3") || day.equals("13") || day.equals("23")) { - output = rdFormatter.format(convertedDate); - }else{ - output = thFormatter.format(convertedDate); - } - - return output; - } - - - -} From 1747388263e8f61831f9ed7f30c82a7021677fb7 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Tue, 3 Sep 2019 00:41:03 +0800 Subject: [PATCH 22/26] added Parser class --- src/main/java/Duke.java | 106 +++++++----------------------------- src/main/java/Parser.java | 85 +++++++++++++++++++++++++++++ src/main/java/data/duke.txt | 6 +- 3 files changed, 108 insertions(+), 89 deletions(-) create mode 100644 src/main/java/Parser.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 75feb62f..91758df6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,19 +1,23 @@ -import java.io.*; -import java.text.DateFormat; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; -import java.util.Scanner; + public class Duke { + private Parser parser; private Ui ui; public Duke() { ui = new Ui(); + parser = new Parser(); } private static final String WHITESPACE = " "; - Scanner inputs = new Scanner(System.in); ArrayList taskList = new ArrayList<>(100); public void loadFile() { @@ -27,33 +31,7 @@ public void loadFile() { String input = bufferedReader.readLine(); while (input != null) { - String[] arr = input.split(" \\| "); - String taskType = arr[0].trim(); - String taskStatus = arr[1].trim(); - String taskDescription = arr[2].trim(); - String taskTime = ""; - - if (taskType.equals("T")) { - Task todo = new ToDo(taskDescription); - if (taskStatus.equals("\u2713")) { - todo.markAsDone(); - } - taskList.add(todo); - }else if (taskType.equals("D")) { - taskTime = arr[3].trim(); - Task deadline = new Deadline(taskDescription , taskTime); - if (taskStatus.equals("\u2713")) { - deadline.markAsDone(); - } - taskList.add(deadline); - }else { - taskTime = arr[3].trim(); - Task event = new Event(taskDescription , taskTime); - if (taskStatus.equals("\u2713")) { - event.markAsDone(); - } - taskList.add(event); - } + parser.loadParse(taskList , input); input = bufferedReader.readLine(); } @@ -136,10 +114,10 @@ public void run() { String time; String day; - String userInput = inputs.nextLine(); + String userInput = ui.readUserInput(); while(!userInput.equals("bye")) { - String[] firstBox = firstFilter(userInput); // return a string array with commands and description + String[] firstBox = parser.firstFilter(userInput); // return a string array with commands and description String commands = firstBox[0]; try { switch (commands) { @@ -175,21 +153,21 @@ public void run() { case "deadline": newString = firstBox[1]; - secondBox = secondFilter(newString , "deadline"); + secondBox = parser.secondFilter(newString , "deadline"); description = secondBox[0]; time = secondBox[1].trim(); day = time.split("/")[0]; - String timeInString = timeFormatter(time , day); + String timeInString = parser.timeFormatter(time , day); runDeadline(description , timeInString); break; case "event": newString = firstBox[1]; - secondBox = secondFilter(newString , "event"); + secondBox = parser.secondFilter(newString , "event"); description = secondBox[0]; time = secondBox[1]; day = time.split("/")[0]; - timeInString = timeFormatter(time , day); + timeInString = parser.timeFormatter(time , day); runEvent(description , timeInString); break; @@ -201,7 +179,7 @@ public void run() { }catch(DukeException | ParseException e) { e.printStackTrace(); }finally { - userInput = inputs.nextLine().trim(); + userInput = ui.readUserInput().trim(); } } @@ -240,6 +218,7 @@ public void runFind(String userInput) { public void runToDo(String description) { Task toDoTask = new ToDo(description); taskList.add(toDoTask); + ui.showAddTodoTask(taskList , toDoTask); writeToFile(); } @@ -258,53 +237,6 @@ public void runEvent(String description , String time) { writeToFile(); } - public String[] firstFilter(String userInput) { - String[] filter = userInput.split(" "); - String firstWord = filter[0]; - StringBuilder str = new StringBuilder(); - for (int i = 1 ; i < filter.length ; i++) { - str.append(filter[i]); - str.append(WHITESPACE); - } - String[] result = {firstWord , str.toString().trim()}; - - return result; - } - - public String[] secondFilter(String newString , String firstWord) { - String[] filter; - if (firstWord.equals("deadline")) { - filter = newString.split(" /by "); - }else{ - filter = newString.split(" /at "); - } - return filter; - } - - public String timeFormatter(String dateInString , String day) throws ParseException { - DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); - DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha"); - DateFormat ndFormatter = new SimpleDateFormat("d'nd of' MMMM yyyy , ha"); - DateFormat rdFormatter = new SimpleDateFormat("d'rd of' MMMM yyyy , ha"); - DateFormat thFormatter = new SimpleDateFormat("d'th of' MMMM yyyy , ha"); - - String output; - - Date convertedDate = parser.parse(dateInString); - if (day.equals("1")){ - output = stFormatter.format(convertedDate); - }else if (day.equals("2")) { - output = ndFormatter.format(convertedDate); - }else if (day.equals("3")) { - output = rdFormatter.format(convertedDate); - }else{ - output = thFormatter.format(convertedDate); - } - - return output; - } - - public static void main(String[] args) { new Duke().run(); diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 00000000..07d8d903 --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,85 @@ +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; + +public class Parser { + private static final String WHITESPACE = " "; + + + public void loadParse(ArrayList taskList , String input) { + String[] arr = input.split(" \\| "); + String taskType = arr[0].trim(); + String taskStatus = arr[1].trim(); + String taskDescription = arr[2].trim(); + String taskTime = ""; + + if (taskType.equals("T")) { + Task todo = new ToDo(taskDescription); + if (taskStatus.equals("\u2713")) { + todo.markAsDone(); + } + taskList.add(todo); + }else if (taskType.equals("D")) { + taskTime = arr[3].trim(); + Task deadline = new Deadline(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { + deadline.markAsDone(); + } + taskList.add(deadline); + }else { + taskTime = arr[3].trim(); + Task event = new Event(taskDescription , taskTime); + if (taskStatus.equals("\u2713")) { + event.markAsDone(); + } + taskList.add(event); + } + } + + public String timeFormatter(String dateInString , String day) throws ParseException { + DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); + DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha"); + DateFormat ndFormatter = new SimpleDateFormat("d'nd of' MMMM yyyy , ha"); + DateFormat rdFormatter = new SimpleDateFormat("d'rd of' MMMM yyyy , ha"); + DateFormat thFormatter = new SimpleDateFormat("d'th of' MMMM yyyy , ha"); + + String output; + + Date convertedDate = parser.parse(dateInString); + if (day.equals("1")){ + output = stFormatter.format(convertedDate); + }else if (day.equals("2")) { + output = ndFormatter.format(convertedDate); + }else if (day.equals("3")) { + output = rdFormatter.format(convertedDate); + }else{ + output = thFormatter.format(convertedDate); + } + return output; + } + + public String[] firstFilter(String userInput) { + String[] filter = userInput.split(" "); + String firstWord = filter[0]; + StringBuilder str = new StringBuilder(); + for (int i = 1 ; i < filter.length ; i++) { + str.append(filter[i]); + str.append(WHITESPACE); + } + String[] result = {firstWord , str.toString().trim()}; + + return result; + } + + public String[] secondFilter(String newString , String firstWord) { + String[] filter; + if (firstWord.equals("deadline")) { + filter = newString.split(" /by "); + }else{ + filter = newString.split(" /at "); + } + return filter; + } +} diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 94f8fb0d..bad745f4 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,3 +1,5 @@ D | ✓ | read book | 20th of November 1029 , 7PM -E | ✓ | cook food | 20th of October 2011 , 5PM -D | ✘ | jogging | 12nd of December 2012 , 7PM +D | ✓ | jogging | 12nd of December 2012 , 7PM +T | ✘ | hello +D | ✘ | hello | 27th of September 1993 , 7PM +E | ✘ | hello | 22th of November 1993 , 1PM From c557615cb6f2eeb291a8e3a68ef853eb283f3de7 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Tue, 3 Sep 2019 17:02:58 +0800 Subject: [PATCH 23/26] added TaskList class --- src/main/java/Duke.java | 137 ++++-------------------------------- src/main/java/Parser.java | 6 +- src/main/java/TaskList.java | 119 +++++++++++++++++++++++++++++++ src/main/java/data/duke.txt | 6 +- 4 files changed, 142 insertions(+), 126 deletions(-) create mode 100644 src/main/java/TaskList.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 91758df6..e65aa055 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,9 +1,7 @@ import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; @@ -11,13 +9,14 @@ public class Duke { private Parser parser; + private TaskList tasks; private Ui ui; public Duke() { ui = new Ui(); parser = new Parser(); + tasks = new TaskList(taskList); } - private static final String WHITESPACE = " "; ArrayList taskList = new ArrayList<>(100); public void loadFile() { @@ -58,60 +57,9 @@ public void loadFile() { } } - public void writeToFile () { - FileWriter fileWriter = null; - BufferedWriter bufferedWriter = null; - try{ - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - fileWriter = new FileWriter(file , false); - bufferedWriter = new BufferedWriter(fileWriter); - for (Task t : taskList) { - String taskType = t.getType(); - String taskStatus = t.getStatusIcon(); - String taskDescription = t.getDescription(); - String taskTime = t.getTime(); - - if (taskType.equals("T")) { - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); - bufferedWriter.newLine(); - - }else{ - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); - bufferedWriter.newLine(); - } - - - bufferedWriter.flush(); - } - - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (fileWriter != null) { - fileWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedWriter != null) { - bufferedWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - } - - } - public void run() { ui.showWelcome(); loadFile(); - String newString; - String[] secondBox; - String description; - String time; String day; String userInput = ui.readUserInput(); @@ -126,15 +74,15 @@ public void run() { break; case "done": - runDone(userInput); + tasks.runDone(userInput); break; case "delete": - runDelete(userInput); + tasks.runDelete(userInput); break; case "find": - runFind(userInput); + tasks.runFind(userInput); break; case "todo": @@ -142,8 +90,7 @@ public void run() { if (firstBox[1].isBlank()){ throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); }else { - description = firstBox[1]; - runToDo(description); + tasks.runToDo(firstBox[1]); } }catch(DukeException e) { e.printStackTrace(); @@ -152,23 +99,16 @@ public void run() { } case "deadline": - newString = firstBox[1]; - secondBox = parser.secondFilter(newString , "deadline"); - description = secondBox[0]; - time = secondBox[1].trim(); - day = time.split("/")[0]; - String timeInString = parser.timeFormatter(time , day); - runDeadline(description , timeInString); + String[] secondBox = parser.secondFilter(firstBox[1] , "deadline"); + day = parser.dayExtractor(secondBox); + String timeInString = parser.timeFormatter(secondBox[1].trim() , day); + tasks.runDeadline(secondBox[0] , timeInString); break; case "event": - newString = firstBox[1]; - secondBox = parser.secondFilter(newString , "event"); - description = secondBox[0]; - time = secondBox[1]; - day = time.split("/")[0]; - timeInString = parser.timeFormatter(time , day); - runEvent(description , timeInString); + secondBox = parser.secondFilter(firstBox[1] , "event"); + timeInString = parser.timeFormatter(secondBox[1] , parser.dayExtractor(secondBox)); + tasks.runEvent(secondBox[0] , timeInString); break; default: @@ -186,57 +126,6 @@ public void run() { ui.showExit(); } - public void runDone(String userInput) { - int index = Integer.parseInt(userInput.split(" ")[1]); - Task chosenTask = taskList.get(index - 1); - chosenTask.markAsDone(); - ui.showDone(taskList , index); - writeToFile(); - } - - public void runDelete(String userInput) { - int index = Integer.parseInt(userInput.split(" ")[1]); - ui.showDelete(taskList , index); - taskList.remove(index - 1); - ui.showTaskListSize(taskList); - writeToFile(); - } - - public void runFind(String userInput) { - int index = 0 ; - ArrayList searchResults = new ArrayList<>(); - String keyword = userInput.split(" ")[1]; - for (Task task : taskList) { - String description = task.getDescription(); - if (description.contains(keyword)) { - searchResults.add(task); - } - } - ui.showMatchTasks(taskList , index); - } - - public void runToDo(String description) { - Task toDoTask = new ToDo(description); - taskList.add(toDoTask); - ui.showAddTodoTask(taskList , toDoTask); - writeToFile(); - } - - public void runDeadline(String description , String time) { - - Task deadlineTask = new Deadline(description , time); - taskList.add(deadlineTask); - ui.showAddDeadlineTask(taskList , deadlineTask); - writeToFile(); - } - - public void runEvent(String description , String time) { - Task eventTask = new Event(description , time); - taskList.add(eventTask); - ui.showAddEventTask(taskList , eventTask); - writeToFile(); - } - public static void main(String[] args) { new Duke().run(); diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 07d8d903..26124ace 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -7,7 +7,6 @@ public class Parser { private static final String WHITESPACE = " "; - public void loadParse(ArrayList taskList , String input) { String[] arr = input.split(" \\| "); String taskType = arr[0].trim(); @@ -38,6 +37,11 @@ public void loadParse(ArrayList taskList , String input) { } } + public String dayExtractor(String[] secondBox) { + String day = secondBox[1].trim().split("/")[0]; + return day; + } + public String timeFormatter(String dateInString , String day) throws ParseException { DateFormat parser = new SimpleDateFormat("dd/M/yyyy HHmm"); DateFormat stFormatter = new SimpleDateFormat("d'st of' MMMM yyyy , ha"); diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 00000000..ada970a1 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,119 @@ +import java.io.*; +import java.util.ArrayList; + +public class TaskList { + public TaskList() {} + public TaskList(ArrayList taskList) { + this.taskList = taskList; + } + + private ArrayList taskList = new ArrayList<>(); + Ui ui = new Ui(); + + public void writeToFile () { + FileWriter fileWriter = null; + BufferedWriter bufferedWriter = null; + try{ + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + fileWriter = new FileWriter(file , false); + bufferedWriter = new BufferedWriter(fileWriter); + for (Task t : taskList) { + String taskType = t.getType(); + String taskStatus = t.getStatusIcon(); + String taskDescription = t.getDescription(); + String taskTime = t.getTime(); + + if (taskType.equals("T")) { + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); + bufferedWriter.newLine(); + + }else{ + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); + bufferedWriter.newLine(); + } + + + bufferedWriter.flush(); + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public void runDone(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + Task chosenTask = taskList.get(index - 1); + chosenTask.markAsDone(); + ui.showDone(taskList , index); + writeToFile(); + } + + public void runDelete(String userInput) { + int index = Integer.parseInt(userInput.split(" ")[1]); + ui.showDelete(taskList , index); + taskList.remove(index - 1); + ui.showTaskListSize(taskList); + writeToFile(); + } + + public void runFind(String userInput) { + int index = 0 ; + ArrayList searchResults = new ArrayList<>(); + String keyword = userInput.split(" ")[1]; + for (Task task : taskList) { + String description = task.getDescription(); + if (description.contains(keyword)) { + searchResults.add(task); + } + } + ui.showMatchTasks(searchResults , index); + } + + public void runToDo(String description) { + Task toDoTask = new ToDo(description); + taskList.add(toDoTask); + ui.showAddTodoTask(taskList , toDoTask); + writeToFile(); + } + + public void runDeadline(String description , String time) { + + Task deadlineTask = new Deadline(description , time); + taskList.add(deadlineTask); + ui.showAddDeadlineTask(taskList , deadlineTask); + writeToFile(); + } + + public void runEvent(String description , String time) { + Task eventTask = new Event(description , time); + taskList.add(eventTask); + ui.showAddEventTask(taskList , eventTask); + writeToFile(); + } + + + + + + + + + +} diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index bad745f4..72e91bfe 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,5 +1,9 @@ D | ✓ | read book | 20th of November 1029 , 7PM D | ✓ | jogging | 12nd of December 2012 , 7PM -T | ✘ | hello D | ✘ | hello | 27th of September 1993 , 7PM E | ✘ | hello | 22th of November 1993 , 1PM +D | ✘ | hello | 27th of October 2019 , 7PM +E | ✘ | kiki | 19th of November 2019 , 2PM +T | ✓ | hello +D | ✘ | kokokoko | 27th of October 1999 , 10AM +E | ✘ | kikikiki | 29th of October 2019 , 7PM From f9cec821ffc707df129e74678dc83947d4fe25ef Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Tue, 3 Sep 2019 18:47:08 +0800 Subject: [PATCH 24/26] added Storage class + A-MoreOOP (Minimal) [Completed] --- src/main/java/Duke.java | 58 ++++------------------- src/main/java/Storage.java | 94 +++++++++++++++++++++++++++++++++++++ src/main/java/TaskList.java | 59 ++++------------------- src/main/java/Ui.java | 16 +++++++ src/main/java/data/duke.txt | 11 ++--- 5 files changed, 130 insertions(+), 108 deletions(-) create mode 100644 src/main/java/Storage.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e65aa055..b86851ef 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,65 +1,20 @@ -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; import java.text.ParseException; -import java.util.ArrayList; - public class Duke { + private Storage storage; private Parser parser; private TaskList tasks; private Ui ui; public Duke() { ui = new Ui(); + storage = new Storage(); parser = new Parser(); - tasks = new TaskList(taskList); - } - ArrayList taskList = new ArrayList<>(100); - - public void loadFile() { - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - FileReader fileReader = null; - BufferedReader bufferedReader = null; - try { - fileReader = new FileReader(file); - bufferedReader = new BufferedReader(fileReader); - - String input = bufferedReader.readLine(); - - while (input != null) { - parser.loadParse(taskList , input); - input = bufferedReader.readLine(); - } - - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - }finally { - try { - if (fileReader != null) { - fileReader.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedReader != null) { - bufferedReader.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - - } + tasks = new TaskList(storage.load()); } public void run() { ui.showWelcome(); - loadFile(); String day; String userInput = ui.readUserInput(); @@ -70,15 +25,17 @@ public void run() { try { switch (commands) { case "list": - ui.showTaskList(taskList); + ui.showTaskList(tasks.getTaskList()); break; case "done": tasks.runDone(userInput); + storage.save(); break; case "delete": tasks.runDelete(userInput); + storage.save(); break; case "find": @@ -95,6 +52,7 @@ public void run() { }catch(DukeException e) { e.printStackTrace(); }finally{ + storage.save(); break; } @@ -103,12 +61,14 @@ public void run() { day = parser.dayExtractor(secondBox); String timeInString = parser.timeFormatter(secondBox[1].trim() , day); tasks.runDeadline(secondBox[0] , timeInString); + storage.save(); break; case "event": secondBox = parser.secondFilter(firstBox[1] , "event"); timeInString = parser.timeFormatter(secondBox[1] , parser.dayExtractor(secondBox)); tasks.runEvent(secondBox[0] , timeInString); + storage.save(); break; default: diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 00000000..ddbcabf3 --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,94 @@ +import java.io.*; +import java.util.ArrayList; + +public class Storage { + + private ArrayList taskList = new ArrayList<>(); + Parser parser = new Parser(); + + public ArrayList load() { + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + FileReader fileReader = null; + BufferedReader bufferedReader = null; + try { + fileReader = new FileReader(file); + bufferedReader = new BufferedReader(fileReader); + + String input = bufferedReader.readLine(); + + while (input != null) { + parser.loadParse(taskList , input); + input = bufferedReader.readLine(); + } + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }finally { + try { + if (fileReader != null) { + fileReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + return taskList; + + } + } + + public void save () { + FileWriter fileWriter = null; + BufferedWriter bufferedWriter = null; + try{ + File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); + fileWriter = new FileWriter(file , false); + bufferedWriter = new BufferedWriter(fileWriter); + for (Task t : taskList) { + String taskType = t.getType(); + String taskStatus = t.getStatusIcon(); + String taskDescription = t.getDescription(); + String taskTime = t.getTime(); + + if (taskType.equals("T")) { + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); + bufferedWriter.newLine(); + + }else{ + bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); + bufferedWriter.newLine(); + } + + + bufferedWriter.flush(); + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + try { + if (bufferedWriter != null) { + bufferedWriter.close(); + } + }catch (IOException e) { + e.printStackTrace(); + } + } + + } +} diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index ada970a1..d5da02da 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,4 +1,3 @@ -import java.io.*; import java.util.ArrayList; public class TaskList { @@ -9,52 +8,10 @@ public TaskList(ArrayList taskList) { private ArrayList taskList = new ArrayList<>(); Ui ui = new Ui(); + //Storage storage = new Storage(); - public void writeToFile () { - FileWriter fileWriter = null; - BufferedWriter bufferedWriter = null; - try{ - File file = new File("/Users/qianjie/Desktop/duke/src/main/java/data/duke.txt"); - fileWriter = new FileWriter(file , false); - bufferedWriter = new BufferedWriter(fileWriter); - for (Task t : taskList) { - String taskType = t.getType(); - String taskStatus = t.getStatusIcon(); - String taskDescription = t.getDescription(); - String taskTime = t.getTime(); - - if (taskType.equals("T")) { - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription); - bufferedWriter.newLine(); - - }else{ - bufferedWriter.write(taskType + " | " + taskStatus + " | " + taskDescription + " | " + taskTime); - bufferedWriter.newLine(); - } - - - bufferedWriter.flush(); - } - - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (fileWriter != null) { - fileWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - try { - if (bufferedWriter != null) { - bufferedWriter.close(); - } - }catch (IOException e) { - e.printStackTrace(); - } - } - + public ArrayList getTaskList() { + return taskList; } public void runDone(String userInput) { @@ -62,7 +19,7 @@ public void runDone(String userInput) { Task chosenTask = taskList.get(index - 1); chosenTask.markAsDone(); ui.showDone(taskList , index); - writeToFile(); + //storage.save(); } public void runDelete(String userInput) { @@ -70,7 +27,7 @@ public void runDelete(String userInput) { ui.showDelete(taskList , index); taskList.remove(index - 1); ui.showTaskListSize(taskList); - writeToFile(); + //storage.save(); } public void runFind(String userInput) { @@ -90,7 +47,7 @@ public void runToDo(String description) { Task toDoTask = new ToDo(description); taskList.add(toDoTask); ui.showAddTodoTask(taskList , toDoTask); - writeToFile(); + //storage.save(); } public void runDeadline(String description , String time) { @@ -98,14 +55,14 @@ public void runDeadline(String description , String time) { Task deadlineTask = new Deadline(description , time); taskList.add(deadlineTask); ui.showAddDeadlineTask(taskList , deadlineTask); - writeToFile(); + //storage.save(); } public void runEvent(String description , String time) { Task eventTask = new Event(description , time); taskList.add(eventTask); ui.showAddEventTask(taskList , eventTask); - writeToFile(); + //storage.save(); } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index e445f6c2..282d1e4f 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -18,15 +18,19 @@ public void showWelcome() { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; + showLine(); System.out.println("Hello from\n" + logo); System.out.println("Hello! I'm Duke\nWhat can I do for you?"); + showLine(); } public void showTaskList(ArrayList taskList) { + showLine(); System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskList.size(); i++) { System.out.println((i + 1) + ". " + taskList.get(i)); } + showLine(); } public void showExit() { @@ -34,43 +38,55 @@ public void showExit() { } public void showDone(ArrayList taskList , int index) { + showLine(); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskList.get(index - 1)); + showLine(); } public void showDelete(ArrayList taskList , int index) { + showLine(); System.out.println("Noted. I've removed this task:"); System.out.println(taskList.get(index - 1)); } public void showTaskListSize(ArrayList taskList) { System.out.println("Now you have " + taskList.size() + " tasks in the list."); + showLine(); } public void showMatchTasks(ArrayList searchResults , int index) { + showLine(); System.out.println("Here are the matching tasks in your list:"); for (Task results : searchResults) { index++; System.out.println(index + "." + results); } + showLine(); } public void showAddTodoTask(ArrayList taskList , Task todoTask) { + showLine(); System.out.println("Got it. I've added this task:"); System.out.println(todoTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); + showLine(); } public void showAddDeadlineTask(ArrayList taskList , Task deadlineTask) { + showLine(); System.out.println("Got it. I've added this task:"); System.out.println(deadlineTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); + showLine(); } public void showAddEventTask(ArrayList taskList , Task eventTask) { + showLine(); System.out.println("Got it. I've added this task:"); System.out.println(eventTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); + showLine(); } public void showError (String message) { diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 72e91bfe..28130ab4 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,9 +1,4 @@ -D | ✓ | read book | 20th of November 1029 , 7PM -D | ✓ | jogging | 12nd of December 2012 , 7PM -D | ✘ | hello | 27th of September 1993 , 7PM -E | ✘ | hello | 22th of November 1993 , 1PM -D | ✘ | hello | 27th of October 2019 , 7PM -E | ✘ | kiki | 19th of November 2019 , 2PM T | ✓ | hello -D | ✘ | kokokoko | 27th of October 1999 , 10AM -E | ✘ | kikikiki | 29th of October 2019 , 7PM +D | ✓ | borrow book | 27th of October 2019 , 6PM +D | ✘ | hello | 27th of July 1112 , 7PM +E | ✘ | lklklk | 19th of November 2011 , 10AM From fb3e8fbd6696dddfd4c4eb065c88e20c6c0563d7 Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Wed, 4 Sep 2019 16:17:37 +0800 Subject: [PATCH 25/26] A-TextUiTesting: Text UI Testing [Completed] --- src/main/java/Ui.java | 34 ++++++++++++------------- src/main/java/data/duke.txt | 12 ++++++--- src/main/java/text-ui-test/ACTUAL.TXT | 6 +++++ src/main/java/text-ui-test/EXPECTED.txt | 6 +++++ src/main/java/text-ui-test/input.txt | 2 ++ src/main/java/text-ui-test/runtest.sh | 34 +++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 src/main/java/text-ui-test/ACTUAL.TXT create mode 100644 src/main/java/text-ui-test/EXPECTED.txt create mode 100644 src/main/java/text-ui-test/input.txt create mode 100755 src/main/java/text-ui-test/runtest.sh diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index 282d1e4f..fc231e45 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -18,19 +18,19 @@ public void showWelcome() { + "| | | | | | | |/ / _ \\\n" + "| |_| | |_| | < __/\n" + "|____/ \\__,_|_|\\_\\___|\n"; - showLine(); - System.out.println("Hello from\n" + logo); + //showLine(); + //System.out.println("Hello from\n" + logo); System.out.println("Hello! I'm Duke\nWhat can I do for you?"); - showLine(); + //showLine(); } public void showTaskList(ArrayList taskList) { - showLine(); + //showLine(); System.out.println("Here are the tasks in your list:"); for (int i = 0; i < taskList.size(); i++) { System.out.println((i + 1) + ". " + taskList.get(i)); } - showLine(); + //showLine(); } public void showExit() { @@ -38,55 +38,55 @@ public void showExit() { } public void showDone(ArrayList taskList , int index) { - showLine(); + //showLine(); System.out.println("Nice! I've marked this task as done:"); System.out.println(taskList.get(index - 1)); - showLine(); + //showLine(); } public void showDelete(ArrayList taskList , int index) { - showLine(); + //showLine(); System.out.println("Noted. I've removed this task:"); System.out.println(taskList.get(index - 1)); } public void showTaskListSize(ArrayList taskList) { System.out.println("Now you have " + taskList.size() + " tasks in the list."); - showLine(); + //showLine(); } public void showMatchTasks(ArrayList searchResults , int index) { - showLine(); + //showLine(); System.out.println("Here are the matching tasks in your list:"); for (Task results : searchResults) { index++; System.out.println(index + "." + results); } - showLine(); + //showLine(); } public void showAddTodoTask(ArrayList taskList , Task todoTask) { - showLine(); + //showLine(); System.out.println("Got it. I've added this task:"); System.out.println(todoTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); - showLine(); + //showLine(); } public void showAddDeadlineTask(ArrayList taskList , Task deadlineTask) { - showLine(); + //showLine(); System.out.println("Got it. I've added this task:"); System.out.println(deadlineTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); - showLine(); + //showLine(); } public void showAddEventTask(ArrayList taskList , Task eventTask) { - showLine(); + //showLine(); System.out.println("Got it. I've added this task:"); System.out.println(eventTask); System.out.println("Now you have " + taskList.size() + " tasks in the list."); - showLine(); + //showLine(); } public void showError (String message) { diff --git a/src/main/java/data/duke.txt b/src/main/java/data/duke.txt index 28130ab4..c864b09e 100644 --- a/src/main/java/data/duke.txt +++ b/src/main/java/data/duke.txt @@ -1,4 +1,8 @@ -T | ✓ | hello -D | ✓ | borrow book | 27th of October 2019 , 6PM -D | ✘ | hello | 27th of July 1112 , 7PM -E | ✘ | lklklk | 19th of November 2011 , 10AM +D | ✓ | return book | 20th of October 2017 , 6PM +E | ✘ | read book | 11th of September 2011 , 6PM +D | ✘ | return book | 10th of October 2019 , 7PM +D | ✘ | return book | 10th of October 2019 , 7PM +D | ✘ | return book | 10th of October 2019 , 7PM +D | ✘ | return book | 10th of October 2019 , 7PM +D | ✘ | return book | 10th of October 2019 , 7PM +D | ✘ | return book | 10th of October 2019 , 7PM diff --git a/src/main/java/text-ui-test/ACTUAL.TXT b/src/main/java/text-ui-test/ACTUAL.TXT new file mode 100644 index 00000000..e7e457ca --- /dev/null +++ b/src/main/java/text-ui-test/ACTUAL.TXT @@ -0,0 +1,6 @@ +Hello! I'm Duke +What can I do for you? +Got it. I've added this task: +[D][✘] return book (by: 10th of October 2019 , 7PM) +Now you have 8 tasks in the list. +Bye. Hope to see you again soon! diff --git a/src/main/java/text-ui-test/EXPECTED.txt b/src/main/java/text-ui-test/EXPECTED.txt new file mode 100644 index 00000000..e7e457ca --- /dev/null +++ b/src/main/java/text-ui-test/EXPECTED.txt @@ -0,0 +1,6 @@ +Hello! I'm Duke +What can I do for you? +Got it. I've added this task: +[D][✘] return book (by: 10th of October 2019 , 7PM) +Now you have 8 tasks in the list. +Bye. Hope to see you again soon! diff --git a/src/main/java/text-ui-test/input.txt b/src/main/java/text-ui-test/input.txt new file mode 100644 index 00000000..75d0b131 --- /dev/null +++ b/src/main/java/text-ui-test/input.txt @@ -0,0 +1,2 @@ +deadline return book /by 10/10/2019 1900 +bye \ No newline at end of file diff --git a/src/main/java/text-ui-test/runtest.sh b/src/main/java/text-ui-test/runtest.sh new file mode 100755 index 00000000..e562d837 --- /dev/null +++ b/src/main/java/text-ui-test/runtest.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +# create bin directory if it doesn't exist +if [ ! -d "../bin" ] +then + mkdir ../bin +fi + +# delete output from previous run +if [ -e "./ACTUAL.TXT" ] +then + rm ACTUAL.TXT +fi + +# compile the code into the bin folder, terminates if error occurred +if ! javac -cp ~/Desktop/duke/src -Xlint:none -d ~/Desktop/duke/bin ~/Desktop/duke/src/main/java/*.java +then + echo "********** BUILD FAILURE **********" + exit 1 +fi + +# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT +java -classpath ~/Desktop/duke/bin Duke < input.txt > ACTUAL.TXT + +# compare the output to the expected output +diff ACTUAL.TXT EXPECTED.TXT +if [ $? -eq 0 ] +then + echo "Test result: PASSED" + exit 0 +else + echo "Test result: FAILED" + exit 1 +fi \ No newline at end of file From ebb826490b251178382d508cc1a65f17fda2c4fc Mon Sep 17 00:00:00 2001 From: Qian Jie Date: Sat, 7 Sep 2019 10:11:21 +0800 Subject: [PATCH 26/26] A-JUnit : JUnit Testing [Completed] --- src/main/java/Parser.java | 5 +++++ src/main/java/Storage.java | 2 ++ src/main/java/TaskList.java | 5 +++++ src/main/java/Ui.java | 2 ++ src/main/java/{ => duke}/Deadline.java | 2 ++ src/main/java/{ => duke}/Event.java | 2 ++ src/main/java/{ => duke}/Task.java | 5 ++++- src/main/java/{ => duke}/ToDo.java | 2 ++ src/test/java/DukeTest.java | 10 ++++++++++ src/test/java/duke/DeadlineTest.java | 25 +++++++++++++++++++++++++ src/test/java/duke/EventTest.java | 25 +++++++++++++++++++++++++ src/test/java/duke/TodoTest.java | 26 ++++++++++++++++++++++++++ 12 files changed, 110 insertions(+), 1 deletion(-) rename src/main/java/{ => duke}/Deadline.java (95%) rename src/main/java/{ => duke}/Event.java (95%) rename src/main/java/{ => duke}/Task.java (94%) rename src/main/java/{ => duke}/ToDo.java (94%) create mode 100644 src/test/java/DukeTest.java create mode 100644 src/test/java/duke/DeadlineTest.java create mode 100644 src/test/java/duke/EventTest.java create mode 100644 src/test/java/duke/TodoTest.java diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 26124ace..969a8d10 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -1,3 +1,8 @@ +import duke.Deadline; +import duke.Event; +import duke.Task; +import duke.ToDo; + import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index ddbcabf3..2cd235e9 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -1,3 +1,5 @@ +import duke.Task; + import java.io.*; import java.util.ArrayList; diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java index d5da02da..22ac6374 100644 --- a/src/main/java/TaskList.java +++ b/src/main/java/TaskList.java @@ -1,3 +1,8 @@ +import duke.Deadline; +import duke.Event; +import duke.Task; +import duke.ToDo; + import java.util.ArrayList; public class TaskList { diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java index fc231e45..883a6555 100644 --- a/src/main/java/Ui.java +++ b/src/main/java/Ui.java @@ -1,3 +1,5 @@ +import duke.Task; + import java.util.ArrayList; import java.util.Scanner; diff --git a/src/main/java/Deadline.java b/src/main/java/duke/Deadline.java similarity index 95% rename from src/main/java/Deadline.java rename to src/main/java/duke/Deadline.java index 606d7f9b..21e93443 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/duke/Deadline.java @@ -1,3 +1,5 @@ +package duke; + public class Deadline extends Task { public Deadline(String description , String time) { diff --git a/src/main/java/Event.java b/src/main/java/duke/Event.java similarity index 95% rename from src/main/java/Event.java rename to src/main/java/duke/Event.java index 2b70e37a..3b12c2bb 100644 --- a/src/main/java/Event.java +++ b/src/main/java/duke/Event.java @@ -1,3 +1,5 @@ +package duke; + public class Event extends Task { public Event(String description , String time) { diff --git a/src/main/java/Task.java b/src/main/java/duke/Task.java similarity index 94% rename from src/main/java/Task.java rename to src/main/java/duke/Task.java index 13186ada..4ae4c441 100644 --- a/src/main/java/Task.java +++ b/src/main/java/duke/Task.java @@ -1,4 +1,6 @@ -public class Task{ +package duke; + +public abstract class Task{ protected String description; protected boolean isDone; @@ -35,5 +37,6 @@ public void markAsDone() { public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } + } diff --git a/src/main/java/ToDo.java b/src/main/java/duke/ToDo.java similarity index 94% rename from src/main/java/ToDo.java rename to src/main/java/duke/ToDo.java index e401f61d..bb1fe45a 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/duke/ToDo.java @@ -1,3 +1,5 @@ +package duke; + public class ToDo extends Task { public ToDo(String description) { super(description); diff --git a/src/test/java/DukeTest.java b/src/test/java/DukeTest.java new file mode 100644 index 00000000..b1573688 --- /dev/null +++ b/src/test/java/DukeTest.java @@ -0,0 +1,10 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DukeTest { + @Test + public void dummyTest(){ + assertEquals(2, 2); + } +} diff --git a/src/test/java/duke/DeadlineTest.java b/src/test/java/duke/DeadlineTest.java new file mode 100644 index 00000000..437fcfba --- /dev/null +++ b/src/test/java/duke/DeadlineTest.java @@ -0,0 +1,25 @@ +package duke; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DeadlineTest { + @Test + void testToString() { + Deadline newDeadline = new Deadline("I need to test the code", "20/11/2019 1800"); + assertEquals("[D]" + "[\u2718]" + " " + "I need to test the code (by: tonight)", newDeadline.toString()); + } + + @Test + void getStatusIcon() { + Deadline newDeadline = new Deadline("I need to test the code", "20/11/2019 1800"); + assertEquals("\u2718", newDeadline.getStatusIcon()); + } + + @Test + void markAsDone() { + Deadline newDeadline = new Deadline("I need to test the code", "20/11/2019 1800"); + newDeadline.markAsDone(); + assertEquals("\u2713", newDeadline.getStatusIcon()); + } +} diff --git a/src/test/java/duke/EventTest.java b/src/test/java/duke/EventTest.java new file mode 100644 index 00000000..75900472 --- /dev/null +++ b/src/test/java/duke/EventTest.java @@ -0,0 +1,25 @@ +package duke; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EventTest { + @Test + void testToString() { + Event newEvent = new Event("project meeting", "20/11/2019 1800"); + assertEquals("[E]" + "[\u2718]" + " " + "project meeting (at: 7th May)", newEvent.toString()); + } + + @Test + void getStatusIcon() { + Event newEvent = new Event("project meeting", "20/11/2019 1800"); + assertEquals("\u2718", newEvent.getStatusIcon()); + } + + @Test + void markAsDone() { + Event newEvent = new Event("project meeting", "20/11/2019 1800"); + newEvent.markAsDone(); + assertEquals("\u2713", newEvent.getStatusIcon()); + } +} diff --git a/src/test/java/duke/TodoTest.java b/src/test/java/duke/TodoTest.java new file mode 100644 index 00000000..a5a16a2d --- /dev/null +++ b/src/test/java/duke/TodoTest.java @@ -0,0 +1,26 @@ +package duke; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TodoTest { + + @Test + void testToString() { + ToDo newTodo = new ToDo("I need to test the code"); + assertEquals("[T]" + "[\u2718]" + " " + "I need to test the code" , newTodo.toString()); + } + + @Test + void getStatusIcon() { + ToDo newToDo = new ToDo("I need to test the code"); + assertEquals("\u2718", newToDo.getStatusIcon()); + } + + @Test + void markAsDone() { + ToDo newToDo = new ToDo("I need to test the code"); + newToDo.markAsDone(); + assertEquals("\u2713", newToDo.getStatusIcon()); + } +}