Skip to content

Commit a942198

Browse files
Move data/HelperBot.txt
1 parent 2fab431 commit a942198

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/main/java/helperbot/storage/data/HelperBot.txt renamed to data/HelperBot.txt

File renamed without changes.

docs/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ To add an `Event`:
7777
```
7878
event [task name] \from [date] [time] \to [date] [time]
7979
```
80-
Noted that date should be in the format of `YYYY-MM-DD` and time (which is optional) should be in the format of `hh: mm`.
80+
Noted that date should be in the format of `YYYY-MM-DD` and time (which is optional) should be in the format of `hh:mm`.
8181

8282
<br>
8383

@@ -113,7 +113,7 @@ list
113113

114114
<br>
115115

116-
### 6. Mark a Task as Done/ Not Done
116+
### 6. Mark a `Task` as Done/ Not Done
117117
Mark / Unmark a `Task` with its index; you can check its index using the `list` command.
118118
```
119119
mark [index]
@@ -125,7 +125,7 @@ unmark [index]
125125

126126
<br>
127127

128-
### 7. Update Task
128+
### 7. Update `Task`
129129
To update a task, an user need the index and the type of the task.
130130
* Update description only.
131131
```
@@ -158,7 +158,9 @@ User can close the program by tapping the close button on the top right or enter
158158
bye
159159
```
160160
161-
### Troubleshooting
161+
<br>
162+
163+
## Troubleshooting
162164
If the user enters an invalid command, the HelperBot will output relevant error message to help user
163165
identify the problem.
164166

src/main/java/helperbot/ui/HelperBot.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package helperbot.ui;
22

3+
import java.io.File;
34
import java.io.FileNotFoundException;
45
import java.io.IOException;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
58
import java.util.Scanner;
69

710
import helperbot.command.Command;
@@ -16,6 +19,8 @@
1619
*/
1720
public class HelperBot {
1821

22+
private static final String DATA_DIRECTORY = "data";
23+
private static final String FILE_NAME = "HelperBot.txt";
1924
private final TaskList tasks;
2025
private final Response response;
2126
private final Storage storage;
@@ -31,15 +36,42 @@ public HelperBot(String filePath) {
3136
try {
3237
tasks1 = new TaskList(this.storage.load());
3338
} catch (FileNotFoundException e) {
34-
this.response.getErrorMessage(filePath + " is not found.");
39+
System.err.println(this.response.getErrorMessage(filePath + " is not found."));
3540
tasks1 = new TaskList();
3641
} catch (HelperBotFileException e) {
37-
this.response.getErrorMessage(e.toString());
42+
System.err.println(this.response.getErrorMessage(e.toString()));
3843
tasks1 = new TaskList();
3944
}
4045
this.tasks = tasks1;
4146
}
4247

48+
/**
49+
* Create data/HelperBot.txt if it does not exist.
50+
* @throws IOException Error occurs when creating data/HelperBot.txt
51+
*/
52+
public static void createFileIfNotExist() throws IOException {
53+
Path currentPath = Paths.get(".").toAbsolutePath();
54+
Path dataPath = currentPath.resolve(DATA_DIRECTORY);
55+
Path filePath = dataPath.resolve(FILE_NAME);
56+
57+
File dataDir = dataPath.toFile();
58+
File file = filePath.toFile();
59+
60+
if (!dataDir.exists()) {
61+
boolean success = dataDir.mkdir();
62+
if (!success) {
63+
throw new IOException("Failed to create the data directory: " + dataDir.getAbsolutePath());
64+
}
65+
}
66+
67+
if (!file.exists()) {
68+
boolean success = file.createNewFile();
69+
if (!success) {
70+
throw new IOException("Failed to create the file: " + file.getAbsolutePath());
71+
}
72+
}
73+
}
74+
4375
/**
4476
* Initializes the chat.
4577
*/
@@ -93,14 +125,14 @@ public String greet() {
93125
*/
94126
public void saveToFile() {
95127
try {
128+
HelperBot.createFileIfNotExist();
96129
this.storage.write(this.tasks);
97-
response.getExitMessage();
98130
} catch (IOException e) {
99-
response.getExitErrorMessage(e.toString());
131+
System.err.println(this.response.getExitErrorMessage(e.toString()));
100132
}
101133
}
102134

103135
public static void main(String[] args) {
104-
new HelperBot("./helperbot/storage/data/HelperBot.txt").chat();
136+
new HelperBot("data/HelperBot.txt").chat();
105137
}
106138
}

src/main/java/helperbot/ui/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
*/
1818
public class Main extends Application {
1919

20-
private final HelperBot helperBot = new HelperBot("src/main/java/helperbot/storage/data/HelperBot.txt");
20+
private HelperBot helperBot;
2121

2222
@Override
2323
public void start(Stage stage) {
2424
try {
25+
HelperBot.createFileIfNotExist();
26+
this.helperBot = new HelperBot("data/HelperBot.txt");
2527
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/view/MainWindow.fxml"));
2628
AnchorPane ap = fxmlLoader.load();
2729
Scene scene = new Scene(ap);

0 commit comments

Comments
 (0)