Skip to content

Commit 532ecb4

Browse files
committed
Remove Path Dependency
1 parent 8805fef commit 532ecb4

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/main/java/duke/Duke.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package duke;
22

3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URLDecoder;
5+
36
public class Duke {
47
private Storage taskStorage;
58
private Storage contactStorage;
@@ -9,8 +12,8 @@ public class Duke {
912
private ContactList contactList;
1013

1114
public Duke() {
12-
this.taskStorage = new Storage("C:/Users/Jeremias/Documents/GitHub/ip/data/", "task.txt");
13-
this.contactStorage = new Storage("C:/Users/Jeremias/Documents/GitHub/ip/data/", "contact.txt");
15+
this.taskStorage = new Storage( "task.txt");
16+
this.contactStorage = new Storage("contact.txt");
1417
this.ui = new Ui();
1518
this.parser = new Parser();
1619
this.tasks = taskStorage.loadTaskList();
@@ -115,4 +118,15 @@ public String getResponse(String input) {
115118
return ui.getError(e);
116119
}
117120
}
121+
122+
public static String getPath() {
123+
String path = Duke.class.getProtectionDomain().getCodeSource().getLocation().getPath();
124+
String decodedPath = null;
125+
try {
126+
decodedPath = URLDecoder.decode(path, "UTF-8");
127+
} catch (UnsupportedEncodingException e) {
128+
e.printStackTrace();
129+
}
130+
return decodedPath;
131+
}
118132
}

src/main/java/duke/Storage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.File;
55
import java.io.FileWriter;
66

7+
import java.io.UnsupportedEncodingException;
78
import java.time.LocalDateTime;
89
import java.time.format.DateTimeFormatter;
910

@@ -13,8 +14,8 @@ public class Storage {
1314
private final String filePath;
1415
private final String fileName;
1516

16-
public Storage(String filePath, String fileName) {
17-
this.filePath = filePath;
17+
public Storage(String fileName) {
18+
this.filePath = Duke.getPath();
1819
this.fileName = fileName;
1920
}
2021

0 commit comments

Comments
 (0)