From 898968db7fa4777dfa7994caf3c6ade9b0b722d4 Mon Sep 17 00:00:00 2001 From: aaronxujiachen <1943999635x@gmail.com> Date: Thu, 21 Sep 2023 12:09:47 +0800 Subject: [PATCH] Add Dude Level 7 --- data/duke.txt | 0 src/dude/Deadline.java | 21 ++++++++--- src/dude/Dude.java | 81 +++++++++++++++++++++++++++++++++++++++--- src/dude/Event.java | 29 ++++++++++----- src/dude/Task.java | 34 ++++++++++++------ 5 files changed, 139 insertions(+), 26 deletions(-) create mode 100644 data/duke.txt diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 000000000..e69de29bb diff --git a/src/dude/Deadline.java b/src/dude/Deadline.java index 45e1e24ba..cd8e7f518 100644 --- a/src/dude/Deadline.java +++ b/src/dude/Deadline.java @@ -1,17 +1,30 @@ package dude; public class Deadline extends Task { - String by; + private String by; - // Constructor to initialize a deadline with a description and a due date public Deadline(String description, String by) { super(description); this.by = by; - this.type = "[D]"; // Type [D] indicates this is a dude.Deadline + this.type = "[D]"; + } + + @Override + public String toFileFormat() { + return super.toFileFormat() + " | " + by; + } + + public static Deadline fromFileFormat(String fileString) { + String[] parts = fileString.split("\\s\\|\\s"); + Deadline deadline = new Deadline(parts[2], parts[3]); + if (parts[1].equals("1")) { + deadline.markAsDone(); + } + return deadline; } @Override public String toString() { - return getType() + getStatusIcon() + " " + description + " (by: " + by + ")"; + return super.toString() + " (by: " + by + ")"; } } diff --git a/src/dude/Dude.java b/src/dude/Dude.java index 0a30483c9..b784275cf 100644 --- a/src/dude/Dude.java +++ b/src/dude/Dude.java @@ -2,9 +2,16 @@ import java.util.ArrayList; import java.util.Scanner; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.FileNotFoundException; public class Dude { + private static final String DIRECTORY_PATH = "./data"; + private static final String FILE_PATH = DIRECTORY_PATH + "/duke.txt"; + // Method to draw horizontal lines public static void drawLine() { for (int i = 0; i < 30; i++) { @@ -32,7 +39,8 @@ public static void hiDude() { // Method to handle the storage of tasks public static void storeDude() { Scanner scan = new Scanner(System.in); - ArrayList tasks = new ArrayList<>(); + setupFile(); + ArrayList tasks = loadFromFile(); String input = scan.nextLine(); while (!input.isEmpty()) { @@ -67,7 +75,6 @@ public static void storeDude() { scan.close(); } - private static void listTasks(ArrayList tasks) { System.out.println("Here are the tasks in your list:"); for (int i = 0; i < tasks.size(); i++) { @@ -86,9 +93,9 @@ private static void addTodoTask(ArrayList tasks, String input) throws Dude } tasks.add(new Task(taskDescription)); printAddedTask(tasks); + saveToFile(tasks); } - private static void addDeadlineTask(ArrayList tasks, String input) throws DudeException { int byIndex = input.indexOf("/by"); if (byIndex == -1) { @@ -107,6 +114,7 @@ private static void addDeadlineTask(ArrayList tasks, String input) throws tasks.add(new Deadline(taskDescription, by)); printAddedTask(tasks); + saveToFile(tasks); } private static void addEventTask(ArrayList tasks, String input) throws DudeException { @@ -134,6 +142,7 @@ private static void addEventTask(ArrayList tasks, String input) throws Dud tasks.add(new Event(taskDescription, from, to)); printAddedTask(tasks); + saveToFile(tasks); } private static void markOrUnmarkTask(ArrayList tasks, String input) { @@ -158,6 +167,8 @@ private static void markOrUnmarkTask(ArrayList tasks, String input) { } catch (NumberFormatException e) { System.out.println("Invalid task index format."); } + + saveToFile(tasks); } private static void printAddedTask(ArrayList tasks) { @@ -187,8 +198,71 @@ private static void deleteTask(ArrayList tasks, String input) { } catch (NumberFormatException e) { System.out.println("Invalid task index format."); } + + saveToFile(tasks); + } + + private static void setupFile() { + // Check if the directory exists, if not, create it + File directory = new File(DIRECTORY_PATH); + if (!directory.exists()) { + directory.mkdir(); + } + + // Check if the file exists, if not, create it + File file = new File(FILE_PATH); + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + System.out.println("Error creating file: " + e.getMessage()); + } + } } + private static ArrayList loadFromFile() { + ArrayList tasks = new ArrayList<>(); + try { + File file = new File(FILE_PATH); + Scanner fileReader = new Scanner(file); + + while (fileReader.hasNextLine()) { + String line = fileReader.nextLine(); + // Assuming the first char denotes the type of task + char taskType = line.charAt(0); + switch (taskType) { + case 'T': + tasks.add(Task.fromFileFormat(line)); + break; + case 'D': + tasks.add(Deadline.fromFileFormat(line)); + break; + case 'E': + tasks.add(Event.fromFileFormat(line)); + break; + } + } + + fileReader.close(); + } catch (FileNotFoundException e) { + System.out.println("Error reading from file: " + e.getMessage()); + } + return tasks; + } + + private static void saveToFile(ArrayList tasks) { + try { + FileWriter fileWriter = new FileWriter(FILE_PATH); + for (Task task : tasks) { + fileWriter.write(task.toFileFormat() + "\n"); + } + fileWriter.close(); + } catch (IOException e) { + System.out.println("Error writing to file: " + e.getMessage()); + } + } + + // Method to print the goodbye message public static void byeDude() { System.out.println("Bye. Hope to see you again soon!"); @@ -201,4 +275,3 @@ public static void main(String[] args) { storeDude(); } } - diff --git a/src/dude/Event.java b/src/dude/Event.java index 9dd86d126..7872f4a1d 100644 --- a/src/dude/Event.java +++ b/src/dude/Event.java @@ -1,19 +1,32 @@ package dude; public class Event extends Task { - String startDate; - String endDate; + private String from; + private String to; - // Constructor to initialize an event with a description, start date, and end date - public Event(String description, String startDate, String endDate) { + public Event(String description, String from, String to) { super(description); - this.startDate = startDate; - this.endDate = endDate; - this.type = "[E]"; // Type [E] indicates this is an dude.Event + this.from = from; + this.to = to; + this.type = "[E]"; + } + + @Override + public String toFileFormat() { + return super.toFileFormat() + " | " + from + " | " + to; + } + + public static Event fromFileFormat(String fileString) { + String[] parts = fileString.split("\\s\\|\\s"); + Event event = new Event(parts[2], parts[3], parts[4]); + if (parts[1].equals("1")) { + event.markAsDone(); + } + return event; } @Override public String toString() { - return getType() + getStatusIcon() + " " + description + " (from: " + startDate + " to: " + endDate + ")"; + return super.toString() + " (from: " + from + " to: " + to + ")"; } } diff --git a/src/dude/Task.java b/src/dude/Task.java index 1d5dbb0b1..5870b4e0c 100644 --- a/src/dude/Task.java +++ b/src/dude/Task.java @@ -1,29 +1,43 @@ package dude; public class Task { - // Instance variables to store task description and status protected String description; protected boolean isDone; protected String type; - // Constructor to initialize a task with a description public Task(String description) { this.description = description; this.isDone = false; - this.type = "[T]"; // Default type is [T] + this.type = "[T]"; // Represents a generic task. } - public String getType() { - return type; - } - - // Method to get the status icon based on whether the task is done or not public String getStatusIcon() { return (isDone ? "[X]" : "[ ]"); } + public String getDescription() { + return description; + } + + public void markAsDone() { + isDone = true; + } + + public String toFileFormat() { + return type.charAt(1) + " | " + (isDone ? "1" : "0") + " | " + description; + } + + public static Task fromFileFormat(String fileString) { + String[] parts = fileString.split("\\s\\|\\s"); + Task task = new Task(parts[2]); + if (parts[1].equals("1")) { + task.markAsDone(); + } + return task; + } + @Override - public String toString () { - return getType() + getStatusIcon() + " " + description; + public String toString() { + return type + getStatusIcon() + " " + description; } }