Skip to content

Commit fde74d5

Browse files
weixue123weixue123
authored andcommitted
Change Storage class so that loading and saving will work when ran from JAR file
1 parent 2cdcec4 commit fde74d5

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

src/main/java/duke/Duke.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public class Duke {
1919
* state of the application, and a <code>Storage</code> property, which handles the loading and saving
2020
* of tasks when the application starts and shuts down respectively.
2121
*
22-
* @param fileName Name of text file in directory data/ from which tasks are loaded when the app starts, and to
22+
* @param fileName Name of text file in directory /data/ from which tasks are loaded when the app starts, and to
2323
* which tasks are saved when the app terminates.
24-
* If the text file does not already exist in the directory data/, then the app starts
24+
* If the text file does not already exist in the directory /data/, then the app starts
2525
* with an empty <code>TaskList</code>. When the app terminates, a new text file
2626
* with the input file name will be created, to which existing tasks are saved.
2727
*/

src/main/java/duke/tasks/Storage.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import java.io.FileNotFoundException;
55
import java.io.FileWriter;
66
import java.io.IOException;
7-
import java.nio.file.Files;
8-
import java.nio.file.Path;
7+
import java.net.URLDecoder;
8+
import java.nio.charset.StandardCharsets;
99
import java.time.LocalDateTime;
1010
import java.util.Scanner;
1111

12+
import duke.Launcher;
1213
import duke.ui.Parser;
1314

1415

@@ -22,11 +23,11 @@ public class Storage {
2223
/**
2324
* Initializes a storage/loader with a file path.
2425
*
25-
* @param fileName Name of text file in directory data/ from which tasks are loaded, and to which tasks are saved.
26+
* @param fileName Name of text file in directory /data/ from which tasks are loaded,
27+
* and to which tasks are saved.
2628
*/
2729
public Storage(String fileName) {
28-
assert Files.isDirectory(Path.of("data"));
29-
this.filePath = "data/" + fileName;
30+
this.filePath = this.getSaveFilePath(fileName);
3031
}
3132

3233
/**
@@ -152,4 +153,35 @@ private String convertTaskToSavableString(Task task) {
152153

153154
return taskType + " | " + done + " | " + description + " | " + dateTimeString + "\n";
154155
}
156+
157+
/**
158+
* Builds the path to the text file from which to load data and to which to save data.
159+
*
160+
* @param fileName Name of text file.
161+
* @return Path to the save file.
162+
*/
163+
private String getSaveFilePath(String fileName) {
164+
String appDirectory = URLDecoder.decode(
165+
Launcher.class
166+
.getProtectionDomain()
167+
.getCodeSource()
168+
.getLocation()
169+
.getPath(),
170+
StandardCharsets.UTF_8);
171+
172+
// Obtain the path of the directory containing the save file
173+
String dataDirectory;
174+
if (appDirectory.endsWith(".jar")) {
175+
dataDirectory = new File(appDirectory).getParentFile().getPath() + "/data";
176+
} else {
177+
dataDirectory = "data";
178+
}
179+
180+
if (!new File(dataDirectory).exists()) {
181+
boolean directoryCreated = new File(dataDirectory).mkdir();
182+
assert directoryCreated;
183+
}
184+
185+
return dataDirectory + "/" + fileName;
186+
}
155187
}

src/test/java/duke/tasks/TestStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public TestStorage() {
3131
*/
3232
@Test
3333
public void testLoadEmptyTasks() {
34-
Storage invalidStorage = new Storage("filepath_that_does_not_exist.txt");
34+
Storage invalidStorage = new Storage("file_that_does_not_exist.txt");
3535
TaskList emptyTaskList = invalidStorage.loadTasks();
3636
assertEquals(0, emptyTaskList.getSize());
3737
}

0 commit comments

Comments
 (0)