From 22137c1efe017cb479e527d5436cf5cbd7c04877 Mon Sep 17 00:00:00 2001 From: Chang Chuan Hao Date: Thu, 19 Aug 2021 16:47:36 +0800 Subject: [PATCH] Completed Level 7. Save --- data/save.txt | 2 + src/main/java/Deadline.java | 11 +++++- src/main/java/Duke.java | 76 +++++++++++++++++++++++++++++-------- src/main/java/Event.java | 10 +++++ src/main/java/Task.java | 13 +++++++ src/main/java/ToDo.java | 9 +++++ 6 files changed, 105 insertions(+), 16 deletions(-) create mode 100644 data/save.txt diff --git a/data/save.txt b/data/save.txt new file mode 100644 index 0000000000..5dbc8541d6 --- /dev/null +++ b/data/save.txt @@ -0,0 +1,2 @@ +E-0-asd 123-something 567 +D-0-##24325-qwe 123 \ No newline at end of file diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index 2de70b7540..aee39f7189 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,5 +1,4 @@ public class Deadline extends Task { - protected String by; public Deadline(String description, String by) { @@ -7,8 +6,18 @@ public Deadline(String description, String by) { this.by = by; } + public Deadline(boolean done, String description, String by) { + super(done, description); + this.by = by; + } + @Override public String toString() { return "[D]" + super.toString() + " (by: " + by + ")"; } + + @Override + public String saveString() { + return "D-" + super.saveString() + "-" + by; + } } \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index a3ce6e71ab..e3d4720e7c 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,18 +1,17 @@ +import java.io.*; import java.util.Scanner; import java.util.ArrayList; public class Duke { + private static String PATH; + private static boolean run; private static ArrayList tasks; - private static int index; - - private enum Commands { - BYE (), - } public static void main(String[] args) { + PATH = System.getProperty("user.dir") + "\\data\\save.txt"; String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" @@ -27,7 +26,7 @@ public static void main(String[] args) { System.out.println(greeting); run = true; - tasks = new ArrayList(); + fromFile(PATH); Scanner inputReader = new Scanner(System.in); @@ -46,13 +45,14 @@ public static boolean eval(String input) { switch (inputArray[0]) { case "bye": + saveFile(PATH); System.out.println("____________________________________________________________\n" + "Bye. Don't come again!\n" + "____________________________________________________________\n"); return false; case "list": System.out.println("____________________________________________________________\n"); - for(int i = 0; i < index; i++) { + for(int i = 0; i < tasks.size(); i++) { System.out.printf("%d. %s\n", i + 1, tasks.get(i).toString()); } System.out.println("____________________________________________________________\n"); @@ -70,9 +70,8 @@ public static boolean eval(String input) { selectedTask = Integer.parseInt(inputArray[1]) - 1; System.out.println("Noted. I've removed this task:\n" + tasks.get(selectedTask).toString() + "\n" + - "Now you have " + (index - 1) + " tasks in the list.\n"); + "Now you have " + tasks.size() + " tasks in the list.\n"); tasks.remove(selectedTask); - index--; return true; case "event": params = input.split("/at"); @@ -82,9 +81,8 @@ public static boolean eval(String input) { System.out.println("____________________________________________________________\n" + "Got it. I've added this task:\n" - + tasks.get(index).toString() + "\n" + + tasks.get(tasks.size() - 1).toString() + "\n" + "____________________________________________________________\n"); - index++; return true; case "deadline": params = input.split("/by"); @@ -94,9 +92,8 @@ public static boolean eval(String input) { System.out.println("____________________________________________________________\n" + "Got it. I've added this task:\n" - + tasks.get(index).toString() + "\n" + + tasks.get(tasks.size() - 1).toString() + "\n" + "____________________________________________________________\n"); - index++; return true; case "todo": try { @@ -107,9 +104,8 @@ public static boolean eval(String input) { tasks.add(new ToDo(name)); System.out.println("____________________________________________________________\n" + "Got it. I've added this task:\n" - + tasks.get(index).toString() + "\n" + + tasks.get(tasks.size() - 1).toString() + "\n" + "____________________________________________________________\n"); - index++; } catch (StringIndexOutOfBoundsException e) { System.out.println("____________________________________________________________\n" + @@ -125,4 +121,54 @@ public static boolean eval(String input) { return true; } } + + public static void fromFile(String path) { + try { + BufferedReader bufferedReader = new BufferedReader(new FileReader(path)); + String line; + String[] arr; + boolean done; + tasks = new ArrayList<>(); + + while((line = bufferedReader.readLine()) != null) { + arr = line.split("-"); + done = arr[1].equals("1"); + + switch (arr[0]) { + case "T": + tasks.add(new ToDo(done, arr[2])); + break; + case "E": + tasks.add(new Event(done, arr[2], arr[3])); + break; + case "D": + tasks.add(new Deadline(done, arr[2], arr[3])); + break; + default: + throw new IllegalArgumentException("Unrecognized task flag"); + } + } + } catch(IOException e) { + System.out.println(e.toString()); + } + } + + public static void saveFile(String path) { + StringBuilder stringBuilder = new StringBuilder(); + for (Task task : tasks) { + if (stringBuilder.length() != 0) { + stringBuilder.append("\n"); + } + stringBuilder.append(task.saveString()); + } + try { + FileWriter fileWriter = new FileWriter(path); + String output = stringBuilder.toString(); + fileWriter.write(output); + fileWriter.close(); + } catch (IOException e) { + System.out.println(e); + } + } + } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 953aa48199..efc8d1f70f 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -6,8 +6,18 @@ public Event(String description, String at) { this.at = at; } + public Event(boolean done, String description, String at) { + super(done, description); + this.at = at; + } + @Override public String toString() { return "[E]" + super.toString() + " (at: " + at + ")"; } + + @Override + public String saveString() { + return "E-" + super.saveString() + "-" + at; + } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index a65c590aba..0d59661e56 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -6,6 +6,11 @@ public Task(String description) { this.description = description; } + public Task(boolean done, String description) { + this.description = description; + this.done = done; + } + public void setDone() { done = true; } @@ -18,4 +23,12 @@ public String toString() { } return "[" + indicator + "] " + this.description; } + + public String saveString() { + char indicator = '0'; + if (done) { + indicator = '1'; + } + return indicator + "-" + this.description; + } } \ No newline at end of file diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java index 023c97b369..725176f01d 100644 --- a/src/main/java/ToDo.java +++ b/src/main/java/ToDo.java @@ -3,8 +3,17 @@ public ToDo(String description) { super(description); } + public ToDo(boolean done, String description) { + super(done, description); + } + @Override public String toString() { return "[T]" + super.toString(); } + + @Override + public String saveString() { + return "T-" + super.saveString(); + } }