diff --git a/data/tasks.txt b/data/tasks.txt index e69de29bb2..54d5374a43 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -0,0 +1,2 @@ +T | 1 | daf +T | 1 | 2rd \ No newline at end of file diff --git a/src/main/java/duke/Parser.java b/src/main/java/duke/Parser.java index 553d4c0789..bff49c360a 100644 --- a/src/main/java/duke/Parser.java +++ b/src/main/java/duke/Parser.java @@ -29,22 +29,13 @@ public String parse() { } else if (command.startsWith("mark")) { return this.taskList.markTask(Integer.valueOf(command.split(" ")[1]) - 1); } else if (command.startsWith("todo")) { - if (command.split(" ", 2).length == 1) { - throw new DukeException(" OOPS!!! The description of a todo cannot be empty."); - } - ToDo newToDo = new ToDo(command.split(" ", 2)[1]); + ToDo newToDo = createToDoFromCommand(); return this.taskList.addTask(newToDo); } else if (command.startsWith("deadline")) { - LocalDate deadline = LocalDate.parse(command.split(" /by ", 2)[1]); - String name = command.split(" /by ", 2)[0].split(" ", 2)[1]; - Deadline newDeadline = new Deadline(name, deadline); + Deadline newDeadline = createDeadlineFromCommand(); return this.taskList.addTask(newDeadline); } else if (command.startsWith("event")) { - LocalDate startTime = LocalDate.parse(command.split(" /from ", 2)[1] - .split(" /to ", 2)[0]); - LocalDate endTime = LocalDate.parse(command.split(" /to ", 2)[1]); - String name = command.split(" /from ", 2)[0].split(" ", 2)[1]; - Event newEvent = new Event(name, startTime, endTime); + Event newEvent = createEventFromCommand(); return this.taskList.addTask(newEvent); } else if (command.startsWith("delete")) { return this.taskList.deleteTask(Integer.valueOf(command.split(" ")[1]) - 1); @@ -63,6 +54,45 @@ public String parse() { return "Invalid command."; } + /** + * Creates the new Event from the command. + * + * @return The new Event. + */ + private Event createEventFromCommand() { + LocalDate startTime = LocalDate.parse(command.split(" /from ", 2)[1] + .split(" /to ", 2)[0]); + LocalDate endTime = LocalDate.parse(command.split(" /to ", 2)[1]); + String name = command.split(" /from ", 2)[0].split(" ", 2)[1]; + Event newEvent = new Event(name, startTime, endTime); + return newEvent; + } + + /** + * Creates the new Deadline from the command. + * + * @return The new Deadline. + */ + private Deadline createDeadlineFromCommand() { + LocalDate deadline = LocalDate.parse(command.split(" /by ", 2)[1]); + String name = command.split(" /by ", 2)[0].split(" ", 2)[1]; + Deadline newDeadline = new Deadline(name, deadline); + return newDeadline; + } + + /** + * Creates the new ToDo from the command. + * + * @return The new ToDo. + */ + private ToDo createToDoFromCommand() { + if (command.split(" ", 2).length == 1) { + throw new DukeException(" OOPS!!! The description of a todo cannot be empty."); + } + ToDo newToDo = new ToDo(command.split(" ", 2)[1]); + return newToDo; + } + /** * Returns if the parser is ended. */ diff --git a/src/main/java/duke/Storage.java b/src/main/java/duke/Storage.java index 91f72bb4d0..6c3ecfb3bd 100644 --- a/src/main/java/duke/Storage.java +++ b/src/main/java/duke/Storage.java @@ -1,6 +1,7 @@ package duke; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; @@ -22,23 +23,46 @@ public Storage(String filePath) { } /** - * Loads the task list with file. Initializes the file - * when necessary. + * Loads the task list with file. * * @return The task list. * @throws IOException If unable to gain input from file. */ public ArrayList load() throws IOException { - File dir = new File("./data"); + String dirPath = this.filePath.split("/")[0]; + File f = getFile(dirPath); + ArrayList tasks = loadTasks(f); + return tasks; + } + + /** + * Returns the file for storage. Initializes the file + * when necessary. + * + * @param path Path of the directory of the storage file. + * @throws IOException If an input or output exception occurs. + */ + private File getFile(String path) throws IOException { + File dir = new File(path); if (!dir.exists()) { dir.mkdir(); } File f = new File(filePath); f.createNewFile(); + return f; + } + + /** + * Returns the task list after loading. + * + * @param f The file for storage. + * @throws FileNotFoundException If an attempt to open the file fails. + */ + private ArrayList loadTasks(File f) throws FileNotFoundException { Scanner s = new Scanner(f); ArrayList tasks = new ArrayList<>(); while (s.hasNext()) { - tasks.add(inputToTask(s.nextLine())); + tasks.add(storageToTask(s.nextLine())); } return tasks; } @@ -49,25 +73,53 @@ public ArrayList load() throws IOException { * @param input The String input. * @return The corresponding Task. */ - private Task inputToTask(String input) { + private Task storageToTask(String input) throws DukeException { String taskType = input.split(" \\| ")[0]; boolean isComplete = input.split(" \\| ")[1].equals("1"); String description = input.split(" \\| ")[2]; if (taskType.equals("T")) { return new ToDo(description, isComplete); } else if (taskType.equals("D")) { - LocalDate d = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); - return new Deadline(description, isComplete, d); + return this.createDeadlineFromStorage(input, description, isComplete); + } else if (taskType.equals("E")) { + return this.createEventFromStorage(input, description, isComplete); } else { - LocalDate start = LocalDate.parse(input.split(" \\| ")[3], - DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); - LocalDate end = LocalDate.parse(input.split(" \\| ")[4], - DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); - return new Event(description, isComplete, start, end); + throw new DukeException("Unknown task type."); } } + /** + * Creates the Event from the storage input. + * + * @param input The input line. + * @param description The description of the Event. + * @param isComplete Whether the Event is completed. + * @return The Event. + */ + private Event createEventFromStorage(String input, String description, boolean isComplete) { + LocalDate start = LocalDate.parse(input.split(" \\| ")[3], + DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); + LocalDate end = LocalDate.parse(input.split(" \\| ")[4], + DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); + return new Event(description, isComplete, start, end); + } + + /** + * Creates the Deadline from the storage input. + * + * @param input The input line. + * @param description The description of the Deadline. + * @param isComplete Whether the Deadline is completed. + * @return The Event. + */ + private Deadline createDeadlineFromStorage(String input, String description, boolean isComplete) { + LocalDate d = LocalDate.parse(input.split(" \\| ")[3], + DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); + return new Deadline(description, isComplete, d); + } + + /** * Adds the last task in the task list to file. * @@ -97,10 +149,12 @@ public void rewriteFile(ArrayList list) throws DukeException { try { FileWriter fw = new FileWriter(filePath); for (int i = 0; i < list.size(); i++) { - fw.write(list.get(i).toTxt()); if (i != list.size() - 1) { + fw.write(list.get(i).toTxt()); fw.write("\n"); + continue; } + fw.write(list.get(i).toTxt()); } fw.close(); } catch (IOException e) { diff --git a/src/main/java/duke/TaskList.java b/src/main/java/duke/TaskList.java index 5657d1bc8d..d462fe42b4 100644 --- a/src/main/java/duke/TaskList.java +++ b/src/main/java/duke/TaskList.java @@ -74,9 +74,10 @@ public String markTask(int num) { public String findTask(String keyword) { ArrayList result = new ArrayList<>(); for (int i = 0; i < this.tasks.size(); i++) { - if (tasks.get(i).toString().contains(keyword)) { - result.add(tasks.get(i)); + if (!tasks.get(i).toString().contains(keyword)) { + continue; } + result.add(tasks.get(i)); } return Ui.listMatchingTasks(result); }