From 71f339a78a878cedab305e8b941ea9707b8e7ff8 Mon Sep 17 00:00:00 2001 From: Mario Lorenzo Date: Thu, 13 Feb 2020 20:49:00 +0800 Subject: [PATCH] Add auto create storage feature --- docs/README.md | 5 ++- src/main/java/Driver.java | 2 +- src/main/java/Duke.java | 83 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 7 deletions(-) diff --git a/docs/README.md b/docs/README.md index 7d2cb88692..99b43071ce 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,8 +25,6 @@ GUI. ## Requirements - Duke runs in Java 11 or later. - The `.jar` file of Duke can be found [here](https://github.com/mario7lorenzo/duke/releases) -- Make sure you have created `./data` directory, and the directory path is relative to the `.jar` -file, with three files: `archive.txt`, `notes.txt`, and `tasks.txt` inside the `./data` directory. ## Features ### Interactive GUI @@ -120,6 +118,7 @@ regarding to archives are listed below: - `archive-list`
Displays all the archived tasks. - `archive-add [task-index]`
Moves the task of a given index in the list to the archive. - `archive-delete [archived-task-index]`
Deletes the task permanently from the archive. +- `unarchive [archived-task-index]`
Unarchives the task from the archive. Example of usage: - `archive-list`
@@ -128,6 +127,8 @@ Duke will list all the archived tasks. Duke will move the first task in the current list to the archive. - `archive-delete 1`
Duke will delete the first task in the archived task list from the archive. +- `unarchive 1`
+Duke will unarchive the first task in the archived list. ###Notes Duke can memorize random notes from the user. The commands regarding to notes are listed below: diff --git a/src/main/java/Driver.java b/src/main/java/Driver.java index bb1916ef3a..faf8d170eb 100644 --- a/src/main/java/Driver.java +++ b/src/main/java/Driver.java @@ -29,7 +29,7 @@ */ public class Driver extends Application { - private final String STAGE_TITLE = "Duke"; + private static final String STAGE_TITLE = "Duke"; private static final String BYE_STRING = "bye"; /** * The main method runs the program. diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 7fec0112ff..a2bf2e09f7 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -11,6 +11,8 @@ import duke.util.Task; import duke.util.TaskList; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; /* @@ -35,10 +37,13 @@ public class Duke { private ArchiveList archiveList; private Storage taskStorage; private Storage archiveStorage; - private Storage storage; private NoteStorage noteStorage; private NoteList noteList; private Parser parser; + private static final String DATA_DIRECTORY = "./data"; + private static final String TASK_DIRECTORY = "./data/tasks.txt"; + private static final String ARCHIVE_DIRECTORY = "./data/archive.txt"; + private static final String NOTES_DIRECTORY = "./data/notes.txt"; /** * Constructs the Duke instance that has a list that @@ -68,9 +73,10 @@ private Duke(TaskList taskList, ArchiveList archiveList, */ public static Duke start() throws DukeInvalidTaskFormatException, DukeInvalidDateFormatException { - Storage taskStorage = new Storage("./data/tasks.txt"); - Storage archiveStorage = new Storage("./data/archive.txt"); - NoteStorage noteStorage = new NoteStorage("./data/notes.txt"); + setStorage(); + Storage taskStorage = new Storage(TASK_DIRECTORY); + Storage archiveStorage = new Storage(ARCHIVE_DIRECTORY); + NoteStorage noteStorage = new NoteStorage(NOTES_DIRECTORY); ArrayList tasks = new ArrayList<>(); ArrayList archives = new ArrayList<>(); ArrayList notes = new ArrayList<>(); @@ -115,4 +121,73 @@ public String processCommand(String commands) { return exc.getMessage(); } } + + /** + * Sets up the storage for Duke to read and write the contents. + */ + + private static void setStorage() { + createDataDirectory(); + createTaskFile(); + createArchiveFile(); + createNoteFile(); + } + /** + * Creates the data directory if it is not exist yet. + */ + + private static void createDataDirectory() { + File file = new File(DATA_DIRECTORY); + if (!file.exists()) { + file.mkdir(); + } + } + + /** + * Creates the tasks.txt file inside the data directory if not exist yet. + */ + + private static void createTaskFile() { + File file = new File(TASK_DIRECTORY); + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + /** + * Creates the archive.txt file inside the data directory if not exist yet. + */ + + private static void createArchiveFile() { + File file = new File(ARCHIVE_DIRECTORY); + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + /** + * Creates the notes.txt file inside the data directory if not exist yet. + */ + + private static void createNoteFile() { + File file = new File(NOTES_DIRECTORY); + if (!file.exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } } \ No newline at end of file