From 388dd7708f12dcbaf15009fcb442ead5b5dc97e0 Mon Sep 17 00:00:00 2001 From: gachia Date: Sun, 25 Aug 2019 13:57:15 +0800 Subject: [PATCH] Level 7 Level 7 Save completed --- src/main/data/duke.txt | 7 +++++ src/main/java/Deadline.java | 10 +++++++ src/main/java/Duke.java | 56 +++++++++++++++++++++++++++++++++++-- src/main/java/Event.java | 10 +++++++ src/main/java/Task.java | 9 ++++++ 5 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/main/data/duke.txt diff --git a/src/main/data/duke.txt b/src/main/data/duke.txt new file mode 100644 index 0000000000..e7d7161c1d --- /dev/null +++ b/src/main/data/duke.txt @@ -0,0 +1,7 @@ +T true read book +D false return book/June 6th +D false project meeting/Aug 6th 2-4pm +T true join sports club +T true dance +D false get food/tmr +E true dance party/my place diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 34b3433ef1..9c33643284 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -6,6 +6,16 @@ public Deadline(String desc, String by) { this.by = by; } + public Deadline(String desc, String by, boolean isDone){ + super(desc, isDone); + this.by = by; + } + + @Override + public String writeFormat(){ + return "D " + isDone + " " + description + "/" + by; + } + @Override public String toString(){ return "[D]" + super.getTask() + " (by: " + by + ")"; diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 493304e757..2a09c89bfa 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,8 +1,50 @@ +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; +import java.io.File; +import java.io.FileWriter; public class Duke { + private static void loadData(String filePath, ArrayList list) throws FileNotFoundException { + File f = new File(filePath); + //System.out.println(f.getAbsolutePath()); + Scanner sc = new Scanner(f); + while (sc.hasNext()){ + String type = sc.next(); + switch(type){ + case "T": + boolean done = sc.nextBoolean(); + String taskName = sc.nextLine().trim(); + list.add(new Task(taskName, done)); + break; + case "D": + done = sc.nextBoolean(); + taskName = sc.nextLine().trim(); + String[] userWords = taskName.split("/"); + list.add(new Deadline(userWords[0], userWords[1], done)); + break; + case "E": + done = sc.nextBoolean(); + taskName = sc.nextLine().trim(); + userWords = taskName.split("/"); + list.add(new Event(userWords[0], userWords[1], done)); + break; + } + } + } + + private static void saveData(String filePath, ArrayList list) throws IOException { + FileWriter fw = new FileWriter(filePath); + String data = ""; + for (Task t : list){ + data += t.writeFormat() + "\n"; + } + fw.write(data); + fw.close(); + } + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -13,8 +55,14 @@ public static void main(String[] args) { final String lineSpace = "_______________________________\n"; String startMessage = lineSpace + "Hello! I'm Duke\nWhat can I do for you?\n" + lineSpace; System.out.println(startMessage); - Scanner sc = new Scanner(System.in); ArrayList list = new ArrayList(); + final String filePath = "src/main/data/duke.txt"; + try{ + loadData(filePath, list); + }catch(FileNotFoundException e){ + System.out.println("File not found"); + } + Scanner sc = new Scanner(System.in); String taskName; Task task; while (sc.hasNext()) { @@ -42,6 +90,7 @@ public static void main(String[] args) { list.add(task); System.out.println(lineSpace + "Got it. I've added this task:\n" + task + "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace); + saveData(filePath, list); break; case "deadline": taskName = sc.nextLine().trim(); @@ -56,7 +105,7 @@ public static void main(String[] args) { list.add(task); System.out.println(lineSpace + "Got it. I've added this task:\n" + task + "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace); - + saveData(filePath, list); break; case "event": taskName = sc.nextLine().trim(); @@ -71,6 +120,7 @@ public static void main(String[] args) { list.add(task); System.out.println(lineSpace + "Got it. I've added this task:\n" + task + "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace); + saveData(filePath, list); break; case "delete": int deletion = sc.nextInt(); @@ -81,6 +131,7 @@ public static void main(String[] args) { list.remove(deletion-1); System.out.println(lineSpace + "Noted. I've removed this task:\n" + temp + "\nNow you have " + list.size() + " tasks in the list.\n" + lineSpace); + saveData(filePath, list); break; case "done": int taskNo = sc.nextInt(); @@ -90,6 +141,7 @@ public static void main(String[] args) { list.get(taskNo - 1).markAsDone(); System.out.println(lineSpace + "Nice! I've marked this task as done:\n" + list.get(taskNo - 1) + "\n" + lineSpace); + saveData(filePath, list); break; default: throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 35f38bc5cc..9081f7f6a0 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -7,6 +7,16 @@ public Event(String desc, String at) { this.at = at; } + public Event(String desc, String at, boolean isDone) { + super(desc, isDone); + this.at = at; + } + + @Override + public String writeFormat(){ + return "E " + isDone + " " + description + "/" + at; + } + @Override public String toString(){ return "[E]" + super.getTask() + " (at: " + at + ")"; diff --git a/src/main/java/Task.java b/src/main/java/Task.java index b96e7d5caa..9ce91a68b6 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -7,6 +7,11 @@ public Task(String desc) { this.isDone = false; } + public Task(String desc, boolean isDone){ + this.description = desc; + this.isDone = isDone; + } + public String getStatusIcon() { return (isDone ? "\u2713" : "\u2718"); //return tick or X symbols } @@ -20,6 +25,10 @@ public String getTask(){ return output; } + public String writeFormat(){ + return "T " + isDone + " " + description; + } + @Override public String toString(){ return "[T]" + getTask();