-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathStorage.java
More file actions
66 lines (54 loc) · 1.92 KB
/
Storage.java
File metadata and controls
66 lines (54 loc) · 1.92 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
package duke.data;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.TaskManager;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Storage {
public static void load(String filePath) throws IOException {
File newFile = new File(filePath);
Scanner scanner = new Scanner(newFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
String[] array = line.split(" | ");
switch (array[0]) {
case "T":
TaskManager.loadToDoFromFile(array[2]);
break;
case "D":
TaskManager.loadDeadlineFromFile(array[2], array[3]);
break;
case "E":
TaskManager.loadEventFromFile(array[2], array[3]);
break;
}
}
}
public static void appendToFile(String filePath, String textToAppend) throws IOException {
FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode
fw.write(textToAppend);
fw.close();
}
public static void writeToFile(String filePath) throws IOException {
File file = new File(filePath);
if (file.createNewFile()) {
System.out.println("File created");
}
String textToAppend;
for (Task task: TaskManager.taskList) {
String taskType = task.getIcon();
String status = task.getStatus();
String description = task.getDescription();
String timing = task.getTime();
textToAppend = taskType + " | " + status + " | " + description;
if (task instanceof Event || task instanceof Deadline) {
textToAppend += " | " + timing;
}
textToAppend += "\n";
appendToFile(filePath, textToAppend);
}
}
}