-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathStorage.java
More file actions
111 lines (103 loc) · 4.2 KB
/
Storage.java
File metadata and controls
111 lines (103 loc) · 4.2 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
package ssagit.storage;
import ssagit.datevalidator.DateValidator;
import ssagit.taskclass.DeadlineTask;
import ssagit.taskclass.EventTask;
import ssagit.taskclass.Task;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class Storage {
Path relativePath;
Path absolutePath;
File taskText;
/**
* Constructor, creates a file of the searched file if it doesn't exist.
* Afterwards, create a {@code FileWriter} which is used to write to the file.
* @param relativePath relative path of file to be searched for.
*/
public Storage (String relativePath) {
this.relativePath = Paths.get(relativePath);
absolutePath = this.relativePath.toAbsolutePath();
taskText = new File(absolutePath.toString());
try {
if (!taskText.exists()) {
System.out.println("new file created");
taskText.createNewFile(); // creates the file if it doesn't exist
}
} catch (IOException e) {
System.out.println("IOException has occurred");
e.printStackTrace();
}
}
/**
* Reads a file into the program and parses each line into a Task and puts it into taskArr.
* @param taskArr array of Task objects.
* @return number of tasks currently available in list (1 index).
*/
public int readTaskListToArray(Task[] taskArr, DateValidator validator) {
int taskIterator = 0;
try {
List<String> list = Files.readAllLines(Paths.get(relativePath.toString()), Charset.defaultCharset());
String[] taskListStr = list.toArray(new String[list.size()]);
for (String str : taskListStr) {
String[] strArr = str.split(" \\| ");
String taskType = strArr[0];
String isDoneStr = strArr[1];
String taskName = strArr[2];
if (taskType.equals("todo")) {
taskArr[taskIterator] = new Task(taskName, isDoneStr.equals("done"));
} else if (taskType.equals("event")) {
if (validator.isValid(strArr[3].trim())) {
Date eventDate = new SimpleDateFormat("d/MM/yyyy HHmm").parse(strArr[3].trim());
taskArr[taskIterator] = new EventTask(taskName, isDoneStr.equals("done"), strArr[3].trim(), eventDate);
} else {
System.out.println("Invalid date format for timed Task");
}
} else if (taskType.equals("deadline")) {
if (validator.isValid(strArr[3].trim())) {
Date deadlineDate = new SimpleDateFormat("d/MM/yyyy HHmm").parse(strArr[3].trim());
taskArr[taskIterator] = new DeadlineTask(taskName, isDoneStr.equals("done"), strArr[3].trim(), deadlineDate);
} else {
System.out.println("Invalid date format for timed Task");
}
}
taskIterator++;
}
return taskIterator;
} catch (IOException e) {
System.out.println("IOException has occurred");
e.printStackTrace();
} catch (ParseException e) {
System.out.println("ParseException has occurred");
e.printStackTrace();
}
return taskIterator;
}
/**
* Writes all tasks from an array of {@code Task} objects to file output.
* @param taskArr Array of {@code Task} objects.
*/
public void writeTasks(Task[] taskArr) {
try {
FileWriter fw = new FileWriter(absolutePath.toString());
for (Task t : taskArr) {
if (t != null) {
fw.write(t.toOutputFileString() + "\n");
fw.flush();
}
}
fw.close();
} catch (IOException e) {
System.out.println("IOException has occurred");
e.printStackTrace();
}
}
}