-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskStorage.java
More file actions
40 lines (36 loc) · 1.44 KB
/
TaskStorage.java
File metadata and controls
40 lines (36 loc) · 1.44 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
import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class TaskStorage {
private static final String FILE_NAME = "tasks.txt";
public static List<Task> loadTasks() {
List<Task> tasks = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" \\| ");
boolean isDone = parts[0].startsWith("[done]");
String description = parts[0].substring(isDone ? 7 : 4);
LocalDate dueDate = LocalDate.parse(parts[1].split(": ")[1]);
Task.Priority priority = Task.Priority.valueOf(parts[2].split(": ")[1]);
Task task = new Task(description, dueDate, priority);
if (isDone) task.markAsDone();
tasks.add(task);
}
} catch (IOException e) {
System.out.println("No existing tasks found.");
}
return tasks;
}
public static void saveTasks(List<Task> tasks) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (Task task : tasks) {
writer.write(task.toString());
writer.newLine();
}
} catch (IOException e) {
System.out.println("Error saving tasks.");
}
}
}