-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.java
More file actions
117 lines (94 loc) · 3.03 KB
/
Copy pathTask.java
File metadata and controls
117 lines (94 loc) · 3.03 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
114
115
116
117
import java.time.LocalDateTime;
public class Task {
private int id;
private String description;
private String status; // e.g: todo, in-progress, done
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
public Task(int id, String description, String status, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.description = description;
this.status = status;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
public Task(String description) {
this.description = description;
this.status = "todo";
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}
public int getID() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public LocalDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public String toString() {
return "{" + "ID: " + id + "\n" + "Description: " + description + "\n" + "Status: " + status + "\n" + "Created at: " + createdAt + "\n" + "Updated at: " + updatedAt + "}";
}
public static String escape(String text) {
return text.replace("\"", "\\\"");
}
// convert to JSON format
public String toJSON() {
return String.format("{\"id\":%d,\"description\":\"%s\",\"status\":\"%s\",\"createdAt\":\"%s\",\"updatedAt\":\"%s\"}", id, escape(description), status, createdAt.toString(), updatedAt.toString());
}
// convert from JSON format
public static Task fromJSON(String json) {
// eliminate {, }
json = json.trim();
if (json.startsWith("{")) json = json.substring(1);
if (json.endsWith("}")) json = json.substring(0, json.length() - 1);
String[] pairs = json.split(",");
int id = 0;
String description = "";
String status = "";
LocalDateTime createdAt = null;
LocalDateTime updatedAt = null;
for (String pair : pairs) {
// split \"id\":%d, into 2, like this \"id\" and %d
String[] keyValue = pair.split(":", 2);
// eliminate \", \"id\" becomes id
String key = keyValue[0].replace("\"", "").trim();
String value = keyValue[1].replace("\"", "").trim();
if (key.equals("id")) {
id = Integer.parseInt(value);
} else if (key.equals("description")) {
description = value;
} else if (key.equals("status")) {
status = value;
} else if (key.equals("createdAt")) {
createdAt = LocalDateTime.parse(value);
} else if (key.equals("updatedAt")) {
updatedAt = LocalDateTime.parse(value);
}
}
return new Task(id, description, status, createdAt, updatedAt);
}
}