-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonWriter.java
More file actions
58 lines (53 loc) · 1.9 KB
/
JsonWriter.java
File metadata and controls
58 lines (53 loc) · 1.9 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
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.util.*;
public class JsonWriter{
private int startDate = -1;
private int endDate = -1;
public JsonWriter(int startDate, int endDate){
this.startDate = startDate;
this.endDate = endDate;
}
public JsonWriter(){
}
public void write(ArrayList<Task> taskList, String filename){
JSONObject json = makeJson(taskList);
try (PrintWriter out = new PrintWriter(new FileWriter(filename))) {
out.write(json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
private JSONObject makeJson(ArrayList<Task> taskList){
JSONObject json = new JSONObject();
String taskName = "";
String type = "";
int date = 0;
for(Task task : taskList){
date = task.getDate();
if((date<startDate || date>endDate) && !(startDate == -1 || endDate == -1)){
continue;
}
JSONObject taskJson = new JSONObject();
taskName = task.getName();
taskJson.put("Date", Integer.toString(date));
type = task.getType().toString();
taskJson.put("Type", type);
taskJson.put("Start_Time", task.getStartTime() + "");
taskJson.put("Duration", task.getDuration() + "");
if(task.hasAntiTask()){
taskJson.put("Has_Anti-Task", "True");
}
else{
taskJson.put("Has_Anti-Task", "False");
}
if(type.equals("RECURRING")){
taskJson.put("End_Date", Integer.toString(task.getEndDate()));
taskJson.put("Frequency", Integer.toString(task.getFrequency()));
}
json.put(taskName, taskJson);
}
return json;
}
}