Skip to content

Commit 71f339a

Browse files
committed
Add auto create storage feature
1 parent d05d69d commit 71f339a

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

docs/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ GUI.
2525
## Requirements
2626
- Duke runs in Java 11 or later.
2727
- The `.jar` file of Duke can be found [here](https://github.com/mario7lorenzo/duke/releases)
28-
- Make sure you have created `./data` directory, and the directory path is relative to the `.jar`
29-
file, with three files: `archive.txt`, `notes.txt`, and `tasks.txt` inside the `./data` directory.
3028

3129
## Features
3230
### Interactive GUI
@@ -120,6 +118,7 @@ regarding to archives are listed below:
120118
- `archive-list`<br>Displays all the archived tasks.
121119
- `archive-add [task-index]`<br>Moves the task of a given index in the list to the archive.
122120
- `archive-delete [archived-task-index]`<br>Deletes the task permanently from the archive.
121+
- `unarchive [archived-task-index]`<br>Unarchives the task from the archive.
123122

124123
Example of usage:
125124
- `archive-list`<br>
@@ -128,6 +127,8 @@ Duke will list all the archived tasks.
128127
Duke will move the first task in the current list to the archive.
129128
- `archive-delete 1`<br>
130129
Duke will delete the first task in the archived task list from the archive.
130+
- `unarchive 1`<br>
131+
Duke will unarchive the first task in the archived list.
131132

132133
###Notes
133134
Duke can memorize random notes from the user. The commands regarding to notes are listed below:

src/main/java/Driver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030

3131
public class Driver extends Application {
32-
private final String STAGE_TITLE = "Duke";
32+
private static final String STAGE_TITLE = "Duke";
3333
private static final String BYE_STRING = "bye";
3434
/**
3535
* The main method runs the program.

src/main/java/Duke.java

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import duke.util.Task;
1212
import duke.util.TaskList;
1313

14+
import java.io.File;
15+
import java.io.IOException;
1416
import java.util.ArrayList;
1517

1618
/*
@@ -35,10 +37,13 @@ public class Duke {
3537
private ArchiveList archiveList;
3638
private Storage taskStorage;
3739
private Storage archiveStorage;
38-
private Storage storage;
3940
private NoteStorage noteStorage;
4041
private NoteList noteList;
4142
private Parser parser;
43+
private static final String DATA_DIRECTORY = "./data";
44+
private static final String TASK_DIRECTORY = "./data/tasks.txt";
45+
private static final String ARCHIVE_DIRECTORY = "./data/archive.txt";
46+
private static final String NOTES_DIRECTORY = "./data/notes.txt";
4247

4348
/**
4449
* Constructs the Duke instance that has a list that
@@ -68,9 +73,10 @@ private Duke(TaskList taskList, ArchiveList archiveList,
6873
*/
6974

7075
public static Duke start() throws DukeInvalidTaskFormatException, DukeInvalidDateFormatException {
71-
Storage taskStorage = new Storage("./data/tasks.txt");
72-
Storage archiveStorage = new Storage("./data/archive.txt");
73-
NoteStorage noteStorage = new NoteStorage("./data/notes.txt");
76+
setStorage();
77+
Storage taskStorage = new Storage(TASK_DIRECTORY);
78+
Storage archiveStorage = new Storage(ARCHIVE_DIRECTORY);
79+
NoteStorage noteStorage = new NoteStorage(NOTES_DIRECTORY);
7480
ArrayList<Task> tasks = new ArrayList<>();
7581
ArrayList<Task> archives = new ArrayList<>();
7682
ArrayList<Note> notes = new ArrayList<>();
@@ -115,4 +121,73 @@ public String processCommand(String commands) {
115121
return exc.getMessage();
116122
}
117123
}
124+
125+
/**
126+
* Sets up the storage for Duke to read and write the contents.
127+
*/
128+
129+
private static void setStorage() {
130+
createDataDirectory();
131+
createTaskFile();
132+
createArchiveFile();
133+
createNoteFile();
134+
}
135+
/**
136+
* Creates the data directory if it is not exist yet.
137+
*/
138+
139+
private static void createDataDirectory() {
140+
File file = new File(DATA_DIRECTORY);
141+
if (!file.exists()) {
142+
file.mkdir();
143+
}
144+
}
145+
146+
/**
147+
* Creates the tasks.txt file inside the data directory if not exist yet.
148+
*/
149+
150+
private static void createTaskFile() {
151+
File file = new File(TASK_DIRECTORY);
152+
if (!file.exists()) {
153+
try {
154+
file.createNewFile();
155+
} catch (IOException e) {
156+
e.printStackTrace();
157+
}
158+
159+
}
160+
}
161+
162+
/**
163+
* Creates the archive.txt file inside the data directory if not exist yet.
164+
*/
165+
166+
private static void createArchiveFile() {
167+
File file = new File(ARCHIVE_DIRECTORY);
168+
if (!file.exists()) {
169+
try {
170+
file.createNewFile();
171+
} catch (IOException e) {
172+
e.printStackTrace();
173+
}
174+
175+
}
176+
}
177+
178+
/**
179+
* Creates the notes.txt file inside the data directory if not exist yet.
180+
*/
181+
182+
private static void createNoteFile() {
183+
File file = new File(NOTES_DIRECTORY);
184+
if (!file.exists()) {
185+
try {
186+
file.createNewFile();
187+
} catch (IOException e) {
188+
e.printStackTrace();
189+
}
190+
191+
}
192+
}
118193
}

0 commit comments

Comments
 (0)