From f362e1a935d03ecf16f2464501fb6bbd638f142b Mon Sep 17 00:00:00 2001 From: wuzhzn Date: Wed, 15 Feb 2023 02:21:32 +0800 Subject: [PATCH] Reformat code and added check file access code --- src/main/java/duke/Duke.java | 65 +++++++++++-------- src/main/java/duke/Task.java | 7 +- src/main/java/duke/UI.java | 21 +++--- .../java/duke/exception/DukeException.java | 26 ++++---- .../java/duke/exception/EmptyListError.java | 3 +- .../duke/exception/EmptyTaskDescription.java | 2 +- .../exception/UnknownInputFieldError.java | 2 +- src/main/java/duke/storage/Storage.java | 46 ++++++++----- src/main/java/duke/storage/duke.txt | 12 +--- src/main/java/duke/task/Deadline.java | 5 +- src/main/java/duke/task/Event.java | 5 +- src/main/java/duke/task/Todo.java | 6 +- 12 files changed, 110 insertions(+), 90 deletions(-) diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 4593bc7dc..cfaef7863 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -15,16 +15,17 @@ public class Duke { public static ArrayList tasksList = new ArrayList<>(); - public static final String filePath = "src/main/java/duke/storage/duke.txt"; - public static void main(String[] args) { try { - List taskLists = Storage.convertToArray(filePath); + Storage.checkFileAccess(); + List taskLists = Storage.convertToList(); tasksList.addAll(taskLists); - } catch (FileNotFoundException e) { + } catch (FileNotFoundException err) { System.out.println("File not found"); + } catch (IOException err) { + System.out.println("Something went wrong: " + err.getMessage()); } String logo = " ____ _ \n" @@ -38,22 +39,23 @@ public static void main(String[] args) { UI.showWelcomeMessage(); do { input = in.nextLine(); - if (DukeException.hasError(input)){ + if (DukeException.hasError(input)) { continue; } - command(input); + run(input); } while (!input.equals("bye")); } - private static void command(String input) { + private static void run(String input) { String[] inputWords = input.split(" "); - switch (inputWords[0]){ + switch (inputWords[0]) { case "todo": input = input.replaceFirst("todo", "").trim(); UI.printTodo(input); - Todo t = new Todo(input, "T"); - tasksList.add(t); + Todo todo = new Todo(input, "T"); + tasksList.add(todo); UI.printTaskList(tasksList.size()); + updateFile(); break; case "deadline": input = input.replaceFirst("deadline", "").trim(); @@ -61,9 +63,10 @@ private static void command(String input) { String taskName = input.substring(0, indexOfSlash - 1); String by = input.substring(indexOfSlash + 4); UI.printDeadline(taskName, by); - Deadline d = new Deadline(taskName, "D", by); - tasksList.add(d); + Deadline deadline = new Deadline(taskName, "D", by); + tasksList.add(deadline); UI.printTaskList(tasksList.size()); + updateFile(); break; case "event": input = input.replaceFirst("event", "").trim(); @@ -73,14 +76,15 @@ private static void command(String input) { String start = input.substring(indexOfSlash + 6, lastIndexOfSlash - 1); String end = input.substring(lastIndexOfSlash + 4); UI.printEvent(taskName, start, end); - Event e = new Event(taskName, "E", start, end); - tasksList.add(e); + Event event = new Event(taskName, "E", start, end); + tasksList.add(event); UI.printTaskList(tasksList.size()); + updateFile(); break; case "list": - try{ + try { printList(); - } catch (EmptyListError err){ + } catch (EmptyListError err) { UI.printMessage("There is nothing inside the list"); } break; @@ -89,12 +93,14 @@ private static void command(String input) { UI.printMessage("Nice! I've marked this task as done:"); tasksList.get(taskNum).markAsDone(); System.out.println(" [" + tasksList.get(taskNum).getStatusIcon() + "] " + tasksList.get(taskNum).description); + updateFile(); break; case "unmark": taskNum = Integer.parseInt(inputWords[1]) - 1; UI.printMessage("OK, I've marked this task as not done yet:"); tasksList.get(taskNum).markAsUndone(); System.out.println(" [" + tasksList.get(taskNum).getStatusIcon() + "] " + tasksList.get(taskNum).description); + updateFile(); break; case "delete": taskNum = Integer.parseInt(inputWords[1]) - 1; @@ -102,27 +108,30 @@ private static void command(String input) { System.out.println(" " + tasksList.get(taskNum).toString()); tasksList.remove(taskNum); UI.printTaskList(tasksList.size()); + updateFile(); break; case "bye": UI.showByeMessage(); - try { - Storage.writeToFile(filePath, ""); - for (Task task_i : tasksList){ - Storage.appendToFile(filePath, task_i.textToSave() + System.lineSeparator()); - } - } catch (IOException err) { - System.out.println("Something went wrong: " + err.getMessage()); - } + updateFile(); break; default: //never reached - UI.printMessage("added: " + input); - Task u = new Task(input, ""); - tasksList.add(u); + UI.printMessage("Never reached"); + } + } + + private static void updateFile() { + try { + Storage.writeToFile(""); + for (Task task : tasksList) { + Storage.appendToFile(task.textToSave() + System.lineSeparator()); + } + } catch (IOException err) { + System.out.println("Something went wrong: " + err.getMessage()); } } private static void printList() throws EmptyListError { - if (tasksList.isEmpty()){ + if (tasksList.isEmpty()) { throw new EmptyListError(); } UI.printMessage("Here are the tasks in your list:"); diff --git a/src/main/java/duke/Task.java b/src/main/java/duke/Task.java index 03b21f847..d169abb66 100644 --- a/src/main/java/duke/Task.java +++ b/src/main/java/duke/Task.java @@ -13,22 +13,23 @@ public Task(String description, String taskType) { this.isDone = false; } - public void markAsDone(){ + public void markAsDone() { isDone = true; } - public void markAsUndone(){ + public void markAsUndone() { isDone = false; } public String getStatusIcon() { return (isDone ? "X" : " "); // mark done task with X } + public String toString() { return "[" + getStatusIcon() + "] " + description; } - public String textToSave(){ + public String textToSave() { return ""; } } diff --git a/src/main/java/duke/UI.java b/src/main/java/duke/UI.java index 5087f782a..34d2a0954 100644 --- a/src/main/java/duke/UI.java +++ b/src/main/java/duke/UI.java @@ -1,43 +1,44 @@ package duke; public class UI { - public static final String HORIZONTAL_LINE = "____________________________________________________________"; + private static final String HORIZONTAL_LINE = "____________________________________________________________"; - public static void printTodo(String message){ + public static void printTodo(String message) { System.out.println("Got it. I've added this task:"); System.out.println(" [T][ ] " + message); } - public static void printDeadline(String taskName, String by){ + public static void printDeadline(String taskName, String by) { System.out.println("Got it. I've added this task:"); System.out.println(" [D][ ] " + taskName + " (by: " + by + ")"); } - public static void printEvent(String taskName, String start, String end){ + public static void printEvent(String taskName, String start, String end) { System.out.println("Got it. I've added this task:"); System.out.println(" [E][ ] " + taskName + " (from: " + start + " to: " + end + ")"); } - public static void printTaskList(int taskListLength){ + public static void printTaskList(int taskListLength) { System.out.println("Now you have " + taskListLength + " tasks in the list."); } - public static void printMessage(String message){ + + public static void printMessage(String message) { System.out.println(message); } - public static void showWelcomeMessage(){ + public static void showWelcomeMessage() { printPicture(0); System.out.println(HORIZONTAL_LINE); System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); System.out.println(HORIZONTAL_LINE); } - public static void showByeMessage(){ + public static void showByeMessage() { System.out.println("Bye. Hope to see you again soon!"); } - public static void printPicture(int index){ - switch(index){ + private static void printPicture(int index) { + switch (index) { case 1: System.out.println(" ___\n" + " _/ ..\\\n" + diff --git a/src/main/java/duke/exception/DukeException.java b/src/main/java/duke/exception/DukeException.java index 34b77fbbd..e77b40f18 100644 --- a/src/main/java/duke/exception/DukeException.java +++ b/src/main/java/duke/exception/DukeException.java @@ -4,15 +4,14 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.List; -public class DukeException extends Exception { - public static boolean hasError(String input){ - try{ +public class DukeException extends Exception { + public static boolean hasError(String input) { + try { catchError(input); - } catch (UnknownInputFieldError e) { + } catch (UnknownInputFieldError err) { return true; - } catch (EmptyTaskDescription e){ + } catch (EmptyTaskDescription err) { return true; } return false; @@ -20,17 +19,16 @@ public static boolean hasError(String input){ private static void catchError(String input) throws UnknownInputFieldError, EmptyTaskDescription { String[] inputWords = input.split(" "); - String[] taskTypes = {"todo", "deadline", "event", "list", "mark", "unmark", "delete", "bye"}; - ArrayList taskTypeList = new ArrayList<>(Arrays.asList(taskTypes)); - if (!taskTypeList.contains(inputWords[0])) { - + String command = inputWords[0]; + String[] commandArray = {"todo", "deadline", "event", "list", "mark", "unmark", "delete", "bye"}; + ArrayList commandList = new ArrayList<>(Arrays.asList(commandArray)); + if (!commandList.contains(command)) { UI.printMessage("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); throw new UnknownInputFieldError(); - } else if (inputWords[0].equals("list") | inputWords[0].equals("bye")) { - } else if(inputWords.length == 1){ - UI.printMessage("☹ OOPS!!! The description of a " + inputWords[0] + " cannot be empty"); + } else if (command.equals("list") | command.equals("bye")) { + } else if (inputWords.length == 1) { + UI.printMessage("☹ OOPS!!! The description of a " + command + " cannot be empty"); throw new EmptyTaskDescription(); } } - } \ No newline at end of file diff --git a/src/main/java/duke/exception/EmptyListError.java b/src/main/java/duke/exception/EmptyListError.java index 64f72c51a..85d64f1ae 100644 --- a/src/main/java/duke/exception/EmptyListError.java +++ b/src/main/java/duke/exception/EmptyListError.java @@ -1,3 +1,4 @@ package duke.exception; -public class EmptyListError extends Exception{ + +public class EmptyListError extends Exception { } diff --git a/src/main/java/duke/exception/EmptyTaskDescription.java b/src/main/java/duke/exception/EmptyTaskDescription.java index e133d2e32..80ef3b72d 100644 --- a/src/main/java/duke/exception/EmptyTaskDescription.java +++ b/src/main/java/duke/exception/EmptyTaskDescription.java @@ -1,4 +1,4 @@ package duke.exception; -public class EmptyTaskDescription extends Exception{ +public class EmptyTaskDescription extends Exception { } diff --git a/src/main/java/duke/exception/UnknownInputFieldError.java b/src/main/java/duke/exception/UnknownInputFieldError.java index 61f3eaf30..cd9ff29e2 100644 --- a/src/main/java/duke/exception/UnknownInputFieldError.java +++ b/src/main/java/duke/exception/UnknownInputFieldError.java @@ -1,4 +1,4 @@ package duke.exception; -public class UnknownInputFieldError extends Exception{ +public class UnknownInputFieldError extends Exception { } diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index cd1e74842..136100efe 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -1,9 +1,11 @@ package duke.storage; + import duke.Task; import duke.UI; import duke.task.Deadline; import duke.task.Event; import duke.task.Todo; + import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; @@ -11,35 +13,38 @@ import java.util.ArrayList; import java.util.List; import java.util.Scanner; + public class Storage { - public static List convertToArray(String filePath) throws FileNotFoundException { - List taskList = new ArrayList<>(); + private static final String dirPath = "src/main/java/duke/storage"; + private static final String filePath = "src/main/java/duke/storage/duke.txt"; - File f = new File(filePath); // create a File for the given file path - Scanner s = new Scanner(f); // create a Scanner using the File as the source + public static List convertToList() throws FileNotFoundException { + List taskList = new ArrayList<>(); + File file = new File(filePath); // create a File for the given file path + Scanner s = new Scanner(file); // create a Scanner using the File as the source while (s.hasNext()) { String textLine = s.nextLine(); String[] textLineArray = textLine.split(" "); switch (textLineArray[0]) { case "T": String taskName = textLine.substring(8); - Todo t = new Todo(taskName, "T"); + Todo todo = new Todo(taskName, "T"); if (textLineArray[2].equals("1")) { - t.markAsDone(); + todo.markAsDone(); } - taskList.add(t); + taskList.add(todo); break; case "D": textLine = textLine.substring(8); int indexOfBoundary = textLine.indexOf("|"); taskName = textLine.substring(0, indexOfBoundary - 1); String by = textLine.substring(indexOfBoundary + 2); - Deadline d = new Deadline(taskName, "D", by); + Deadline deadline = new Deadline(taskName, "D", by); if (textLineArray[2].equals("1")) { - d.markAsDone(); + deadline.markAsDone(); } - taskList.add(d); + taskList.add(deadline); break; case "E": textLine = textLine.substring(8); @@ -48,11 +53,11 @@ public static List convertToArray(String filePath) throws FileNotFoundExce taskName = textLine.substring(0, indexOfBoundary - 1); String from = textLine.substring(indexOfBoundary + 2, lastIndexOfBoundary - 1); String to = textLine.substring(lastIndexOfBoundary + 2); - Event e = new Event(taskName, "E", from, to); + Event event = new Event(taskName, "E", from, to); if (textLineArray[2].equals("1")) { - e.markAsDone(); + event.markAsDone(); } - taskList.add(e); + taskList.add(event); break; default: UI.printMessage("finish converting"); @@ -61,16 +66,27 @@ public static List convertToArray(String filePath) throws FileNotFoundExce return taskList; } - public static void writeToFile(String filePath, String textToAdd) throws IOException { + public static void writeToFile(String textToAdd) throws IOException { FileWriter fw = new FileWriter(filePath); fw.write(textToAdd); fw.close(); } - public static void appendToFile(String filePath, String textToAppend) throws IOException { + public static void appendToFile(String textToAppend) throws IOException { FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode fw.write(textToAppend); fw.close(); } + public static void checkFileAccess() throws IOException { + File dir = new File(dirPath); + if (!dir.exists()) { + dir.mkdir(); + } + File f = new File(filePath); + if (!f.exists()) { + f.createNewFile(); + } + } + } \ No newline at end of file diff --git a/src/main/java/duke/storage/duke.txt b/src/main/java/duke/storage/duke.txt index 824bfc25f..16e3208de 100644 --- a/src/main/java/duke/storage/duke.txt +++ b/src/main/java/duke/storage/duke.txt @@ -1,12 +1,4 @@ -T | 0 | read book now +T | 1 | read book D | 0 | return book | June 6th -E | 0 | project meeting | Aug 6th 2-4pm | DEC +E | 0 | project meeting | Aug 6th 2-4pm | Dec T | 1 | join sports club -D | 0 | xxx | yyy -E | 0 | xxx | yyy | zzz -T | 0 | adawd -D | 0 | adsad | asdasd -E | 0 | adsad | asd | asd -T | 0 | x -D | 0 | y | z -E | 0 | x | y | z diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index be53b95c8..0770456cc 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -9,13 +9,14 @@ public Deadline(String description, String taskType, String by) { super(description, taskType); this.by = by; } + @Override public String toString() { return "[D]" + super.toString() + " (by: " + by + ")"; } @Override - public String textToSave(){ - return "D | " + (super.isDone ? 1 : 0) + " | " + super.description + " | " + by; + public String textToSave() { + return "D | " + (super.isDone ? 1 : 0) + " | " + super.description + " | " + by; } } \ No newline at end of file diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 345552dd1..26256a0f3 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -11,13 +11,14 @@ public Event(String description, String taskType, String start, String end) { this.start = start; this.end = end; } + @Override public String toString() { return "[E]" + super.toString() + " (from: " + start + " to: " + end + ")"; } @Override - public String textToSave(){ - return "E | " + (super.isDone ? 1 : 0) + " | " + super.description + " | " + start + " | " + end; + public String textToSave() { + return "E | " + (super.isDone ? 1 : 0) + " | " + super.description + " | " + start + " | " + end; } } \ No newline at end of file diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/task/Todo.java index 70bee084b..a641e6599 100644 --- a/src/main/java/duke/task/Todo.java +++ b/src/main/java/duke/task/Todo.java @@ -5,15 +5,15 @@ public class Todo extends Task { public Todo(String description, String taskType) { super(description, taskType); - } + @Override public String toString() { return "[T]" + super.toString(); } @Override - public String textToSave(){ - return "T | " + (super.isDone ? 1 : 0) + " | " + super.description; + public String textToSave() { + return "T | " + (super.isDone ? 1 : 0) + " | " + super.description; } } \ No newline at end of file