-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManager.java
More file actions
68 lines (57 loc) · 1.75 KB
/
Copy pathTaskManager.java
File metadata and controls
68 lines (57 loc) · 1.75 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
import java.io.*;
import java.time.LocalDate;
import java.util.*;
public class TaskManager {
private List<Task> tasks = new ArrayList<>();
private final String FILE_NAME = "tasks.txt";
public TaskManager() {
loadTasks();
}
public void addTask(int id, String name, int priority, String deadlineStr) {
LocalDate deadline = LocalDate.parse(deadlineStr);
Task task = new Task(id, name, priority, deadline);
tasks.add(task);
saveTasks();
}
public void deleteTask(int id) {
tasks.removeIf(t -> t.getTaskId() == id);
saveTasks();
}
public Task searchTask(int id) {
for (Task t : tasks) {
if (t.getTaskId() == id) return t;
}
return null;
}
public List<Task> getAllTasks() {
return tasks;
}
public void updateStatus(int id, String status) {
Task t = searchTask(id);
if (t != null) {
t.setStatus(status);
saveTasks();
}
}
public void saveTasks() {
try (PrintWriter pw = new PrintWriter(new FileWriter(FILE_NAME))) {
for (Task t : tasks) {
pw.println(t.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadTasks() {
File file = new File(FILE_NAME);
if (!file.exists()) return;
try (BufferedReader br = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = br.readLine()) != null) {
tasks.add(Task.fromString(line));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}