From 2b77d92a275d9ca9187a9f9422f2b4377c36c1ba Mon Sep 17 00:00:00 2001 From: geeeetyx Date: Wed, 15 Feb 2023 17:30:33 +0800 Subject: [PATCH] fix: level-6 and level-7 merge conflict resolved --- src/main/java/duke.txt | 2 + src/main/java/duke/Duke.java | 146 ++++++++++++++++++++-------- src/main/java/duke/print/Print.java | 30 +++++- 3 files changed, 136 insertions(+), 42 deletions(-) create mode 100644 src/main/java/duke.txt diff --git a/src/main/java/duke.txt b/src/main/java/duke.txt new file mode 100644 index 000000000..15d80e218 --- /dev/null +++ b/src/main/java/duke.txt @@ -0,0 +1,2 @@ +T|X|read book +T| |return book diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 76db231dd..8a2571c38 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -8,22 +8,39 @@ import duke.exception.EventException; import duke.exception.KeywordException; import duke.exception.TodoException; + import duke.task.Task; import static duke.print.Print.*; +import java.io.*; + import java.util.ArrayList; import java.util.Scanner; public class Duke { static final int USER_INPUT_EXPECTED_SIZE = 2; + static final String FILE_PATH = "src/main/java/duke.txt"; public static void main(String[] args) { showWelcomeMessage(); + File F = new File(FILE_PATH); + if (!F.exists()) { + new File("duke.txt"); + } + ArrayList taskList = new ArrayList<>(); + try { + loadFileContent(taskList); + } catch (FileNotFoundException e) { + println("A save file was not found. I shall create a new one."); + } catch (IOException e) { + println("Sorry, I could not read file."); + } + Scanner input = new Scanner(System.in); while (true) { @@ -31,6 +48,11 @@ public static void main(String[] args) { String text = input.nextLine(); if ("bye".equalsIgnoreCase(text)) { + try { + writeToFile(taskList); + } catch (IOException e) { + System.out.println("Something broke " + e.getMessage()); + } break; } @@ -110,6 +132,82 @@ public static void main(String[] args) { showExitMessage(); } + private static void loadFileContent(ArrayList taskList) throws IOException{ + File f = new File(FILE_PATH); + Scanner scanner = new Scanner(f); + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] task = line.split("\\|"); + String taskType = task[0]; + String taskDoneStatus = task[1]; + String taskDescription = task[2]; + + switch (taskType) { + case "T": + Todo new_Todo = new Todo(taskDescription); + if (taskDoneStatus.equalsIgnoreCase("X")) { + new_Todo.markDone(); + } + taskList.add(new_Todo); + + break; + + case "D": + String taskBy = task[3]; + Deadline new_deadline = new Deadline(taskDescription, taskBy); + if (taskDoneStatus.equalsIgnoreCase("X")) { + new_deadline.markDone(); + } + + taskList.add(new_deadline); + break; + + case "E": + String[] taskDuration = task[3].split(" ",2); + Event new_event = new Event(taskDescription, taskDuration[0], taskDuration[1]); + if (taskDoneStatus.equalsIgnoreCase("X")) { + new_event.markDone(); + } + + taskList.add(new_event); + break; + } + } + } + + /** + * Writes the task list to a save file + * + * @param taskList The taskList to update the save file + */ + private static void writeToFile(ArrayList taskList) throws IOException { + FileWriter fw = new FileWriter(FILE_PATH); + + for (Task taskToAppend : taskList) { + String toWrite; + + String taskType = taskToAppend.getTypeIcon(); + String taskDone = taskToAppend.getDoneIcon(); + String taskDescription = taskToAppend.getDescription(); + + toWrite = taskType + "|" + taskDone + "|" + taskDescription; + + if (taskToAppend.isDeadline()) { + String taskDeadline = taskToAppend.getBy(); + toWrite = toWrite + "|" + taskDeadline; + } else if (taskToAppend.isEvent()) { + String taskFrom = taskToAppend.getFrom(); + String taskTo = taskToAppend.getTo(); + toWrite = toWrite + "|" + taskFrom + " " + taskTo; + } + + toWrite = toWrite + '\n'; + fw.write(toWrite); + } + fw.close(); + } + private static void deleteOneTask(ArrayList taskList, String taskNumber) { int taskNumberToDelete = Integer.parseInt(taskNumber); taskNumberToDelete -= 1; @@ -126,11 +224,11 @@ private static void deleteOneTask(ArrayList taskList, String taskNumber) { * @param taskList The list to insert the task into * @param userInput The details of the task to be added */ - private static void addTodo(ArrayList taskList, String[] userInput) { + private static void addTodo(ArrayList taskList, String[] userInput) throws TodoException { Todo new_todo = new Todo(userInput[1]); taskList.add(new_todo); - printAddingOneTask(taskList, new_todo); + printAddingOneTask(new_todo, taskList); } /** @@ -140,13 +238,13 @@ private static void addTodo(ArrayList taskList, String[] userInput) { * @param userInput The details of the task to be added */ private static void addEvent(ArrayList taskList, String[] userInput) { - String[] eventDetails = userInput[1].split("/from | /to"); + String[] eventDetails = userInput[1].split(" /from | /to "); Event new_event = new Event(eventDetails[0], eventDetails[1], eventDetails[2]); taskList.add(new_event); - printAddingOneTask(taskList, new_event); + printAddingOneTask(new_event, taskList); } /** @@ -156,7 +254,7 @@ private static void addEvent(ArrayList taskList, String[] userInput) { * @param userInput The details of the task to be added */ private static void addDeadline(ArrayList taskList, String[] userInput) { - String[] taskDetails = userInput[1].split(" /by", 2); + String[] taskDetails = userInput[1].split(" /by ", 2); String taskName = taskDetails[0]; String taskDueDate = taskDetails[1]; @@ -165,7 +263,7 @@ private static void addDeadline(ArrayList taskList, String[] userInput) { taskList.add(new_deadline); - printAddingOneTask(taskList, new_deadline); + printAddingOneTask(new_deadline, taskList); } /** @@ -185,24 +283,10 @@ private static void unmarkSelectedTask(ArrayList taskList, String taskNumb printOneLine(); println(" OK, I've marked this task as not done yet:"); - printTypeAndStatus(selectedTask); - print(selectedTask.getDescription()); - - switch (selectedTask.getTypeIcon()) { - case "D": - println(" (by:" + selectedTask.getBy() + ")"); - break; - - case "E": - println("(from: " + selectedTask.getFrom() + " to:" + selectedTask.getTo() + ")"); - break; + printMarkingOrUnmarkingOneTask(selectedTask); + } - default: - println(""); - } - printOneLine(); - } /** * Sets a specified task as done @@ -220,22 +304,6 @@ private static void markSelectedTask(ArrayList taskList, String taskNumber printOneLine(); println(" Nice! I've marked this task as done:"); - printTypeAndStatus(selectedTask); - print(selectedTask.getDescription()); - - switch (selectedTask.getTypeIcon()) { - case "D": - println(" (by:" + selectedTask.getBy() + ")"); - break; - - case "E": - println("(from: " + selectedTask.getFrom() + " to:" + selectedTask.getTo() + ")"); - break; - - default: - println(""); - } - - printOneLine(); + printMarkingOrUnmarkingOneTask(selectedTask); } } \ No newline at end of file diff --git a/src/main/java/duke/print/Print.java b/src/main/java/duke/print/Print.java index 30956cb51..0b4d905bb 100644 --- a/src/main/java/duke/print/Print.java +++ b/src/main/java/duke/print/Print.java @@ -134,10 +134,10 @@ public static void printTaskList(ArrayList taskList) { /** * Prints the details of a newly added task * - * @param taskList The list of tasks, to print the size (i.e. how many tasks in the list) - * @param taskToAdd The task to print the details of. + * @param taskToAdd The newly added task to print the details of. + * @param taskList The list of tasks where the newly added task is found */ - public static void printAddingOneTask(ArrayList taskList, Task taskToAdd) { + public static void printAddingOneTask(Task taskToAdd, ArrayList taskList) { printOneLine(); println(" Got it. I've added this task:"); printTypeAndStatus(taskToAdd); @@ -163,6 +163,30 @@ public static void printAddingOneTask(ArrayList taskList, Task taskToAdd) printOneLine(); } + /** + * Prints a task when it has been marked on unmarked as DONE. + * @param selectedTask The task that has been marked or unmarked. + */ + public static void printMarkingOrUnmarkingOneTask(Task selectedTask) { + printTypeAndStatus(selectedTask); + print(selectedTask.getDescription()); + + switch (selectedTask.getTypeIcon()) { + case "D": + println(" (by:" + selectedTask.getBy() + ")"); + break; + + case "E": + println("(from: " + selectedTask.getFrom() + " to:" + selectedTask.getTo() + ")"); + break; + + default: + println(""); + } + + printOneLine(); + } + /** * Prints the details of a deleted task *