-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathStorage.java
More file actions
113 lines (106 loc) · 4.19 KB
/
Storage.java
File metadata and controls
113 lines (106 loc) · 4.19 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
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
import duke.exception.RepeatedCompletionException;
import duke.task.Task;
import duke.task.TaskBank;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Storage {
private String filePath;
private File file;
Storage(String filePath, TaskBank tb, Ui ui) {
this.filePath = filePath;
file = new File(filePath);
if (file.exists()) {
loadTasks(tb);
ui.showLoadMessage();
} else {
File directoryPath = new File(filePath);
directoryPath.mkdir();
try {
file.createNewFile();
} catch(IOException e){
System.out.println(e.getMessage());
}
ui.showGreeting();
}
}
/** Exports the content in the TaskBank to duke.txt
*
* @param tb - the TaskBank which tasks are exported from
*/
public void exportTasks(TaskBank tb) {
StringBuffer taskTextString = new StringBuffer();
for (Task task : tb.getTasks()) {
taskTextString.append(task.getTaskType());
taskTextString.append(" | ");
taskTextString.append(task.getDone() ? "1" : "0");
taskTextString.append(" | ");
taskTextString.append(task.describeInFile());
taskTextString.append("\r\n");
}
try {
FileWriter fw = new FileWriter(filePath);
fw.write(taskTextString.toString());
fw.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/** Loads taks from file to target TaskBank
*
* @param tb TaskBank which tasks are loaded to
*/
public void loadTasks(TaskBank tb) {
File f = new File(this.filePath);
try (Scanner sc = new Scanner(f)) {
while (sc.hasNext()) {
loadTaskLine(sc.nextLine(), tb);
}
} catch (FileNotFoundException e) {
System.out.println("duke.txt is not found");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void loadTaskLine(String taskLine, TaskBank tb) throws IOException {
String taskTypeString = taskLine.substring(0, 1);
int firstDivisor = taskLine.indexOf("|", 1);
int secondDivisor = taskLine.indexOf("|", firstDivisor + 1);
int thirdDivisor = taskLine.indexOf("|", secondDivisor + 1);
if (thirdDivisor == -1) { // todo type
tb.addTodo(taskLine.substring(secondDivisor + 1).trim());
} else {
if (taskTypeString.equals("D")) {
String deadLineInput = taskLine.substring(secondDivisor + 1).trim();
String taskCompletionStatus = taskLine.substring(firstDivisor + 2, secondDivisor).trim();
Task newTask = tb.addDeadline(deadLineInput.replace("| ", "/"));
if (taskCompletionStatus.equals("1")) {
try {
newTask.markAsDone();
} catch (RepeatedCompletionException e) {
// This is left blank intentinally
// as from tasks are generated from
// local files, and will not be completed repeatedly
}
}
} else if (taskTypeString.equals("E")) {
String eventLineInput = taskLine.substring(secondDivisor + 1).trim();
String taskCompletionStatus = taskLine.substring(firstDivisor + 2, secondDivisor).trim();
Task newTask = tb.addEvent(eventLineInput.replace("| ", "/"));
if (taskCompletionStatus.equals("1")) {
try {
newTask.markAsDone();
} catch (RepeatedCompletionException e) {
// This is left blank intentinally
// as from tasks are generated from
// local files, and will not be completed repeatedly
}
}
} else {
throw new IOException("Wrong type from data loader");
}
}
}
}