forked from nus-cs2103-AY2324S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.java
137 lines (112 loc) · 4.23 KB
/
Storage.java
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
137
package bob;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* A class for saving and loading files from storage.
*/
public class Storage {
private final String filePath;
/*
* A constructor that specifies the storage filepath.
*/
public Storage(String filePath) {
this.filePath = filePath;
}
/**
* A method to load tasks from the specified file path.
*
* @return A TaskList parsed from the save file.
*/
public TaskList loadFile() {
TaskList taskList = new TaskList();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\|");
String taskType = parts[0].trim();
boolean isDone = parts[1].trim().equals("1");
String taskDescription = parts[2].trim();
Task task;
if (taskType.equals("T")) {
task = new ToDo(taskDescription);
}
else if (taskType.equals("D")) {
String deadlineDate = parts[3].trim();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(deadlineDate, formatter);
task = new Deadline(taskDescription, dateTime);
}
else if (taskType.equals("E")) {
String fromDate = parts[3].trim();
String toDate = parts[4].trim();
task = new Event(taskDescription, fromDate, toDate);
}
else {
throw new IllegalStateException("Unexpected value: " + taskType);
}
if (isDone) {
task.markAsDone();
}
taskList.addTask(task);
}
}
catch (FileNotFoundException e) {
File data = new File("data");
data.mkdir();
File tasks = new File(data, "tasks.txt");
try {
tasks.createNewFile();
}
catch (IOException x) {
System.out.println(x.getMessage());
}
}
catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println("File loaded.");
return taskList;
}
/**
* A method to save the current tasks.
*
* @param taskList An ArrayList of the current tasks.
*/
public void saveFile(TaskList taskList) {
try {
File tasks = new File(filePath);
FileWriter writer = new FileWriter(tasks);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
for (Task task : taskList) {
String taskType;
String line;
String isDone = task.getIsDone() ? "1" : "0";
String description = task.getDescription();
if (task instanceof Deadline) {
taskType = "D";
LocalDateTime deadlineDate = ((Deadline) task).getBy();
String deadline = deadlineDate.format(DateTimeFormatter.ofPattern("MMM dd yyyy HH:mm"));
line = String.format("%s | %s | %s | %s", taskType, isDone, description, deadline);
}
else if (task instanceof Event) {
taskType = "E";
String from = ((Event) task).getFrom();
String to = ((Event) task).getTo();
line = String.format("%s | %s | %s | %s | %s", taskType, isDone, description, from, to);
}
else {
taskType = "T";
line = String.format("%s | %s | %s", taskType, isDone, description);
}
bufferedWriter.write(line);
bufferedWriter.newLine();
}
bufferedWriter.close();
System.out.println("File saved.");
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}