Skip to content

Commit

Permalink
Fix documentation clarity and create storage file on launch
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanosync committed Sep 18, 2019
1 parent d21947c commit 30a007d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
40 changes: 38 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,43 @@ You can filter out important tasks by searching for their name as a keyword.

### Load and save tasks

Doke saves your tasks to: `data/tasks.txt`. For advanced users, you may modify it by inferring from the given format. Corrupt files will be discarded by the application and re-generated on launch.
Doke saves your tasks to: `data/tasks.txt`. Corrupt files will be discarded by the application and re-generated on launch.

The format of each task type is defined below.

---

Deadline Format:
```
D|[Done: true/false]|Description|[By date]
```

Event Format:
```
E|[Done: true/false]|Description|[At date]
```

Todo Format:
```
T|[Done: true/false]|Description
```

---

For example, given a save file with the following content:
```
D|false|complete The Project|20/9/2019 1800
E|false|Attend the funfair|25/12/2019 1200
T|true|homework
```

The following output is returned:
```
Here are the tasks in your list:
1. [D][ ] complete The Project (by: Fri Sep 20 18:00:00 SGT 2019)
2. [E][ ] Attend the funfair (at: Wed Dec 25 12:00:00 SGT 2019)
3. [T][x] homework
```

## Usage

Expand Down Expand Up @@ -197,7 +233,7 @@ Task number `1`, labelled `homework` from the task list is marked as done, as in

#### `find` - Find a task

Finds a task with given keywords.
Finds a task with given keywords (case-sensitive).

**Example of usage:**

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import duke.command.Command;
import duke.exception.DukeException;
import duke.exception.IoDukeException;
import duke.parser.CommandParser;
import duke.storage.Storage;
import duke.task.TaskList;
Expand Down Expand Up @@ -31,6 +32,12 @@ public Duke(String filePath) {
} catch (DukeException e) {
ui.showLoadingError();
tasks = new TaskList();

try {
storage.createStorageFile();
} catch (IoDukeException ioException) {
// Suppress the error, as we only want to create the file.
}
}
}

Expand Down
45 changes: 38 additions & 7 deletions src/main/java/duke/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,14 @@ public List<Task> load() throws DukeException {
/**
* Saves a list of tasks to the file path in the constructor object.
*
* @param tasks List of tasks
* @throws DukeException If the file failed to save.
*/
public void save(TaskList tasks) throws DukeException {
PrintWriter writer = null;
try {
Path path = Paths.get(filePath);
writer = getStorageFile();

// Create directories if necessary
if (path.getParent() != null) {
Files.createDirectories(path.getParent().getFileName());
}

writer = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
for (Task task : tasks.getTaskList()) {
String output = StorageSerializer.serialize(task);
writer.write(output);
Expand All @@ -88,4 +83,40 @@ public void save(TaskList tasks) throws DukeException {
}
}
}

/**
* Gets the file handle for save storage.
* The handle must be closed after use.
*
* @return A file handle for the storage file.
* @throws IOException If an IO error occurred.
*/
private PrintWriter getStorageFile() throws IOException {
Path path = Paths.get(filePath);

// Create directories if necessary
if (path.getParent() != null) {
Files.createDirectories(path.getParent().getFileName());
}

return new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
}

/**
* Creates a storage file if it does not exist.
*
* @throws IoDukeException If an IO error occurred.
*/
public void createStorageFile() throws IoDukeException {
PrintWriter writer = null;
try {
writer = getStorageFile();
} catch (IOException e) {
throw new IoDukeException("Error opening task file for reading");
} finally {
if (writer != null) {
writer.close();
}
}
}
}

0 comments on commit 30a007d

Please sign in to comment.