From cb85e8d126c0c29e5dd9e6e17a0fc845714f87b5 Mon Sep 17 00:00:00 2001 From: Taha Date: Mon, 2 Sep 2019 17:20:51 +0800 Subject: [PATCH] Implemented saving to saving to/reading from disk + Level-7 --- src/main/java/Deadline.java | 12 +- src/main/java/Duke.java | 19 ++- src/main/java/Event.java | 14 +- src/main/java/MainManager.java | 261 +++++++++++++++++++++++---------- src/main/java/Task.java | 2 + src/main/java/Todo.java | 13 +- 6 files changed, 226 insertions(+), 95 deletions(-) diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index db18196f52..e1ba15c832 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -8,11 +8,19 @@ public Deadline(String name, String time) { this.isDone = false; } + public String toFile() { + if(isDone) { + return "D-1-" + name + "-" + time; + } else { + return "D-0-" + name + "-" + time; + } + } + public String toString() { if (isDone) { - return "[D}[✓] " + name + " (by: " + time + ")"; + return "[D][✓] " + name + " (by: " + time + ")"; } else { - return "[D}[✗] " + name + " (by: " + time + ")"; + return "[D][✗] " + name + " (by: " + time + ")"; } } } \ No newline at end of file diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 926203d2ab..2044d49fb0 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,11 +1,18 @@ +import java.io.FileNotFoundException; + public class Duke { public static void main(String[] args) { - System.out.println(" ____________________________________________________________\n" + - " Hello! I'm Duke\n" + - " What can I do for you?\n" + - " ____________________________________________________________\n"); + System.out.println(" ____________________________________________________________\n" + + " Hello! I'm Duke\n" + + " What can I do for you?\n" + + " ____________________________________________________________\n"); - MainManager mm = new MainManager(); - mm.run(); + MainManager mm = new MainManager(); + try { + mm.readFromFile(); + } catch (FileNotFoundException fE) { + System.out.println(fE); + } + mm.run(); } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 19be359633..709d80e989 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -8,11 +8,19 @@ public Event(String name, String time) { this.isDone = false; } - public String toString() { + public String toFile() { if(isDone) { - return "[E}[✓] " + name + " (at: " + time + ")"; + return "E-1-" + name + "-" + time; + } else { + return "E-0-" + name + "-" + time; + } + } + + public String toString() { + if (isDone) { + return "[E][✓] " + name + " (at: " + time + ")"; } else { - return "[E}[✗] " + name + " (at: " + time + ")"; + return "[E][✗] " + name + " (at: " + time + ")"; } } } diff --git a/src/main/java/MainManager.java b/src/main/java/MainManager.java index cd279e639f..ab67839e09 100644 --- a/src/main/java/MainManager.java +++ b/src/main/java/MainManager.java @@ -1,6 +1,11 @@ -import java.lang.reflect.Array; +import java.nio.file.Paths; import java.util.Scanner; import java.util.ArrayList; +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Path; +import java.io.IOException; +import java.io.FileNotFoundException; public class MainManager { private ArrayList list; @@ -25,107 +30,203 @@ private String[] getDeadlineDetails(String command) throws TaskException{ return woCommand.split(" /by "); } + private void writeToFile() throws FileNotFoundException, IOException { + try { + Path p = Paths.get(System.getProperty("user.dir")); + File data = new File(p + "/data/duke.txt"); + FileWriter fw = new FileWriter(data); - public void run(){ - Scanner sc = new Scanner(System.in); + String toWrite = ""; + for(Task task : list) { + toWrite += task.toFile() + " \n"; + } - while(sc.hasNext()) { - String toPrint = sc.nextLine(); - String[] command = toPrint.split(" "); - switch(command[0]) { - case "bye": - System.out.println("Bye. Hope to see you again soon!"); - break; + fw.write(toWrite); + fw.close(); + } catch (FileNotFoundException Fe) { + System.out.println(Fe); + } catch (IOException IOe) { + System.out.println(IOe); + } + } - case "list": - for(int i = 0; i < list.size(); i++) { - if(list.get(i) != null) { - System.out.println(i + 1 + "." + list.get(i)); - } + public void readFromFile() throws FileNotFoundException{ + Path p = Paths.get(System.getProperty("user.dir")); + File data = new File(p + "/data/duke.txt"); + try { + Scanner sc = new Scanner(data); + + while(sc.hasNextLine()) { + String[] next = sc.nextLine().split("-"); + + switch (next[0]) { + case ("T"): + Task todo = new Todo(next[2]); + + if (Integer.valueOf(next[1]) == 1) { + todo.setDone(); } + list.add(todo); break; - case "done" : - try { - if(toPrint.length() <= 4) { - throw new DoneException(); - } - System.out.println("Nice! I've marked this task as done:"); - Task currTask = list.get(Integer.parseInt(command[1]) - 1); - currTask.setDone(); - System.out.println(" " + currTask); - } catch (DoneException doEx) { - System.out.println(doEx); + + case ("E"): + Task event = new Event(next[2], next[3]); + + if (Integer.valueOf(next[1]) == 1) { + event.setDone(); } + list.add(event); break; - case "todo": - try { - if(toPrint.length() <= 4) { - throw new TaskException(); - } - String todoName = toPrint.substring(5); - Task todo = new Todo(todoName); - list.add(todo); - System.out.println("Got it. I've added this task: "); - System.out.println(" " + todo); - System.out.println("Now you have " + list.size() + " tasks in the list."); + case ("D"): + Task deadline = new Deadline(next[2], next[3]); - } catch(TaskException toEx) { - System.out.println(toEx); + if (Integer.valueOf(next[1]) == 1) { + deadline.setDone(); } + list.add(deadline); break; + } + } + sc.close(); + } catch(FileNotFoundException fE) { + System.out.println(fE); + } - case "deadline": - try { - String[] deadlineDetails = getDeadlineDetails(toPrint); - Task deadline = new Deadline(deadlineDetails[0], deadlineDetails[1]); - list.add(deadline); - System.out.println("Got it. I've added this task: "); - System.out.println(" " + deadline); - System.out.println("Now you have " + list.size() + " tasks in the list."); - } catch(TaskException dlEx) { - System.out.println(dlEx); + + } + + public void run(){ + Scanner sc = new Scanner(System.in); + + while(sc.hasNext()) { + String toPrint = sc.nextLine(); + String[] command = toPrint.split(" "); + + switch(command[0]) { + case "bye": + System.out.println("Bye. Hope to see you again soon!"); + break; + + case "list": + for(int i = 0; i < list.size(); i++) { + if(list.get(i) != null) { + System.out.println(i + 1 + "." + list.get(i)); } - break; + } + break; - case "event": - try { - String[] eventDetails = getEventDetails(toPrint); - Task event = new Event(eventDetails[0], eventDetails[1]); - list.add(event); - System.out.println("Got it. I've added this task: "); - System.out.println(" " + event); - System.out.println("Now you have " + list.size() + " tasks in the list."); - } catch (TaskException evEx) { - System.out.println(evEx); + case "done" : + try { + if(toPrint.length() <= 4) { + throw new DoneException(); } - break; + System.out.println("Nice! I've marked this task as done:"); + Task currTask = list.get(Integer.parseInt(command[1]) - 1); + currTask.setDone(); + System.out.println(" " + currTask); + + writeToFile(); + } catch (DoneException doEx) { + System.out.println(doEx); + } catch (FileNotFoundException Fe) { + System.out.println(Fe); + } catch (IOException IOe) { + System.out.println(IOe); + } + break; - case "delete": - try { - if (command.length <= 1){ - throw new DeleteException(); - } else { - int index = Integer.parseInt(command[1]); + case "todo": + try { + if(toPrint.length() <= 4) { + throw new TaskException(); + } + String todoName = toPrint.substring(5); + Task todo = new Todo(todoName); + list.add(todo); + System.out.println("Got it. I've added this task: "); + System.out.println(" " + todo); + System.out.println("Now you have " + list.size() + " tasks in the list."); + + writeToFile(); + } catch(TaskException toEx) { + System.out.println(toEx); + } catch (FileNotFoundException Fe) { + System.out.println(Fe); + } catch (IOException IOe) { + System.out.println(IOe); + } + break; - if (command.length <= 1 || index > list.size() || index <= 0) { - throw new DeleteException(); - } + case "deadline": + try { + String[] deadlineDetails = getDeadlineDetails(toPrint); + Task deadline = new Deadline(deadlineDetails[0], deadlineDetails[1]); + list.add(deadline); + System.out.println("Got it. I've added this task: "); + System.out.println(" " + deadline); + System.out.println("Now you have " + list.size() + " tasks in the list."); + + writeToFile(); + } catch(TaskException dlEx) { + System.out.println(dlEx); + } catch (FileNotFoundException Fe) { + System.out.println(Fe); + } catch (IOException IOe) { + System.out.println(IOe); + } + break; + + case "event": + try { + String[] eventDetails = getEventDetails(toPrint); + Task event = new Event(eventDetails[0], eventDetails[1]); + list.add(event); + System.out.println("Got it. I've added this task: "); + System.out.println(" " + event); + System.out.println("Now you have " + list.size() + " tasks in the list."); + + writeToFile(); + } catch (TaskException evEx) { + System.out.println(evEx); + } catch (FileNotFoundException Fe) { + System.out.println(Fe); + } catch (IOException IOe) { + System.out.println(IOe); + } + break; + + case "delete": + try { + if (command.length <= 1){ + throw new DeleteException(); + } else { + int index = Integer.parseInt(command[1]); - System.out.println(" Noted. I've removed this task: "); - System.out.println(" " + list.remove(index - 1)); - System.out.println("Now you have " + list.size() + " tasks in the list."); + if (command.length <= 1 || index > list.size() || index <= 0) { + throw new DeleteException(); } - } catch(DeleteException deEx) { - System.out.println(deEx); + + System.out.println(" Noted. I've removed this task: "); + System.out.println(" " + list.remove(index - 1)); + System.out.println("Now you have " + list.size() + " tasks in the list."); + + writeToFile(); } - break; + } catch(DeleteException deEx) { + System.out.println(deEx); + } catch (FileNotFoundException Fe) { + System.out.println(Fe); + } catch (IOException IOe) { + System.out.println(IOe); + } + break; - default: - System.out.println(new UnknownCommandException()); - break; + default: + System.out.println(new UnknownCommandException()); + break; } if(toPrint.equals("bye")) { @@ -133,6 +234,4 @@ public void run(){ } } } - - } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index c718c6efee..7f2b53a0c1 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -14,6 +14,8 @@ public void setDone() { isDone = true; } + public abstract String toFile(); + @Override public abstract String toString(); } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index acbea24770..2f162a585d 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -5,11 +5,18 @@ public Todo(String name) { this.isDone = false; } - public String toString() { + public String toFile() { if(isDone) { - return "[T}[✓] " + name; + return "T-1-" + name; + } else { + return "T-0-" + name; + } + } + public String toString() { + if (isDone) { + return "[T][✓] " + name; } else { - return "[T}[✗] " + name; + return "[T][✗] " + name; } } }