-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransporterUtils.java
More file actions
101 lines (81 loc) · 2.86 KB
/
TransporterUtils.java
File metadata and controls
101 lines (81 loc) · 2.86 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
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
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;
/**
* The TransporterParser is for any methods involving the conversion of JSON
* data to Transporter objects or vice versa.
*/
public class TransporterUtils {
/**
* Constructor, currently it does not do anything special
*/
public TransporterUtils() {
}
/**
* Reads in a JSON file, parses it and inputs the data into an ArrayList of
* Transporter objects.
*
* @param filename the name of the JSON file to read the transporter info from
* @return an ArrayList of Transporter objects
*/
public ArrayList<Transporter> parseFromFile(String filename) {
ArrayList<Transporter> transporters = new ArrayList<Transporter>();
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader(filename));
JSONArray jsonArray = (JSONArray) obj;
Iterator<JSONObject> iterator = jsonArray.iterator();
while (iterator.hasNext()) {
JSONObject jobj = iterator.next();
String location = (String) jobj.get(Transporter.TAG_LOCATION);
String coords = (String) jobj.get(Transporter.TAG_COORDS);
ArrayList<HashMap<String, String>> transports_to = new ArrayList<HashMap<String, String>>();
JSONArray places = (JSONArray) jobj.get(Transporter.TAG_GOINGTO);
Iterator<JSONObject> it = places.iterator();
while (it.hasNext()) {
JSONObject j = it.next();
HashMap<String, String> map = new HashMap<String, String>();
map.put(Transporter.TAG_PLACE, (String) j.get(Transporter.TAG_PLACE));
map.put(Transporter.TAG_PRICE, (String) j.get(Transporter.TAG_PRICE));
transports_to.add(map);
}
transporters.add(new Transporter(location, coords, transports_to));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return transporters;
}
/**
* Sorts an ArrayList of Transporter objects by alphabetical order (of their
* location)
*
* @param tlist the ArrayList to be sorted
* @return the sorted ArrayList
*/
public ArrayList<Transporter> sortTransporters(ArrayList<Transporter> tlist) {
tlist.sort(new TransporterComparator());
return tlist;
}
/**
* Custom Comparator class for comparing Transporter objects
*/
private class TransporterComparator implements Comparator<Transporter> {
@Override
public int compare(Transporter o1, Transporter o2) {
return o1.getLocation().compareTo(o2.getLocation());
}
}
}