1111import duke .util .Task ;
1212import duke .util .TaskList ;
1313
14+ import java .io .File ;
15+ import java .io .IOException ;
1416import 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