forked from nus-cs2103-AY2223S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.java
96 lines (89 loc) · 3.1 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
package duke;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Loads data if any and saves user input in the same file.
*/
public class Storage {
private static final String BREAK_LINE = " ____________________________________________________________";
private String path;
/**
* Storage Constructor
* @param path file path where the .txt file is stored
*/
public Storage(String path) {
this.path = path;
}
/**
* Loads data in a .txt file into an ArrayList of tasks
* @return ArrayList of Task stored in a previously saved file if any or a new ArrayList.
* @throws DukeException If the type format in the .txt file is invalid.
*/
public ArrayList<Task> load() throws DukeException {
ArrayList<Task> tasks = new ArrayList<>();
try {
File taskFile = new File(this.path);
Scanner sc = new Scanner(taskFile);
while (sc.hasNext()) {
String input = sc.nextLine();
String[] inputArr = input.split(" \\| ");
String type = inputArr[0];
Task task;
switch (type) {
case "T":
task = new Todo(inputArr[2]);
tasks.add(task);
if (inputArr[1].equals("1")) {
task.markAsDone();
}
break;
case "D":
task = new Deadline(inputArr[2], inputArr[3]);
tasks.add(task);
if (inputArr[1].equals("1")) {
task.markAsDone();
}
break;
case "E":
task = new Event(inputArr[2], inputArr[3]);
tasks.add(task);
if (inputArr[1].equals("1")) {
task.markAsDone();
}
break;
default:
throw new DukeException("It is an invalid type!");
}
}
sc.close();
} catch (FileNotFoundException err) {
printError(err);
} catch (DukeException err) {
printError(err);
}
return tasks;
}
/**
* Saves an ArrayList of Task in a .txt file
* @param tasks TaskList object consists of a list of task inputted by user
*/
public void save(TaskList tasks) {
try {
FileWriter fw = new FileWriter(this.path);
for(int i = 0; i < tasks.getSize(); i++) {
fw.write(tasks.getTask(i).toSave());
}
fw.close();
} catch (IOException err) {
printError(err);
}
}
private static void printError(Exception err) {
System.out.println(BREAK_LINE + "\n" + " ☹ OOPS!!! "
+ err + "\n" + BREAK_LINE + "\n");
}
}