diff --git a/duke.txt b/duke.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index d82c0f4205..c01b4d1d24 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -11,4 +11,9 @@ public Deadline(String description, String deadline) { public String toString() { return super.toString() + "(by: " + deadline + ")"; } + + @Override + public String data() { + return super.data() + ", " + deadline; + } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 33c739aa8b..85d7c7b231 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,7 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; @@ -5,7 +9,7 @@ public class Duke { public static void main(String[] args) { System.out.println("Hello! I'm Duke\nWhat can I do for you?"); Scanner sc = new Scanner(System.in); - ArrayList tasks = new ArrayList<>(); + ArrayList tasks = initialize(); while (true) { try { String s = sc.next(); @@ -79,10 +83,61 @@ public static void main(String[] args) { String rest = sc.nextLine(); throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); } + modify(tasks); } catch (DukeException e) { System.out.println(e.getMessage()); } } sc.close(); } + + private static ArrayList initialize() { + File file = new File("./duke.txt"); + ArrayList tasks = new ArrayList<>(); + Scanner sc; + try { + file.createNewFile(); + sc = new Scanner(file); + } catch (IOException e) { + return tasks; + } + while (sc.hasNext()) { + String task = sc.nextLine(); + String[] split = task.split(", "); + for (String element : split) { + System.out.println(element); + } + if (split[0].equals("T")) { + tasks.add(new Task(split[2])); + if (split[1].equals("1")) { + tasks.get(tasks.size() - 1).markAsDone(); + } + } else if (split[0].equals("D")) { + tasks.add(new Deadline(split[2], split[3])); + if (split[1].equals("1")) { + tasks.get(tasks.size() - 1).markAsDone(); + } + } else if (split[0].equals("E")) { + tasks.add(new Event(split[2], split[3])); + if (split[1].equals("1")) { + tasks.get(tasks.size() - 1).markAsDone(); + } + } + } + return tasks; + } + + private static void modify(ArrayList tasks) { + String data = ""; + try { + FileWriter fw = new FileWriter("./duke.txt"); + for (Task task : tasks) { + data += task.data() + "\n"; + } + fw.write(data); + fw.close(); + } catch (IOException e) { + + } + } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 9a384ef01b..5dbca3bd23 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -11,4 +11,9 @@ public Event(String description, String time) { public String toString() { return super.toString() + "(at: " + time + ")"; } + + @Override + public String data() { + return super.data() + ", " + time; + } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 91dcdbe21d..ee1884ebb8 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -25,4 +25,8 @@ public void markAsDone() { public void markNotDone() { isDone = false; } + + public String data() { + return type + ", " + (isDone ? 1 : 0) + ", " + description; + } }