Skip to content

Commit fd15136

Browse files
committed
Level-7: updated saving and loading such that user may specify an address to save to and load from
1 parent fc92a48 commit fd15136

File tree

9 files changed

+126
-12
lines changed

9 files changed

+126
-12
lines changed

dukeLog.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
0,todo hbw
2-
1,deadline work /by9/9/2022
1+
0,todo run

resources/dukeLog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0,todo run

src/main/java/command/ByeCommand.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package command;
22

33
import exception.DukeException;
4+
import exception.DukeFileAddressInvalidException;
45
import main.Storage;
56
import main.TaskList;
67
import main.Ui;
@@ -12,10 +13,16 @@
1213
*/
1314

1415
public class ByeCommand extends Command{
16+
17+
private String logFileAddress = "";
1518

1619
public ByeCommand() {
1720
}
1821

22+
public ByeCommand(String newAddress) {
23+
this.logFileAddress = newAddress;
24+
}
25+
1926

2027
/**
2128
* Checks if command will cause chatbot to end.
@@ -36,13 +43,31 @@ public boolean isEnd() {
3643
* @throws DukeException
3744
*/
3845
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException{
46+
if (!this.verifyAddress(this.logFileAddress)) {
47+
throw new DukeFileAddressInvalidException("User file address invalid, please check pathing");
48+
}
3949
try {
40-
storage.cleanUp();
50+
if (this.logFileAddress.equals("")) {
51+
storage.cleanUp();
52+
} else {
53+
storage.cleanUp(this.logFileAddress);
54+
}
4155
} catch (DukeException e) {
4256
throw e;
4357
}
4458
}
4559

60+
private boolean verifyAddress(String address) {
61+
String addressToCheck = address.strip();
62+
String[] addressSplit = addressToCheck.split(" ");
63+
if (addressSplit.length > 1) {
64+
return false;
65+
}
66+
if (address.equals("")) {
67+
return false;
68+
}
69+
return true;
70+
}
4671

4772
/**
4873
* Returns the task that will be generated from the command, returns an empty task if no task is to be generated

src/main/java/command/LoadCommand.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package command;
22

3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
37
import exception.DukeException;
8+
import exception.DukeFileAddressInvalidException;
49
import main.Storage;
510
import main.TaskList;
611
import main.Ui;
712
import task.Task;
813

914
public class LoadCommand extends Command{
15+
16+
private String logFileAddress = "";
1017

1118
public LoadCommand() {
1219
super();
1320
}
1421

15-
22+
public LoadCommand(String newAddress) {
23+
this.logFileAddress = newAddress;
24+
}
25+
1626
/**
1727
* Checks if command will cause chatbot to end
1828
* @return boolean
@@ -33,12 +43,15 @@ public boolean isEnd() {
3343
*/
3444
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException{
3545
try {
36-
storage.loadLog();
46+
if (this.logFileAddress.equals("")) {
47+
storage.loadLog();
48+
} else {
49+
storage.loadLog(this.logFileAddress);
50+
}
3751
} catch (DukeException e) {
3852
throw e;
3953
}
4054
}
41-
4255

4356
/**
4457
* Returns the task that will be generated from the command, returns an empty task if no task is to be generated
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package exception;
2+
3+
public class DukeFileAddressInvalidException extends DukeException{
4+
5+
public DukeFileAddressInvalidException(String message) {
6+
super(message);
7+
}
8+
}

src/main/java/main/Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Command parse(String userCommand) throws InvalidCommandException {
5555
return new FindCommand(commandArgs);
5656
case COMMAND_LOAD:
5757
if (!commandArgs.equals("")) {
58-
throw new InvalidCommandException("Argument given for command not needing argument");
58+
return new LoadCommand(commandArgs);
5959
}
6060
return new LoadCommand();
6161
case COMMAND_LIST:
@@ -65,7 +65,7 @@ public Command parse(String userCommand) throws InvalidCommandException {
6565
return new ListCommand();
6666
case COMMAND_BYE:
6767
if (!commandArgs.equals("")) {
68-
throw new InvalidCommandException("Argument given for command not needing argument");
68+
return new ByeCommand(commandArgs);
6969
}
7070
return new ByeCommand();
7171
case COMMAND_TODO:

src/main/java/main/Storage.java

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
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;
9+
import java.nio.file.Paths;
710
import java.util.ArrayList;
811
import java.util.Scanner;
912

1013
import command.Command;
1114
import exception.DukeException;
15+
import exception.DukeFileAddressInvalidException;
1216
import exception.DukeFileNotFoundException;
1317
import exception.DukeIOException;
1418
import exception.InvalidCommandException;
1519
import task.Task;
1620

1721
public class Storage {
1822

19-
private static String logFileAddress = "./ip/dukeLog.txt";
23+
private final String LOG_FILE_DIRECTORY = "./resources"; //log files must be in resources directory
24+
private String logFileAddress = "./resources/dukeLog.txt"; //default file address
2025

2126
private ArrayList<String[]> loggedTasks = new ArrayList<String[]>();
2227
private TaskList existingTasks;
@@ -29,12 +34,12 @@ public Storage(TaskList existingTasks, Ui ui) {
2934

3035

3136
/**
32-
* loadLog loads the tasks stored in the log file into the curren tasklist,
37+
* loadLog loads the tasks stored in the log file into the current tasklist,
3338
* throws DukeExceptions if theres issues with the format of the log file or file address is invalid
3439
* @return TaskList
3540
* @throws DukeException
3641
*/
37-
public TaskList loadLog() throws DukeException{
42+
public void loadLog() throws DukeException{
3843
try {
3944
//initialise parser, file scanner and tasklist
4045
Parser parser = new Parser();
@@ -52,25 +57,36 @@ public TaskList loadLog() throws DukeException{
5257
fileReader.close();
5358

5459
//add all the tasks in the temporary array list of task logs into the tasklist
60+
int numOfTasks = 0;
5561
for (String[] loggedTask : loggedTasks) {
5662
boolean isDone = Integer.parseInt(loggedTask[0]) == 1;
5763
Command parsedCommand = parser.parse(loggedTask[1]);
5864
Task newTask = parsedCommand.getTask();
65+
numOfTasks += 1;
5966
if (isDone) {
6067
newTask.mark();
6168
}
6269
newTaskList.add(parsedCommand.getTask());
6370
}
6471

6572
//return the new tasklist to be used
66-
return newTaskList;
73+
this.existingTasks.replace(newTaskList);
74+
this.sendLoadMessage(numOfTasks);
6775
} catch (InvalidCommandException e) {
6876
throw e;
6977
} catch (FileNotFoundException e) {
7078
throw new DukeFileNotFoundException(e.getLocalizedMessage());
7179
}
7280
}
7381

82+
public void loadLog(String newPath) throws DukeException{
83+
String fullPath = this.LOG_FILE_DIRECTORY + "/" + newPath;
84+
if (!this.verifyPath(fullPath)) {
85+
throw new DukeFileAddressInvalidException("File path specified does not exist");
86+
}
87+
this.logFileAddress = fullPath;
88+
this.loadLog();
89+
}
7490

7591
/**
7692
* @throws DukeException
@@ -82,6 +98,9 @@ public void cleanUp() throws DukeException {
8298
return;
8399
}
84100
try {
101+
//Verify filepath exists
102+
this.createDirectory();
103+
85104
//initialise file writer and integer counter
86105
FileWriter fileWriter = new FileWriter(logFileAddress);
87106
int numOfTasks = 0;
@@ -101,6 +120,41 @@ public void cleanUp() throws DukeException {
101120
}
102121
}
103122

123+
/**
124+
*
125+
* @param newFileName
126+
* @throws DukeException
127+
*/
128+
public void cleanUp(String newFileName) throws DukeException{
129+
logFileAddress = this.LOG_FILE_DIRECTORY + "/" + newFileName;
130+
this.cleanUp();
131+
}
132+
133+
private boolean verifyPath(String fullPath) {
134+
Path fullPathToCheck = Paths.get(fullPath);
135+
if (Files.exists(fullPathToCheck)) {
136+
return true;
137+
} else {
138+
return false;
139+
}
140+
}
141+
142+
private void createDirectory() {
143+
String directoriesPath = this.removeFilePath(this.logFileAddress);
144+
Path dirPath = Paths.get(directoriesPath);
145+
if (!Files.exists(dirPath)) {
146+
147+
//if path doesnt exist, create directory
148+
File directory = new File(directoriesPath);
149+
if (!directory.mkdirs()) {
150+
this.ui.logFileError();
151+
}
152+
}
153+
}
154+
155+
private String removeFilePath(String fullPath) {
156+
return fullPath.substring(0, fullPath.lastIndexOf("/") + 1);
157+
}
104158

105159
/**
106160
* @param numOfTasks
@@ -113,4 +167,8 @@ public void sendNoTasksMessage() {
113167
this.ui.chat("No tasks saved to log file \n");
114168
}
115169

170+
public void sendLoadMessage(int numOfTasks) {
171+
this.ui.chat(String.format("Loaded %d tasks", numOfTasks));
172+
}
173+
116174
}

src/main/java/main/TaskList.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public TaskList search(String keyword) {
126126
return this.searchTasks;
127127
}
128128

129+
public void replace(TaskList newList) {
130+
this.tasks = new ArrayList<Task>();
131+
this.tasks.addAll(newList.tasks);
132+
}
133+
129134
/**
130135
* Returns the list command format of the tasks in index order
131136
* @return String

src/main/java/main/Ui.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Ui {
1111
private final String UI_LINE_SPACING = "----------------------------------------";
1212
private final String MESSAGE_GREETING = "Hello! I'm Duke \n" + "What can I do for you?";
1313
private final String MESSAGE_FIND = "Tasks found:\n";
14+
private final String MESSAGE_LOG_ERROR = "File pathing for log file is facing issues, tasks not saved\n";
1415
private final String markMessage = "Task has been marked done:";
1516
private final String unmarkMessage = "Task has been marked not done:";
1617
private final String addMessage = "Task added: \n";
@@ -23,6 +24,10 @@ public Ui(TaskList tasks) {
2324
public void greeting() {
2425
this.chat(this.MESSAGE_GREETING);
2526
}
27+
28+
public void logFileError() {
29+
this.chat(MESSAGE_LOG_ERROR);
30+
}
2631

2732
/**
2833
* Shows a list of the tasks the chatbot holds to the user in tasklist index order,

0 commit comments

Comments
 (0)