From 87b38bf0f40c6f761c082cc37c497e667ce6c0c2 Mon Sep 17 00:00:00 2001 From: Mahesh1772 <87927591+Mahesh1772@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:35:28 +0800 Subject: [PATCH 1/2] Save file of tasks --- data/tasks.txt | 3 +++ src/main/java/Deadline.java | 5 ++++ src/main/java/Event.java | 5 ++++ src/main/java/Kratos.java | 54 +++++++++++++++++++++++++++++++++++++ src/main/java/Task.java | 48 +++++++++++++++++++++++++++++++++ src/main/java/Todo.java | 5 ++++ 6 files changed, 120 insertions(+) create mode 100644 data/tasks.txt diff --git a/data/tasks.txt b/data/tasks.txt new file mode 100644 index 000000000..05beccff8 --- /dev/null +++ b/data/tasks.txt @@ -0,0 +1,3 @@ +D|meeting|0|6pm +T|meeting|0 +E|dgd|1|5pm|6pm diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index c6ccfc76c..810b4ad86 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -11,5 +11,10 @@ public Deadline(String description, String by) { public String toString() { return "[D]" +"[" + this.getStatusIcon()+ "] " + super.toString() + " (by: " + by + ")"; } + + @Override + public String toFileString() { + return "D|" + super.toFileString() + "|" + by; // Prefix with "D" to indicate Deadline + } } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 2dffcfa9e..10fb3ff57 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -13,4 +13,9 @@ public Event(String description, String from, String to) { public String toString() { return "[E]" +"[" + this.getStatusIcon()+ "] " + super.toString() + " (from: " + from + " to: " + to +")"; } + + @Override + public String toFileString() { + return "E|" + super.toFileString() + "|" + from + "|" + to; // Prefix with "E" to indicate Event + } } \ No newline at end of file diff --git a/src/main/java/Kratos.java b/src/main/java/Kratos.java index ab7937761..c1ea01d30 100644 --- a/src/main/java/Kratos.java +++ b/src/main/java/Kratos.java @@ -1,9 +1,61 @@ +import java.util.ArrayList; import java.util.Scanner; +import java.io.File; +import java.io.FileWriter; +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.FileNotFoundException; public class Kratos { public final static int MAX_TASKS = 100; public static final String LINE = "----------------------------------------------------------------"; static Task[] tasksList = new Task[MAX_TASKS]; + private static final String FILE_PATH = "./data/tasks.txt"; + + // Method to save tasks to a file + public static void saveTasksToFile() { + try { + File file = new File(FILE_PATH); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); // Create directories if they don't exist + } + file.createNewFile(); // Create the file if it doesn't exist + try (FileWriter writer = new FileWriter(file)) { + for (int i = 0; i < count; i++) { + writer.write(tasksList[i].toFileString() + "\n"); + } + } + } catch (IOException e) { + System.err.println("Error saving tasks to file: " + e.getMessage()); + } + } + + // Method to load tasks from a file + // Method to load tasks from a file + public static void loadTasksFromFile() { + try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) { + String line; + while ((line = reader.readLine()) != null) { + Task task = Task.fromString(line); + if (task != null) { + tasksList[count++] = task; + } + } + } catch (FileNotFoundException e) { + // Handle the case where the file doesn't exist + System.err.println("File not found. Creating a new file..."); + File file = new File(FILE_PATH); + try { + file.createNewFile(); + } catch (IOException ioException) { + System.err.println("Error creating a new file: " + ioException.getMessage()); + } + } catch (IOException e) { + System.err.println("Error loading tasks from file: " + e.getMessage()); + } + } + public static int count = 0; @@ -96,6 +148,7 @@ public static void displayTasks(int count) { // Main method public static void main(String[] args) { greet(); + loadTasksFromFile(); // Load tasks from file when the program boots up String userInput; Scanner in = new Scanner(System.in); while (true) { @@ -127,6 +180,7 @@ else if (userInput.startsWith("event")) { KratosException.handleException(e, userInput); } } + saveTasksToFile(); } end(); } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index c4306a3e5..085a0de2c 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -39,4 +39,52 @@ public void unmarkTask() { public String toString() { return description; } + + /** + * Converts the task to a string representation that can be written to a file. + * + * @return A string representation of the task for file storage. + */ + public String toFileString() { + // Implement how you want to represent the task as a string for file storage + // For example, you can concatenate the description and status with a delimiter + return description + "|" + (isDone ? "1" : "0"); // Example format: "description|status" + } + + /** + * Parses a string from a file into a Task object. + * + * @param fileString The string read from the file representing a task. + * @return The Task object parsed from the string. + */ + public static Task fromString(String fileString) { + String[] parts = fileString.split("\\|"); + String taskType = parts[0]; // Extract task type + String description = parts[1]; + boolean isDone = parts[2].equals("1"); // Extract status + + Task task; + switch (taskType) { + case "D": + task = new Deadline(description, parts[3]); // Create Deadline task + break; + case "E": + task = new Event(description, parts[3], parts[4]); // Create Event task + break; + case "T": + task = new Todo(description); // Create Todo task + break; + default: + // Handle unknown task type + task = null; + break; + } + + if (task != null && isDone) { + task.markTask(); // Mark task as done if status is '1' + } + + return task; + } + } diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index 3651783e1..30540385f 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -10,5 +10,10 @@ public Todo(String description) { public String toString() { return "[T]" +"[" + this.getStatusIcon()+ "] " + super.toString(); } + + @Override + public String toFileString() { + return "T|" + super.toFileString(); // Prefix with "T" to indicate Todo + } } From f62e08a02e865a61947dbb76b4f6773adab59c94 Mon Sep 17 00:00:00 2001 From: Mahesh1772 <87927591+Mahesh1772@users.noreply.github.com> Date: Thu, 22 Feb 2024 16:42:36 +0800 Subject: [PATCH 2/2] Refactor Exception class --- src/main/java/Kratos.java | 8 ++++---- src/main/java/KratosException.java | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/Kratos.java b/src/main/java/Kratos.java index c1ea01d30..50fed1aff 100644 --- a/src/main/java/Kratos.java +++ b/src/main/java/Kratos.java @@ -27,7 +27,7 @@ public static void saveTasksToFile() { } } } catch (IOException e) { - System.err.println("Error saving tasks to file: " + e.getMessage()); + KratosException.handleException(e, "Error saving tasks to file: " + e.getMessage()); } } @@ -44,15 +44,15 @@ public static void loadTasksFromFile() { } } catch (FileNotFoundException e) { // Handle the case where the file doesn't exist - System.err.println("File not found. Creating a new file..."); + KratosException.handleException(e, "File not found. Creating a new file..."); File file = new File(FILE_PATH); try { file.createNewFile(); } catch (IOException ioException) { - System.err.println("Error creating a new file: " + ioException.getMessage()); + KratosException.handleException(ioException, "Error creating a new file: " + ioException.getMessage()); } } catch (IOException e) { - System.err.println("Error loading tasks from file: " + e.getMessage()); + KratosException.handleException(e, "Error loading tasks from file: " + e.getMessage()); } } diff --git a/src/main/java/KratosException.java b/src/main/java/KratosException.java index ee7624b85..a60681b40 100644 --- a/src/main/java/KratosException.java +++ b/src/main/java/KratosException.java @@ -1,9 +1,16 @@ +import java.io.FileNotFoundException; +import java.io.IOException; + public class KratosException extends Exception { public static void handleException(Exception e, String command) { if (e instanceof IllegalArgumentException) { commandNotFound(); } else if (e instanceof ArrayIndexOutOfBoundsException) { blankAfterTask(command); + } else if (e instanceof IOException) { + System.out.println(command); + } else if (e instanceof FileNotFoundException) { + System.out.println(command); } else { otherIncurredException(); }