From 4197612c99bd3dd2588c2f80d9e71e4dc45177ff Mon Sep 17 00:00:00 2001 From: QianChangru Date: Tue, 5 Sep 2023 17:28:53 +0800 Subject: [PATCH] Finish Level 7. Save --- data/duke.txt | 2 + src/main/java/Deadline.java | 10 ++++ src/main/java/Duke.java | 94 +++++++++++++++++++++++++++++--- src/main/java/DukeException.java | 2 +- src/main/java/Event.java | 11 ++++ src/main/java/Parser.java | 3 + src/main/java/Storage.java | 74 +++++++++++++++++++++++++ src/main/java/Task.java | 9 +++ src/main/java/TaskList.java | 17 ++++++ src/main/java/ToDo.java | 9 +++ src/main/java/Ui.java | 2 + 11 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 data/duke.txt create mode 100644 src/main/java/Parser.java create mode 100644 src/main/java/Storage.java create mode 100644 src/main/java/TaskList.java create mode 100644 src/main/java/Ui.java diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 0000000000..ff43a5977a --- /dev/null +++ b/data/duke.txt @@ -0,0 +1,2 @@ +T | 1 | hihi +T | 0 | hihihi \ No newline at end of file diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index c3b5f879fe..fb74aa8a01 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -5,8 +5,18 @@ public Deadline(String description, String by) { this.by = by; } + public Deadline(String description, boolean isDone, String by) { + super(description, isDone); + this.by = by; + } + @Override public String toString() { return "[D]" + super.toString() + " (by: " + this.by + ")"; } + + @Override + public String toTxt() { + return "D | " + super.toTxt() + " | " + this.by; + } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index df33c7d616..fb688cb58a 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,20 +1,36 @@ import java.util.ArrayList; import java.util.Scanner; -public class Duke { - public static void main(String[] args) { +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; - Scanner userInput = new Scanner(System.in); +public class Duke { - ArrayList list = new ArrayList<>(); + private Storage storage; + private TaskList tasks; + private Ui ui = new Ui(); + private static final String filePath = "./data/duke.txt"; + + public static void main(String[] args) { System.out.println("____________________________________________________________"); System.out.println(" Hello! I'm Jarvis"); System.out.println(" What can I do for you?"); System.out.println("____________________________________________________________"); - String command = userInput.nextLine(); + ArrayList list; + + try { + list = load(); + } catch (IOException e) { + throw new DukeException("Unable to find file with data."); + } + + Scanner userInput = new Scanner(System.in); + String command = userInput.nextLine(); while (!command.startsWith("bye")) { try{ @@ -33,9 +49,10 @@ public static void main(String[] args) { System.out.println(" Nice! I've marked this task as done:"); System.out.println(" " + list.get(Integer.valueOf(command.split(" ")[1]) - 1).toString()); System.out.println("____________________________________________________________"); + rewriteFile(list); } else if (command.startsWith("todo")) { if (command.split(" ", 2).length == 1) { - throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); + throw new DukeException(" OOPS!!! The description of a todo cannot be empty."); } ToDo newToDo = new ToDo(command.split(" ", 2)[1]); list.add(newToDo); @@ -44,6 +61,7 @@ public static void main(String[] args) { System.out.println(" " + newToDo.toString()); System.out.println(" Now you have " + list.size() + " tasks in the list."); System.out.println("____________________________________________________________"); + addToFile(list); } else if (command.startsWith("deadline")) { String deadline = command.split(" /by ", 2)[1]; String name = command.split(" /by ", 2)[0].split(" ", 2)[1]; @@ -54,6 +72,7 @@ public static void main(String[] args) { System.out.println(" " + newDeadline.toString()); System.out.println(" Now you have " + list.size() + " tasks in the list."); System.out.println("____________________________________________________________"); + addToFile(list); } else if (command.startsWith("event")) { String startTime = command.split(" /from ", 2)[1] .split(" /to ", 2)[0]; @@ -66,6 +85,7 @@ public static void main(String[] args) { System.out.println(" " + newEvent.toString()); System.out.println(" Now you have " + list.size() + " tasks in the list."); System.out.println("____________________________________________________________"); + addToFile(list); } else if (command.startsWith("delete")) { Task re = list.remove(Integer.valueOf(command.split(" ")[1]) - 1); System.out.println("____________________________________________________________"); @@ -73,8 +93,9 @@ public static void main(String[] args) { System.out.println(" " + re.toString()); System.out.println(" Now you have " + list.size() + " tasks in the list."); System.out.println("____________________________________________________________"); + rewriteFile(list); } else { - throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); + throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-("); } } catch (DukeException e) { System.out.println("____________________________________________________________"); @@ -89,4 +110,63 @@ public static void main(String[] args) { System.out.println(" Bye. Hope to see you again soon!"); System.out.println("____________________________________________________________"); } + + public static ArrayList load() throws IOException { + File dir = new File("./data"); + if (!dir.exists()) { + dir.mkdir(); + } + File f = new File(filePath); + f.createNewFile(); + Scanner s = new Scanner(f); + ArrayList tasks = new ArrayList<>(); + while (s.hasNext()) { + tasks.add(addTask(s.nextLine())); + } + return tasks; + } + + private static Task addTask(String input) { + String taskType = input.split(" \\| ")[0]; + boolean isComplete = input.split(" \\| ")[1].equals("1"); + String description = input.split(" \\| ")[2]; + if (taskType.equals("T")) { + return new ToDo(description, isComplete); + } else if (taskType.equals("D")) { + return new Deadline(description, isComplete, input.split(" \\| ")[3]); + } else { + return new Event(description, isComplete, input.split(" \\| ")[3], input.split(" \\| ")[4]); + } + } + + + public static void addToFile(ArrayList list) throws DukeException { + try { + FileWriter fw = new FileWriter(filePath, true); + if (list.size() == 1) { + fw.write(list.get(0).toTxt()); + } else { + fw.write("\n" + list.get(list.size() - 1).toTxt()); + } + fw.close(); + } catch (IOException e) { + throw new DukeException("Unable to write to file."); + } + } + + public static void rewriteFile(ArrayList list) throws DukeException { + try { + FileWriter fw = new FileWriter(filePath); + for (int i = 0; i < list.size(); i++) { + if (i == list.size() - 1) { + fw.write(list.get(i).toTxt()); + } else { + fw.write(list.get(i).toTxt() + "\n"); + } + } + fw.close(); + } catch (IOException e) { + throw new DukeException("Unable to write to file."); + } + } } diff --git a/src/main/java/DukeException.java b/src/main/java/DukeException.java index c858d79e91..b106641951 100644 --- a/src/main/java/DukeException.java +++ b/src/main/java/DukeException.java @@ -1,4 +1,4 @@ -public class DukeException extends Exception{ +public class DukeException extends RuntimeException{ public DukeException(String message) { super(message); } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 7ef9a112ee..329c6010ee 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -7,8 +7,19 @@ public Event(String description, String startTime, String endTime) { this.endTime = endTime; } + public Event(String description, boolean isDone, String startTime, String endTime) { + super(description, isDone); + this.startTime = startTime; + this.endTime = endTime; + } + @Override public String toString() { return "[E]" + super.toString() + " (from: " + startTime + " to: " + endTime + ")"; } + + @Override + public String toTxt() { + return "E | " + super.toTxt() + " | " + this.startTime + " | " + this.endTime; + } } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 0000000000..0019ad645a --- /dev/null +++ b/src/main/java/Parser.java @@ -0,0 +1,3 @@ +public class Parser { + +} diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java new file mode 100644 index 0000000000..c674bbcaa2 --- /dev/null +++ b/src/main/java/Storage.java @@ -0,0 +1,74 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + private String filePath; + + public Storage(String filePath) { + this.filePath = filePath; + } + + public ArrayList load() throws IOException { + File dir = new File("./data"); + if (!dir.exists()) { + dir.mkdir(); + } + File f = new File(filePath); + f.createNewFile(); + Scanner s = new Scanner(f); + ArrayList tasks = new ArrayList<>(); + while (s.hasNext()) { + tasks.add(addTask(s.nextLine())); + } + return tasks; + } + + private Task addTask(String input) { + String taskType = input.split(" \\| ")[0]; + boolean isComplete = input.split(" \\| ")[1].equals("1"); + String description = input.split(" \\| ")[2]; + if (taskType.equals("T")) { + return new ToDo(description, isComplete); + } else if (taskType.equals("D")) { + return new Deadline(description, isComplete, input.split(" \\| ")[3]); + } else { + return new Event(description, isComplete, input.split(" \\| ")[3], input.split(" \\| ")[4]); + } + } + + + public void addToFile(ArrayList list) throws DukeException { + try { + FileWriter fw = new FileWriter(filePath, true); + if (list.size() == 1) { + fw.write(list.get(0).toTxt()); + } else { + fw.write("\n" + list.get(list.size() - 1).toTxt()); + } + fw.close(); + } catch (IOException e) { + throw new DukeException("Unable to write to file."); + } + } + + public void rewriteFile(ArrayList list) throws DukeException { + try { + FileWriter fw = new FileWriter(filePath); + for (int i = 0; i < list.size(); i++) { + if (i == list.size() - 1) { + fw.write(list.get(i).toTxt()); + } else { + fw.write(list.get(i).toTxt() + "\n"); + } + } + fw.close(); + } catch (IOException e) { + throw new DukeException("Unable to write to file."); + } + } +} + diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 5bab919966..13c2459b2e 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -7,6 +7,11 @@ public Task(String description) { this.isDone = false; } + public Task(String description, boolean isDone) { + this.description = description; + this.isDone = isDone; + } + public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } @@ -20,4 +25,8 @@ public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } + public String toTxt() { + return (this.isDone ? "1" : "0") + " | " + this.description; + } + } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 0000000000..0fcd04dd5e --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,17 @@ +import java.util.ArrayList; + +public class TaskList { + protected ArrayList tasks; + + public TaskList() { + this.tasks = new ArrayList<>(); + } + + public TaskList(ArrayList tasks) { + this.tasks = tasks; + } + + public boolean isEmpty() { + return tasks.isEmpty(); + } +} diff --git a/src/main/java/ToDo.java b/src/main/java/ToDo.java index ee72ef43cb..18ec05abff 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(String description, boolean isDone) { + super(description, isDone); + } + @Override public String toString() { return "[T]" + super.toString(); } + + @Override + public String toTxt() { + return "T | " + super.toTxt(); + } } diff --git a/src/main/java/Ui.java b/src/main/java/Ui.java new file mode 100644 index 0000000000..30d98a7015 --- /dev/null +++ b/src/main/java/Ui.java @@ -0,0 +1,2 @@ +public class Ui { +}