-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathStorage.java
More file actions
136 lines (125 loc) · 4.42 KB
/
Storage.java
File metadata and controls
136 lines (125 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package duke;
import duke.exception.DukeException;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.ToDo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Deals with loading tasks from the file and saving tasks in the file.
*/
public class Storage {
private String filePath;
private String dirPath;
/**
* @param filePath The file path of the file which is used to store task list.
*/
public Storage(String filePath) {
this.filePath = filePath;
parseFilePath();
}
/**
* Extracts directory path from the file path.
*/
public void parseFilePath() {
String pattern = "(.*)/(.*)$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(filePath);
if (m.find()) {
this.dirPath = m.group(1);
}
}
/**
* Loads task list from the file and returns the task list.
* @return The task list which is loaded from the file.
* @throws DukeException Exception if there is error when loading from the file.
*/
public ArrayList<Task> load() throws DukeException {
// create the folder called data if not found
ArrayList<Task> tasks = new ArrayList<>();
File dir = new File(dirPath);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdir();
}
File file = new File(filePath);
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
tasks.add(parseTask(sc.nextLine()));
}
sc.close();
return tasks;
} catch (FileNotFoundException e) {
throw new DukeException("Cannot load from file!");
}
}
/**
* Parses the task string to a duke.task.Task object.
* @param str The string which represents a single task.
* @return A duke.task.Task object which represents a task as the string.
* @throws DukeException Exception if there is error when parsing the task string.
*/
public Task parseTask(String str) throws DukeException {
try {
char typeLabel = str.charAt(1);
boolean isDone = str.charAt(4) == 'X';
String body = str.substring(7);
String pattern;
Pattern r;
Matcher m;
switch (typeLabel) {
case 'T':
return new ToDo(body, isDone);
case 'D':
pattern = "(.*) \\(by: (.*)\\)";
r = Pattern.compile(pattern);
m = r.matcher(body);
if (m.find()) {
LocalDate deadline = LocalDate.parse(m.group(2), DateTimeFormatter.ofPattern("MMM dd yyyy"));
return new Deadline(m.group(1), isDone, deadline);
}
case 'E':
pattern = "(.*) \\(at: (.*)\\)";
r = Pattern.compile(pattern);
m = r.matcher(body);
if (m.find()) {
LocalDate time = LocalDate.parse(m.group(2), DateTimeFormatter.ofPattern("MMM dd yyyy"));
return new Event(m.group(1), isDone, time);
}
default:
throw new Exception();
}
} catch (Exception e) {
throw new DukeException("error occurs and loading from file is suspended!");
}
}
/**
* Saves the task list to the file.
* @param tasks The TaskList object whose string representation will be saved to the file.
* @throws DukeException Exception when there is an error when saving task list to the file.
*/
public void save(TaskList tasks) throws DukeException {
// make the data directory if not found
File dir = new File(dirPath);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdir();
}
try {
FileWriter writer = new FileWriter(filePath);
writer.write(tasks.toString());
writer.flush();
writer.close();
} catch (IOException e) {
throw new DukeException("Save to file error!");
}
}
}