forked from nus-cs2103-AY2122S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.java
138 lines (128 loc) · 4.23 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package duke.helptool;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;
import duke.tag.Tag;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.Task;
import duke.task.ToDo;
/**
* The type Storage.
*/
public class Storage {
private final String filePath;
/**
* Instantiates a new Storage.
*
* @param filePath the file path
*/
public Storage(String filePath) {
this.filePath = filePath;
}
/**
* Create file.
*
* @throws DukeException the duke exception
*/
public void create() throws DukeException {
try {
File myObj = new File(this.filePath);
boolean isCreated = myObj.createNewFile();
} catch (IOException e) {
throw new DukeException("IO exception occurred.");
}
}
/**
* Write to file.
*
* @param taskList the task list
* @throws DukeException the duke exception
*/
public void write(TaskList taskList) throws DukeException {
try {
FileWriter myWriter = new FileWriter(filePath, false);
for (int i = 0; i < taskList.getSize(); i++) {
String output = String.format("%s\n", taskList.getTask(i).toString());
myWriter.write(output);
}
myWriter.close();
} catch (IOException e) {
throw new DukeException("IO exception occurred when write to file.");
}
}
/**
* Gets tag.
*
* @param data the data
* @return the tag
*/
public Tag getTag(String data) {
int tagPos = data.indexOf("#{");
int tagEnd = data.indexOf("}");
String tagDescription = data.substring(tagPos + 3, tagEnd);
return new Tag(tagDescription);
}
/**
* Load from file.
*
* @return the array list
* @throws DukeException the duke exception
*/
public ArrayList<Task> load() throws DukeException {
try {
File myObj = new File(this.filePath);
Scanner myReader = new Scanner(myObj);
ArrayList<Task> tempTaskList = new ArrayList<>();
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
String tempType = data.substring(1, 2);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy hh:mm a");
switch (tempType) {
case "T":
Tag tag = getTag(data);
Task tempTask = new ToDo(data.substring(7), tag);
if (data.charAt(4) == 'X') {
tempTask.markAsDone();
}
tempTaskList.add(tempTask);
break;
case "D":
int byPos = data.indexOf("(by:");
String by = data.substring(byPos + 5, data.length() - 1);
String description = data.substring(7, byPos - 1);
tag = getTag(data);
tempTask = new Deadline(description, LocalDateTime.parse(by, formatter), tag);
if (data.charAt(4) == 'X') {
tempTask.markAsDone();
}
tempTaskList.add(tempTask);
break;
case "E":
int atPos = data.indexOf("(at:");
String at = data.substring(atPos + 5, data.length() - 1);
description = data.substring(7, atPos - 1);
tag = getTag(data);
tempTask = new Event(description, LocalDateTime.parse(at, formatter), tag);
if (data.charAt(4) == 'X') {
tempTask.markAsDone();
}
tempTaskList.add(tempTask);
break;
default:
break;
}
}
myReader.close();
return tempTaskList;
} catch (FileNotFoundException e) {
this.create();
return null;
}
}
}