-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.java
120 lines (105 loc) · 3.73 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
package linqing.tojava;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Storage tasks e class that will help to load Tasks from the hard disk and save tasks to the hard disk
*/
public class Storage {
public static String path;
public Storage(String path) {
this.path = path;
}
private static List<String> getLines(String path) {// base on:https://www.mkyong
// .com/java/how-to-read-file-from-java-bufferedreader-example/
//Declaration
List<String> lines = new ArrayList<>();
System.out.println(path);
String line;
BufferedReader br;
FileReader fr;
File file;
try {
file = new File(path);
fr = new FileReader(file);
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch ( IOException e ) {
System.out.println(e.getMessage());
}
return lines;
}
private static Task createTask(String line) {
Task task = null;
String type = line.split("\\|")[ 0 ];
boolean isDone = line.split("\\|")[ 1 ].equals("1") ? true : false;
String description = line.split("\\|")[ 2 ];
switch (type) {
case "T":
task = new ToDo(description);
task.setDone(isDone);
break;
case "D":
String by = line.split("\\|")[ 3 ];
task = new Deadline(description, by);
task.setDone(isDone);
break;
default:
Ui.Error();
break;
}
return task;
}
public static List<Task> getTasksFromFile() {
List<Task> loadedTasks = new ArrayList<>();
try {
List<String> lines = getLines("C:/data/tasks.txt");
for (String line : lines) {
if (line.trim().isEmpty()) { //ignore empty lines
continue;
}
loadedTasks.add(createTask(line)); //convert the line to a task and add to the list
}
} catch ( Exception e ) {
System.out.println("problem encountered while loading data: " + e.getMessage());
}
return loadedTasks;
}
/**
* @return tasks from @code getTaskFrom File
* @throws FileNotFoundException if no file is create or found
*/
public List<Task> load() throws FileNotFoundException {
List<Task> tasks = getTasksFromFile();
return tasks;
}
public static void save(List<Task> save) throws IOException {
List<Task> useAdd = new ArrayList<>();
for (int i = 0; i < save.size(); i++) {
useAdd.add(save.get(i));
}
outTask(useAdd);
}
private static String outTask(List<Task> tasks) {
File out = new File(path);
try ( BufferedWriter b = new BufferedWriter(new FileWriter(out)) ) {
for (int i = 0; i < tasks.size(); i++) {
Task t = tasks.get(i);
if (t instanceof Deadline) {
b.append("D");
b.append("|").append(t.isDone() ? "1" : "0").append("|").append(t.getDescription()).append("|").append(((Deadline) t).getDeadline());
} else if (t instanceof ToDo) {
b.append("T");
b.append("|").append(t.isDone() ? "1" : "0").append("|").append(t.getDescription());
}
b.append(System.lineSeparator());
}
return b.toString();
} catch ( IOException e ) {
System.out.println(e.getMessage());
}
return "Empty Tasks" + System.lineSeparator();
}
}