From 4d45b43a9c75114cbddaa8ff3a5ad08dbffcc331 Mon Sep 17 00:00:00 2001 From: TyrusLye Date: Mon, 23 Oct 2023 18:20:48 +0800 Subject: [PATCH] A-Storage --- .../java/URBOI_PACKIN/ResponseController.java | 93 +------------------ .../java/URBOI_PACKIN/Storage/Storage.java | 93 +++++++++++++++++++ 2 files changed, 97 insertions(+), 89 deletions(-) create mode 100644 src/main/java/URBOI_PACKIN/Storage/Storage.java diff --git a/src/main/java/URBOI_PACKIN/ResponseController.java b/src/main/java/URBOI_PACKIN/ResponseController.java index 8c95df38fe..a16fbd9c40 100644 --- a/src/main/java/URBOI_PACKIN/ResponseController.java +++ b/src/main/java/URBOI_PACKIN/ResponseController.java @@ -1,5 +1,6 @@ package URBOI_PACKIN; +import URBOI_PACKIN.Storage.Storage; import URBOI_PACKIN.TaskTypes.Deadline; import URBOI_PACKIN.TaskTypes.Event; import URBOI_PACKIN.TaskTypes.Todo; @@ -10,6 +11,9 @@ import java.util.ArrayList; import java.io.*; +import static URBOI_PACKIN.Storage.Storage.loadTasksFromFile; +import static URBOI_PACKIN.Storage.Storage.saveTasksToFile; + public class ResponseController { //private static final String FILE_PATH = "src/main/java/tasks.txt"; private static final String FILE_PATH = "tasks.txt/"; @@ -97,77 +101,6 @@ public String getResponse(String command) { return response.toString(); } - /** - * Loads tasks from a file into the task list. - * - * @param tasks The ArrayList to store the loaded tasks. - */ - private static void loadTasksFromFile(ArrayList tasks) { - try { - File file = new File(FILE_PATH); - if (file.exists()) { - Scanner fileScanner = new Scanner(file); - while (fileScanner.hasNextLine()) { - String line = fileScanner.nextLine(); - Task task = createTaskFromLine(line); - if (task != null) { - tasks.add(task); - } - } - fileScanner.close(); - } - } catch (FileNotFoundException e) { - // Handle file not found exception - System.out.println("File not found: " + FILE_PATH); - } - } - - /** - * Creates a URBOI_PACKIN.Task object from a line of text in the specified format. - * - * @param line The line of text containing task details. - * @return A URBOI_PACKIN.Task object representing the task described in the line, or null if parsing fails. - */ - private static Task createTaskFromLine(String line) { - String[] parts = line.split(" \\| "); - if (parts.length < 3) { - return null; - } - - String type = parts[0]; - String status = parts[1]; - String description = parts[2]; - - Task task = null; - switch (type) { - case "T": - task = new Todo(description); - break; - case "D": - if (parts.length >= 4) { - String by = parts[3]; - task = new Deadline(description, LocalDateTime.parse(by, DateTimeFormatter.ISO_LOCAL_DATE_TIME)); - } - break; - case "E": - if (parts.length >= 5) { - String from = parts[3]; - String to = parts[4]; - task = new Event(description, from, to); - } - break; - } - - if (task != null) { - if (status.equals("1")) { - task.markDone(); - } else { - task.markNotDone(); - } - } - - return task; - } private static void findTasks(ArrayList tasks, String keyword) { System.out.println("Here are the matching tasks in your list:"); @@ -183,24 +116,6 @@ private static void findTasks(ArrayList tasks, String keyword) { System.out.println("No matching tasks found."); } } - - /** - * Save tasks to a file. - * - * @param tasks The ArrayList of tasks to save. - */ - private static void saveTasksToFile(ArrayList tasks) { - try { - FileWriter fileWriter = new FileWriter(FILE_PATH); - for (Task task : tasks) { - fileWriter.write(task.toFileString() + System.lineSeparator()); - } - fileWriter.close(); - } catch (IOException e) { - // Handle IO exception - System.out.println("Error saving tasks to file: " + e.getMessage()); - } - } } diff --git a/src/main/java/URBOI_PACKIN/Storage/Storage.java b/src/main/java/URBOI_PACKIN/Storage/Storage.java new file mode 100644 index 0000000000..9f99eb7edc --- /dev/null +++ b/src/main/java/URBOI_PACKIN/Storage/Storage.java @@ -0,0 +1,93 @@ +package URBOI_PACKIN.Storage; + +import URBOI_PACKIN.Task; +import URBOI_PACKIN.TaskTypes.Deadline; +import URBOI_PACKIN.TaskTypes.Event; +import URBOI_PACKIN.TaskTypes.Todo; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Scanner; + +public class Storage { + private static final String FILE_PATH = "tasks.txt"; + + public static void loadTasksFromFile(ArrayList tasks) { + try { + File file = new File(FILE_PATH); + if (file.exists()) { + Scanner fileScanner = new Scanner(file); + while (fileScanner.hasNextLine()) { + String line = fileScanner.nextLine(); + Task task = createTaskFromLine(line); + if (task != null) { + tasks.add(task); + } + } + fileScanner.close(); + } + } catch (FileNotFoundException e) { + // Handle file not found exception + System.out.println("File not found: " + FILE_PATH); + } + } + + public static void saveTasksToFile(ArrayList tasks) { + try { + FileWriter fileWriter = new FileWriter(FILE_PATH); + for (Task task : tasks) { + fileWriter.write(task.toFileString() + System.lineSeparator()); + } + fileWriter.close(); + } catch (IOException e) { + // Handle IO exception + System.out.println("Error saving tasks to file: " + e.getMessage()); + } + } + + private static Task createTaskFromLine(String line) { + String[] parts = line.split(" \\| "); + if (parts.length < 3) { + return null; + } + + String type = parts[0]; + String status = parts[1]; + String description = parts[2]; + + Task task = null; + switch (type) { + case "T": + task = new Todo(description); + break; + case "D": + if (parts.length >= 4) { + String by = parts[3]; + task = new Deadline(description, LocalDateTime.parse(by, DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + } + break; + case "E": + if (parts.length >= 5) { + String from = parts[3]; + String to = parts[4]; + task = new Event(description, from, to); + } + break; + } + + if (task != null) { + if (status.equals("1")) { + task.markDone(); + } else { + task.markNotDone(); + } + } + + return task; + } +}