diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 891cd42006..e4824573c8 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -6,6 +6,10 @@ import duke.tasks.Task; import duke.tasks.Todo; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -23,9 +27,20 @@ public class Duke { // a array to store user input task private static List tasks = new ArrayList<>(); + + private static String filePath = "data" + File.separator + "duke.txt"; public static void main(String[] args) { displayWelcomeMessage(); + + // for debugging + // System.out.println(java.nio.file.Paths.get("").toAbsolutePath().toString()); + + try { + loadFileContents(filePath); + } catch (FileNotFoundException e) { + System.out.println("File not found"); + } Scanner in = new Scanner(System.in); String input = null; @@ -42,10 +57,13 @@ public static void main(String[] args) { case DONE_COMMAND: try { markAsDone(Integer.parseInt(split[1].trim())); + updateTasksToFile(filePath, tasks); } catch (ArrayIndexOutOfBoundsException e) { displayEmptyDescriptionMessage(command); } catch (NumberFormatException e) { displayInvalidTaskNumberMessage(); + } catch (IOException e) { + displayIOExceptionMessage(e); } catch (ChatboxException e) { displayInvalidTaskNumberMessage(); } @@ -64,26 +82,35 @@ public static void main(String[] args) { case TODO_COMMAND: try { addTodo(split[1]); + updateTasksToFile(filePath, tasks); } catch (ArrayIndexOutOfBoundsException e) { displayEmptyDescriptionMessage(command); + } catch (IOException e) { + displayIOExceptionMessage(e); } break; case DEADLINE_COMMAND: try { addDeadline(split[1]); + updateTasksToFile(filePath, tasks); } catch (ArrayIndexOutOfBoundsException e) { displayEmptyDescriptionMessage(command); } catch (ChatboxException e) { displayTimeMissingMessage(); + } catch (IOException e) { + displayIOExceptionMessage(e); } break; case EVENT_COMMAND: try { addEvent(split[1]); + updateTasksToFile(filePath, tasks); } catch (ArrayIndexOutOfBoundsException e) { displayEmptyDescriptionMessage(command); } catch (ChatboxException e) { displayTimeMissingMessage(); + } catch (IOException e) { + displayIOExceptionMessage(e); } break; case BYE_COMMAND: @@ -95,7 +122,7 @@ public static void main(String[] args) { } } while (!input.equals(BYE_COMMAND)); } - + private static void displayWelcomeMessage() { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" @@ -197,8 +224,67 @@ private static void displayCommandNotFoundMessage() { private static void displayEmptyDescriptionMessage(String command) { System.out.println(String.format("OOPS!!! The description of a %s cannot be empty.", command)); } - + private static void displayTimeMissingMessage() { System.out.println("Oops! Time is missing!"); } + + private static void displayIOExceptionMessage(IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + + private static void loadFileContents(String filePath) throws FileNotFoundException { + 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 + System.out.println("Tasks on the list: "); + if (!s.hasNext()) { + System.out.println("Empty list"); + } + + while (s.hasNext()) { + String taskLine = s.nextLine(); + System.out.println(taskLine); + saveStringtoTask(taskLine); + } + } + + private static void updateTasksToFile(String filePath, List tasks) throws IOException { + FileWriter fw = new FileWriter(filePath); + for (Task task: tasks) { + fw.write(task.getFileString() + System.lineSeparator()); + } + fw.close(); + } + + private static void saveStringtoTask(String fileLine) { + String[] arrays = fileLine.split("\\|"); + String type = arrays[0].trim(); + String isDone = arrays[1].trim(); + String description = arrays[2].trim(); + switch (type) { + case "T": + Task todo = new Todo(description); + if (isDone == "1") { + todo.markAsDone(); + } + tasks.add(todo); + break; + case "D": + String by = arrays[3].trim(); + Task deadline = new Deadline(description, by); + if (isDone == "1") { + deadline.markAsDone(); + } + tasks.add(deadline); + break; + case "E": + String at = arrays[3].trim(); + Task event = new Event(description, at); + if (isDone == "1") { + event.markAsDone(); + } + tasks.add(event); + break; + } + } } diff --git a/src/main/java/duke/tasks/Deadline.java b/src/main/java/duke/tasks/Deadline.java index e4384245de..6477a7402e 100644 --- a/src/main/java/duke/tasks/Deadline.java +++ b/src/main/java/duke/tasks/Deadline.java @@ -6,10 +6,16 @@ public class Deadline extends Task { public Deadline(String description, String by) { super(description); this.by = by; + this.type = "D"; } @Override public String toString() { - return String.format("[D]%s (by: %s)", super.toString(), by); + return String.format("[%s]%s (by: %s)", this.type, super.toString(), by); + } + + @Override + public String getFileString() { + return String.format("%s | %d | %s | %s", type, isDone ? 1 : 0, description, by); } } diff --git a/src/main/java/duke/tasks/Event.java b/src/main/java/duke/tasks/Event.java index 0405258e2f..d9963db09d 100644 --- a/src/main/java/duke/tasks/Event.java +++ b/src/main/java/duke/tasks/Event.java @@ -6,10 +6,16 @@ public class Event extends Task { public Event(String description, String at) { super(description); this.at = at; + this.type = "E"; } @Override public String toString() { - return String.format("[E]%s (at: %s)", super.toString(), at); + return String.format("[%s]%s (at: %s)", this.type, super.toString(), at); + } + + @Override + public String getFileString() { + return String.format("%s | %d | %s | %s", type, isDone ? 1 : 0, description, at); } } diff --git a/src/main/java/duke/tasks/Task.java b/src/main/java/duke/tasks/Task.java index 7e83ff368f..e9ac31f2c8 100644 --- a/src/main/java/duke/tasks/Task.java +++ b/src/main/java/duke/tasks/Task.java @@ -3,15 +3,16 @@ import static duke.utils.Constants.YES_ICON; import static duke.utils.Constants.NO_ICON; -public class Task { +public abstract class Task { protected String description; protected boolean isDone; + protected String type; // to be assigned in subclasses public Task(String description) { this.description = description; this.isDone = false; } - + public String getStatusIcon() { return (isDone ? YES_ICON : NO_ICON); // return tick or X symbols } @@ -20,7 +21,17 @@ public String toString() { return String.format("[%s] %s", getStatusIcon(), description); } + public boolean isDone() { + return this.isDone; + } + public void markAsDone() { isDone = true; } + + public String getType() { + return type; + } + + public abstract String getFileString(); } diff --git a/src/main/java/duke/tasks/Todo.java b/src/main/java/duke/tasks/Todo.java index f5b91f05f2..3416dddd29 100644 --- a/src/main/java/duke/tasks/Todo.java +++ b/src/main/java/duke/tasks/Todo.java @@ -4,10 +4,16 @@ public class Todo extends Task { public Todo(String description) { super(description); + this.type = "T"; } @Override public String toString() { - return String.format("[T]%s", super.toString()); + return String.format("[%s]%s", this.type, super.toString()); + } + + @Override + public String getFileString() { + return String.format("%s | %d | %s", type, isDone ? 1 : 0, description); } }