-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternship Challenge
277 lines (214 loc) · 7.52 KB
/
Internship Challenge
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package test;
import java.io.FileNotFoundException;
import java.util.*;
import java.text.SimpleDateFormat;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class testing {
public static void main(String[] args){
JSONParser parser = new JSONParser();
try {
JSONArray a = (JSONArray) parser.parse(new FileReader("task.json"));
Scanner input = new Scanner(System.in);
System.out.print("Enter instance ID to count the number of tasks: ");
long instanceId;
instanceId = input.nextLong();
countTasks(a, instanceId);
System.out.print("Enter instance ID to return the name of the most recent task with matching ID: ");
Long matchId = input.nextLong();
findTask(a, matchId);
System.out.print("Enter a date in the form YYYY-MM-DDTHH:mm:ssZ" +
" to return the number of open and closed tasks that day: ");
String inDate = input.next();
findOpenClose(a, inDate);
System.out.print("Enter a start date in the form YYYY-MM-DDTHH:mm:ssZ: ");
String start = input.next();
System.out.print("Enter an end date in the form YYYY-MM-DDTHH:mm:ssZ: ");
String end = input.next();
allOpenClosedInRange(a, start, end);
input.close();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//This method counts the number of tasks given an instance ID
//pre:valid JSONArray and instanceId entered
//post: returns an int representing number of tasks
//param: JSONArray generated from valid document, long ID that user inputs
public static int countTasks(JSONArray a, long instanceId){
int count = 0;
for (Object o: a){
JSONObject jsonObject = (JSONObject) o;
Long id = (Long) jsonObject.get("instanceId");
if (id == instanceId){
count++;
}
}
System.out.print("Total number of tasks with that ID: ");
System.out.println(count + "\n");
return count;
}
//This function finds the most recent task given an instance id
//pre: valid id given, valid JSONArray built
//post: returns appropriate date for id, prints name
//param: JSONArray a, and Long id that user inputs
public static String findTask(JSONArray a, Long id){
String result = null;
String str = null;
Date dateCurrent;
Date dateResult = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Long currentID;
for (Object o : a){
JSONObject jsonObject = (JSONObject) o;
str = (String) jsonObject.get("createDate");
currentID = (Long) jsonObject.get("instanceId");
if (currentID.compareTo(id) == 0){
//parsing the createDate string into a comparable date and then comparing
try {
dateCurrent = sdf.parse(str);
//System.out.println("Date: " + sdf.format(dateCurrent));
if (dateResult == null){
dateResult = dateCurrent;
result = (String) jsonObject.get("name");
}
else{
if (dateCurrent.compareTo(dateResult) > 0){
dateResult = dateCurrent;
result = (String) jsonObject.get("name");
}
}
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println("Most recent date with that instance ID: " +sdf.format(dateResult));
System.out.println("Corresponding name: " + result + "\n");
return result;
}
//this function prints the number of open and closed tasks given a date
//pre: valid date input, valid JSONArray built
//post: prints tasks open/closed on a day inclusively
//param: valid string representing date from user, JSONArray a
//For this function, I'm assuming that any task with 'null' as a closing
//date indicates that it is still open and will always be counted as open
//given any date at all. I also am assuming that a task is considered open
//if the given date is before the close date of that task.
public static void findOpenClose(JSONArray a, String s){
int open = 0;
int closed = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String dateFormat = s;
String closeDate;
Date userDate = null;
Date currentDate = null;
try {
userDate = sdf.parse(dateFormat);
System.out.println(sdf.format(userDate));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Object o : a){
JSONObject jsonObject = (JSONObject) o;
closeDate = (String) jsonObject.get("closeDate");
//parse the close date to compare to the user date
if (closeDate != null){
try {
currentDate = sdf.parse(closeDate);
//System.out.println(sdf.format(currentDate));
if (currentDate.compareTo(userDate) > 0){
open++;
}
else{
closed++;
}
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//other case where closeDate = null so it must be open no matter what
else{
open++;
}
}
System.out.println("Number of open tasks given this date: " + open);
System.out.println("Number of closed tasks given this date " + closed + "\n");
}
//This function will determine whether a task was open or closed in a particular date range
//provided by the user
//pre: valid date range entered, proper JSONArray built
//post: prints number of tasks created or closed in date range
//param: JSONArray a, string start and end date
public static void allOpenClosedInRange(JSONArray a, String start, String end){
int openResult = 0;
int closeResult = 0;
String beginDate = start;
String endDate = end;
String currentBegin = null;
String currentEnd = null;
Date begin = null;
Date ending = null;
Date createDate = null;
Date closeDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
begin = sdf.parse(beginDate);
//System.out.println(sdf.format(begin));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ending = sdf.parse(endDate);
//System.out.println(sdf.format(ending));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Object o : a){
JSONObject jsonObject = (JSONObject) o;
currentBegin = (String) jsonObject.get("createDate");
try {
createDate = sdf.parse(currentBegin);
//System.out.println(sdf.format(createDate));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentEnd = (String) jsonObject.get("closeDate");
try {
if (currentEnd != null){
closeDate = sdf.parse(currentEnd);
//System.out.println(sdf.format(closeDate));
}
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (createDate.compareTo(begin) > 0 && createDate.compareTo(ending) < 0){
openResult++;
}
if (currentEnd != null && closeDate.compareTo(begin) > 0 && closeDate.compareTo(ending) < 0){
closeResult++;
}
}
System.out.println("Number of tasks opened in this range: " + openResult);
System.out.println("Number of tasks closed in this range: " + closeResult);
}
}