Skip to content

Commit

Permalink
Add ability for the user to change the target save file
Browse files Browse the repository at this point in the history
Add two new commands: showpath and setpath.
  • Loading branch information
szelongq committed Sep 9, 2021
1 parent f9c1232 commit b3b4ba9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bin/
/text-ui-test/ACTUAL.txt
text-ui-test/EXPECTED-UNIX.TXT
data/duke.txt
*.txt
3 changes: 2 additions & 1 deletion src/main/java/duke/CommandType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke;

public enum CommandType {
EXIT, LIST, ADD_TASK, COMPLETE_TASK, DELETE_TASK, FIND_TASK
EXIT, LIST, ADD_TASK, COMPLETE_TASK, DELETE_TASK, FIND_TASK,
SHOW_PATH, SET_PATH,
}
28 changes: 22 additions & 6 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ public Duke() {
*/
public Duke(String filepath) {
ui = new Ui();
storage = new Storage(filepath);

try {
tasks = new TaskList(storage.load());
if (tasks.getSize() > 0) {
ui.printLoadTasks(tasks);
}
setStoragePath(filepath);
} catch (DukeException e) {
ui.printErrorMessage(e.getMessage());
tasks = new TaskList();
}
}
Expand Down Expand Up @@ -93,6 +88,10 @@ private String executeCommand(String userInput) throws DukeException {
return deleteTask(Parser.parseTaskNum(userInput));
case FIND_TASK:
return findTask(Parser.parseSearchSubject(userInput));
case SHOW_PATH:
return ui.printCurrentStoragePath(storage);
case SET_PATH:
return setNewStoragePath(Parser.parseNewPath(userInput));
default:
throw new UnsupportedOperationException(); // Error
}
Expand Down Expand Up @@ -126,4 +125,21 @@ private String deleteTask(int taskNum) {
private String findTask(String subject) {
return ui.printTasksWithSubject(tasks, subject);
}

private void setStoragePath(String newPath) throws LoadingException {
Storage newStorage = new Storage(newPath);
TaskList newTasks = new TaskList(newStorage.load());

storage = newStorage;
tasks = newTasks;
}

private String setNewStoragePath(String newPath) {
try {
setStoragePath(newPath);
return ui.printNewStoragePath(storage, tasks);
} catch (DukeException e) {
return ui.printErrorMessage(e.getMessage());
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static CommandType parseCommandType(String userInput)
return CommandType.DELETE_TASK;
case "find":
return CommandType.FIND_TASK;
case "showpath":
return CommandType.SHOW_PATH;
case "setpath":
return CommandType.SET_PATH;
default:
throw new UnsupportedOperationException();
}
Expand Down Expand Up @@ -111,4 +115,10 @@ public static String parseSearchSubject(String userInput) {
userInputScanner.next();
return userInputScanner.nextLine().trim();
}

public static String parseNewPath(String userInput) {
Scanner userInputScanner = new Scanner(userInput);
userInputScanner.next();
return userInputScanner.nextLine().trim();
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ public void saveTasks(TaskList tasks) {
ex.printStackTrace();
}
}

public String getPath() {
return savePath.toString();
}
}
9 changes: 9 additions & 0 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,13 @@ public String printLoadTasks(TaskList tasks) {
public String printErrorMessage(String message) {
return "Error: " + message + ".";
}

public String printCurrentStoragePath(Storage storage) {
return "Current path is:\n" + storage.getPath();
}

public String printNewStoragePath(Storage storage, TaskList tasks) {
return "New path set:\n" + storage.getPath() + "\n\n"
+ printLoadTasks(tasks);
}
}

0 comments on commit b3b4ba9

Please sign in to comment.