From 8c4e65aa2b060b107145dc1efa2218afb3b687e9 Mon Sep 17 00:00:00 2001 From: Gavzzz Date: Thu, 22 Sep 2022 02:22:42 +0800 Subject: [PATCH] Removed unnecessary imports --- database/duke.txt | 1 + src/main/java/Duke/Duke.java | 11 ---- src/main/java/Duke/Storage.java | 50 ++++++++++++------- src/main/java/Duke/TaskList.java | 2 +- src/main/java/Duke/commands/ByeCommands.java | 1 - src/main/java/Duke/commands/Command.java | 2 - .../java/Duke/commands/DeadlineCommands.java | 3 -- .../java/Duke/commands/DeleteCommands.java | 3 -- .../commands/DetectDuplicateCommands.java | 2 - .../java/Duke/commands/EventCommands.java | 2 - src/main/java/Duke/commands/Executor.java | 1 - src/main/java/Duke/commands/FindCommands.java | 1 - src/main/java/Duke/commands/ListCommands.java | 2 - src/main/java/Duke/commands/MarkCommands.java | 2 - src/main/java/Duke/commands/TaskCommands.java | 3 -- src/main/java/Duke/commands/TodoCommands.java | 2 - .../java/Duke/commands/UnmarkCommands.java | 2 - src/main/java/Duke/gui/MainWindow.java | 2 - src/main/java/Duke/tasks/Deadline.java | 4 +- src/main/java/Duke/tasks/Event.java | 5 +- src/main/java/Duke/tasks/Task.java | 2 +- src/main/java/Duke/tasks/Todo.java | 4 +- 22 files changed, 41 insertions(+), 66 deletions(-) diff --git a/database/duke.txt b/database/duke.txt index e69de29bb2..162377b751 100644 --- a/database/duke.txt +++ b/database/duke.txt @@ -0,0 +1 @@ +T;borrow book; diff --git a/src/main/java/Duke/Duke.java b/src/main/java/Duke/Duke.java index c53bbc7533..7b5a445891 100644 --- a/src/main/java/Duke/Duke.java +++ b/src/main/java/Duke/Duke.java @@ -28,14 +28,3 @@ public String getResponse(String input) throws DukeException, IOException { return message; } } - - - - - - - - - - - diff --git a/src/main/java/Duke/Storage.java b/src/main/java/Duke/Storage.java index 1b9968cda6..3f1fac61a9 100644 --- a/src/main/java/Duke/Storage.java +++ b/src/main/java/Duke/Storage.java @@ -17,6 +17,7 @@ public class Storage { private final List> tasks; private static final String TASK_FILE_PATH = "database/duke.txt"; + private static final String TASK_FILE_DIR = "data"; private FileWriter fileWriter; private final File taskFile; @@ -25,7 +26,7 @@ public class Storage { * */ - public Storage() throws IOException { + public Storage() throws IOException, DukeException { taskFile = new File(TASK_FILE_PATH); tasks = new ArrayList<>(); addTasks(fileToList()); @@ -69,7 +70,7 @@ public List fileToList() { return list; } - public void updateTask(int taskNumber, Constants command) throws IOException { + public void updateTask(int taskNumber, Constants command) throws IOException, DukeException { switch (command) { case MARK: tasks.get(taskNumber - 1).set(1, "X"); @@ -86,29 +87,30 @@ public void updateTask(int taskNumber, Constants command) throws IOException { updateFile(); } - private void updateFile() throws IOException { - fileWriter = new FileWriter(TASK_FILE_PATH); - for (List task : tasks) { - StringBuilder taskString = new StringBuilder(task.get(0)).append(";"); - for (int i = 1; i < task.size(); i++) { - taskString.append(task.get(i)); - if (i != task.size() - 1) { - taskString.append(";"); - } + private void updateFile() throws DukeException { + try { + File fileDir = new File(TASK_FILE_DIR); + if (!fileDir.isDirectory() && !fileDir.mkdirs()) { + throw new DukeException("I cant save the task bro."); + } + + FileWriter fileWriter = new FileWriter(TASK_FILE_PATH); + for (List task : tasks) { + fileWriter.write(listToCsv(task)); } - fileWriter.write(taskString.append("\n").toString()); - System.out.println("ayo"); + fileWriter.close(); + } catch (IOException e) { + throw new DukeException( + "Cant save the task list bro: " + e); } - for (List task : tasks) - System.out.println(String.join(", ", task)); } - public void addTask(Task addedTask) throws IOException { + public void addTask(Task addedTask) throws IOException, DukeException { tasks.add(addedTask.toList()); updateFile(); } - private void addTasks(List addedTasks) throws IOException { + private void addTasks(List addedTasks) throws IOException, DukeException { for (Task addedTask : addedTasks) { addTask(addedTask); } @@ -120,4 +122,16 @@ public void close() throws IOException { fileWriter.close(); } } -} + + + private String listToCsv(List list) { + StringBuilder csv = new StringBuilder(list.get(0)).append(";"); + for (int i = 1; i < list.size(); i++) { + csv.append(list.get(i)); + if (i != list.size() - 1) { + csv.append(";"); + } + } + return csv.append("\n").toString(); + } +} \ No newline at end of file diff --git a/src/main/java/Duke/TaskList.java b/src/main/java/Duke/TaskList.java index 0bec4d9ffb..975d2f0e30 100644 --- a/src/main/java/Duke/TaskList.java +++ b/src/main/java/Duke/TaskList.java @@ -74,4 +74,4 @@ public TaskList filter(List Keywords) { return result; } -} +} \ No newline at end of file diff --git a/src/main/java/Duke/commands/ByeCommands.java b/src/main/java/Duke/commands/ByeCommands.java index 6c5fc86090..05ad49048b 100644 --- a/src/main/java/Duke/commands/ByeCommands.java +++ b/src/main/java/Duke/commands/ByeCommands.java @@ -4,7 +4,6 @@ import Duke.Storage; import Duke.TaskList; import Duke.Ui; -import java.util.List; import java.io.IOException; public class ByeCommands extends Command { diff --git a/src/main/java/Duke/commands/Command.java b/src/main/java/Duke/commands/Command.java index 99980d80aa..8cdbb61054 100644 --- a/src/main/java/Duke/commands/Command.java +++ b/src/main/java/Duke/commands/Command.java @@ -1,7 +1,5 @@ package Duke.commands; -import java.util.ArrayList; -import java.util.List; import Duke.DukeException; import Duke.Storage; import Duke.TaskList; diff --git a/src/main/java/Duke/commands/DeadlineCommands.java b/src/main/java/Duke/commands/DeadlineCommands.java index ed31eed0a7..ba2d79b1e0 100644 --- a/src/main/java/Duke/commands/DeadlineCommands.java +++ b/src/main/java/Duke/commands/DeadlineCommands.java @@ -1,8 +1,5 @@ package Duke.commands; - -import java.util.ArrayList; -import java.util.List; import Duke.*; import Duke.tasks.Deadline; import Duke.tasks.Task; diff --git a/src/main/java/Duke/commands/DeleteCommands.java b/src/main/java/Duke/commands/DeleteCommands.java index 3bbf682803..a723e57d63 100644 --- a/src/main/java/Duke/commands/DeleteCommands.java +++ b/src/main/java/Duke/commands/DeleteCommands.java @@ -1,8 +1,5 @@ package Duke.commands; - -import java.util.ArrayList; -import java.util.List; import Duke.*; import java.io.IOException; diff --git a/src/main/java/Duke/commands/DetectDuplicateCommands.java b/src/main/java/Duke/commands/DetectDuplicateCommands.java index dddc0e276d..f6310dd871 100644 --- a/src/main/java/Duke/commands/DetectDuplicateCommands.java +++ b/src/main/java/Duke/commands/DetectDuplicateCommands.java @@ -3,8 +3,6 @@ import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import Duke.*; public class DetectDuplicateCommands extends Command { diff --git a/src/main/java/Duke/commands/EventCommands.java b/src/main/java/Duke/commands/EventCommands.java index 35b80cd8a9..1408f87614 100644 --- a/src/main/java/Duke/commands/EventCommands.java +++ b/src/main/java/Duke/commands/EventCommands.java @@ -4,8 +4,6 @@ import Duke.tasks.Event; import Duke.tasks.Task; import java.io.IOException; -import java.util.List; -import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; diff --git a/src/main/java/Duke/commands/Executor.java b/src/main/java/Duke/commands/Executor.java index d39164fff4..609b106a90 100644 --- a/src/main/java/Duke/commands/Executor.java +++ b/src/main/java/Duke/commands/Executor.java @@ -4,7 +4,6 @@ import Duke.Storage; import Duke.TaskList; import Duke.Ui; -import java.util.List; import java.io.IOException; diff --git a/src/main/java/Duke/commands/FindCommands.java b/src/main/java/Duke/commands/FindCommands.java index 4b13e16f15..3e0a820ad7 100644 --- a/src/main/java/Duke/commands/FindCommands.java +++ b/src/main/java/Duke/commands/FindCommands.java @@ -5,7 +5,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.List; public class FindCommands extends Command { diff --git a/src/main/java/Duke/commands/ListCommands.java b/src/main/java/Duke/commands/ListCommands.java index 7d8f3144de..90aef67baf 100644 --- a/src/main/java/Duke/commands/ListCommands.java +++ b/src/main/java/Duke/commands/ListCommands.java @@ -5,8 +5,6 @@ import Duke.Storage; import Duke.TaskList; import Duke.Ui; -import java.util.ArrayList; -import java.util.List; import java.io.IOException; public class ListCommands extends Command { diff --git a/src/main/java/Duke/commands/MarkCommands.java b/src/main/java/Duke/commands/MarkCommands.java index 550cd9611a..e481c21088 100644 --- a/src/main/java/Duke/commands/MarkCommands.java +++ b/src/main/java/Duke/commands/MarkCommands.java @@ -2,8 +2,6 @@ import Duke.*; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; public class MarkCommands extends TaskCommands { diff --git a/src/main/java/Duke/commands/TaskCommands.java b/src/main/java/Duke/commands/TaskCommands.java index 81959e7b6b..01a460f8e3 100644 --- a/src/main/java/Duke/commands/TaskCommands.java +++ b/src/main/java/Duke/commands/TaskCommands.java @@ -3,9 +3,6 @@ import Duke.*; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.List; public abstract class TaskCommands extends Command { diff --git a/src/main/java/Duke/commands/TodoCommands.java b/src/main/java/Duke/commands/TodoCommands.java index 374b4cbdf5..5bb0f7fa8a 100644 --- a/src/main/java/Duke/commands/TodoCommands.java +++ b/src/main/java/Duke/commands/TodoCommands.java @@ -1,7 +1,5 @@ package Duke.commands; -import java.util.ArrayList; -import java.util.List; import Duke.DukeException; import Duke.Storage; import Duke.TaskList; diff --git a/src/main/java/Duke/commands/UnmarkCommands.java b/src/main/java/Duke/commands/UnmarkCommands.java index d2411fb6e5..0665b32d3a 100644 --- a/src/main/java/Duke/commands/UnmarkCommands.java +++ b/src/main/java/Duke/commands/UnmarkCommands.java @@ -1,7 +1,5 @@ package Duke.commands; -import java.util.ArrayList; -import java.util.List; import Duke.*; import java.io.IOException; diff --git a/src/main/java/Duke/gui/MainWindow.java b/src/main/java/Duke/gui/MainWindow.java index 24f85911e6..5ab8e06d00 100644 --- a/src/main/java/Duke/gui/MainWindow.java +++ b/src/main/java/Duke/gui/MainWindow.java @@ -1,8 +1,6 @@ package Duke.gui; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import Duke.*; import Duke.commands.Command; diff --git a/src/main/java/Duke/tasks/Deadline.java b/src/main/java/Duke/tasks/Deadline.java index c381cab2d8..6a9ee6a6b0 100644 --- a/src/main/java/Duke/tasks/Deadline.java +++ b/src/main/java/Duke/tasks/Deadline.java @@ -21,8 +21,8 @@ public String format(LocalDate time) { } @Override - public List toList() { - List product = new ArrayList<>(); + public ArrayList toList() { + ArrayList product = new ArrayList<>(); product.add("D"); product.addAll(super.toList()); product.add(by.toString()); diff --git a/src/main/java/Duke/tasks/Event.java b/src/main/java/Duke/tasks/Event.java index a51bd62ffd..d1516d14f8 100644 --- a/src/main/java/Duke/tasks/Event.java +++ b/src/main/java/Duke/tasks/Event.java @@ -3,7 +3,6 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import java.util.List; public class Event extends Task { @@ -20,8 +19,8 @@ public String format(LocalDate time) { } @Override - public List toList() { - List product = new ArrayList<>(); + public ArrayList toList() { + ArrayList product = new ArrayList<>(); product.add("E"); product.addAll(super.toList()); product.add(at.toString()); diff --git a/src/main/java/Duke/tasks/Task.java b/src/main/java/Duke/tasks/Task.java index c6da1e8d11..aa8f52d209 100644 --- a/src/main/java/Duke/tasks/Task.java +++ b/src/main/java/Duke/tasks/Task.java @@ -45,4 +45,4 @@ public String toString() { return "[" + this.getStatusIcon() + "] " + description; } -} +} \ No newline at end of file diff --git a/src/main/java/Duke/tasks/Todo.java b/src/main/java/Duke/tasks/Todo.java index b417650a76..0d04857a60 100644 --- a/src/main/java/Duke/tasks/Todo.java +++ b/src/main/java/Duke/tasks/Todo.java @@ -10,8 +10,8 @@ public Todo(String description) { } @Override - public List toList() { - List product = new ArrayList<>(); + public ArrayList toList() { + ArrayList product = new ArrayList<>(); product.add("T"); product.addAll(super.toList()); return product;