diff --git a/data/duke.txt b/data/duke.txt index 20d304df4e..2e3c131ff3 100644 --- a/data/duke.txt +++ b/data/duke.txt @@ -1 +1,2 @@ T|true|1 +T|false|2 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 27ffca547e..ddcb0f7b44 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -176,28 +176,6 @@ private String getResponse(String input) { return ui.getLastMsg(); } - /* - public void run() { - ui.showWelcome(); - boolean isExit = false; - Storage.readTask(tasks, ui); - - while (!isExit) { - try { - String userInput = ui.readInput(); - ui.showLine(); - Command command = Parser.parse(userInput); - isExit = command.execute(this.tasks, ui); - Storage.writeTask(tasks, ui); // Store to file - } catch (DukeException e) { - ui.showError(e.getMessage()); - } finally { - ui.showLine(); - } - } - } - */ - /** * DialogBox. */ diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 63e84a8400..4fee51e418 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -23,60 +23,83 @@ public class Storage { */ public static void readTask(TaskList tasks, Ui ui) { try { - File directory = new File("./data"); - File file = new File(filePath); + File file = checkFileExist(ui); + readFile(tasks, file); + } catch (FileNotFoundException e) { + ui.showError(e.getMessage()); + } catch (DukeException e) { + ui.showError(e.getMessage()); + } + } - if (!directory.exists()) { - directory.mkdir(); - } + /** + * Check if file exist. + * Create new if it does not exist. + * + * @param ui To show error message. + * @return file. + */ + private static File checkFileExist(Ui ui) { + File directory = new File("./data"); + File file = new File(filePath); - if (!file.exists()) { - try { - file.createNewFile(); - } catch (IOException e) { - ui.showError("Error creating new file: " + e.getMessage()); - } - } + if (!directory.exists()) { + directory.mkdir(); + } - Scanner scanner = new Scanner(file); - while (scanner.hasNextLine()) { - Task task; + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + ui.showError("Error creating new file: " + e.getMessage()); + } + } + return file; + } - String line = scanner.nextLine(); - String[] parts = line.split("\\|"); - String taskType = parts[0]; - boolean isDone = parts[1].equals("true"); - String description = parts[2]; + /** + * Read the file and store it in tasklist. + * + * @param tasks List of tasks. + * @param file To read. + * @throws FileNotFoundException When file not found. + * @throws DukeException When failes to save on list. + */ + private static void readFile(TaskList tasks, File file) throws FileNotFoundException, DukeException { + Scanner scanner = new Scanner(file); + while (scanner.hasNextLine()) { + Task task; - switch (taskType) { - case "T": - task = new Todo(description); - break; - case "D": - String date = parts[3]; - task = new Deadline(description, date); - break; - case "E": - String from = parts[3]; - String to = parts[4]; - task = new Event(description, from, to); - break; - default: - continue; - } + String line = scanner.nextLine(); + String[] parts = line.split("\\|"); + String taskType = parts[0]; + boolean isDone = parts[1].equals("true"); + String description = parts[2]; - if (isDone) { - task.mark(); - } + switch (taskType) { + case "T": + task = new Todo(description); + break; + case "D": + String date = parts[3]; + task = new Deadline(description, date); + break; + case "E": + String from = parts[3]; + String to = parts[4]; + task = new Event(description, from, to); + break; + default: + continue; + } - tasks.add(task); + if (isDone) { + task.mark(); } - scanner.close(); - } catch (FileNotFoundException e) { - ui.showError(e.getMessage()); - } catch (DukeException e) { - ui.showError(e.getMessage()); + + tasks.add(task); } + scanner.close(); } diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index cbdcd4fa86..b08b84dfd5 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -37,7 +37,7 @@ public void add(Task task) { * @param index The index of the task to be removed. */ public void remove(int index) { - assert index >= 0 : "index should be >= 0"; + assert index >= 0 : "index should be >= 0"; tasks.remove(index); } @@ -47,7 +47,7 @@ public void remove(int index) { * @param index The index of the task to be marked as done. */ public void mark(int index) { - assert index >= 0 : "index should be >= 0"; + assert index >= 0 : "index should be >= 0"; tasks.get(index).mark(); } @@ -57,7 +57,7 @@ public void mark(int index) { * @param index The index of the task to be unmarked. */ public void unmark(int index) { - assert index >= 0 : "index should be >= 0"; + assert index >= 0 : "index should be >= 0"; tasks.get(index).unmark(); } @@ -107,7 +107,7 @@ public String toString() { * @return all task on the date. * @throws DukeException */ - public String getbyDate(String date) throws DukeException { + public String getByDate(String date) throws DukeException { LocalDate targetDate; try { targetDate = LocalDate.parse(date); diff --git a/src/main/java/duke/command/ListCommand.java b/src/main/java/duke/command/ListCommand.java index b7b77d7667..039ae88320 100644 --- a/src/main/java/duke/command/ListCommand.java +++ b/src/main/java/duke/command/ListCommand.java @@ -21,7 +21,7 @@ public boolean execute(TaskList tasks, Ui ui) throws DukeException { String msg = ui.getLastMsg(); String[] parts = msg.toLowerCase().split("\\s+"); if (parts.length >= 2) { // If the date is provided - ui.respond(tasks.getbyDate(parts[1])); + ui.respond(tasks.getByDate(parts[1])); } else { ui.respond(tasks.toString()); }