-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartCity.java
More file actions
118 lines (112 loc) · 5.15 KB
/
SmartCity.java
File metadata and controls
118 lines (112 loc) · 5.15 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
118
import org.jsoup.Jsoup;
import java.util.Scanner;
import java.util.LinkedList;
import org.jsoup.nodes.Document;
import java.util.stream.Collectors;
import java.net.URL;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import org.json.*;
import org.json.simple.*;
import org.json.simple.parser.*;
import java.util.Scanner;
import java.io.*;
import java.util.*;
import org.json.simple.parser.JSONParser;
import com.google.gson.*;
import com.careerjet.webservice.api.Client;
public class SmartCity {
public static void main(String args[]) throws Exception{
System.out.println("Smart City");
Scanner s = new Scanner(System.in);
//Tourism module
//get hotel
SmartCity sc = new SmartCity();
System.out.println(sc.getHotels());
int distance = s.nextInt();
System.out.println("Enter distance in kilometers");
System.out.println(sc.getAttractions(distance));
}
/**
* Gets the names of the hotels in the city.
* @return the names of the hotels nearby.
*/
public String getHotels() throws Exception {
String hotels = "";
URL url = new URL("https://api.geoapify.com/v2/places?categories=accommodation.hotel&filter=rect:-83.5565068267818,34.08940116555951,-83.18829317321723,33.79675168842013&limit=20&apiKey=a305ad8ec2e34a569c192df2bba27be2");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
StringBuilder result = new StringBuilder();
conn.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
result.append(line);
}
String s = result.toString();
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(s);
JsonArray status = jo.getAsJsonArray("features");
for (int i = 0; i < status.size(); i++){
JsonElement hotel = status.get(i);
JsonObject j = hotel.getAsJsonObject();
JsonObject properties = j.getAsJsonObject("properties");
JsonPrimitive hotelName = properties.getAsJsonPrimitive("name");
if (hotelName == null) {
hotelName = properties.getAsJsonPrimitive("address_line1");
}
JsonPrimitive addr = properties.getAsJsonPrimitive("address_line2");
hotels += hotelName + "\n" + addr;
}
}
return hotels;
}
/**
* Gets the names of attractions within a certain distance from the city.
* @return the names of the attractions nearby.
* @throws Exception
*/
public String getAttractions(int distance) throws Exception {
String res = "";
URL url2 = new URL("https://api.opentripmap.com/0.1/en/places/radius?radius=" + distance * 1000 + "&lon=-83.357604&lat=33.9519&apikey=5ae2e3f221c38a28845f05b634ee2da680014417a5342d1496cdc86a");
HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
StringBuilder result2 = new StringBuilder();
conn2.setRequestMethod("GET");
try (BufferedReader reader2 = new BufferedReader(
new InputStreamReader(conn2.getInputStream()))) {
for (String line2; (line2 = reader2.readLine()) != null; ) {
result2.append(line2);
}
String s2 = result2.toString();
JsonParser jsonParser = new JsonParser();
JsonObject root = (JsonObject)jsonParser.parse(s2);
JsonArray features = root.getAsJsonArray("features");
int count=0;
LinkedList<String> types = new LinkedList<String>();
for (int i = 0; i < features.size(); i++) {
JsonElement place = features.get(i);
JsonObject placeObject = place.getAsJsonObject();
JsonObject properties = placeObject.getAsJsonObject("properties");
System.out.println(properties.getAsJsonPrimitive("name").getAsString() + properties.getAsJsonPrimitive("kinds").getAsString());
types.addLast(properties.getAsJsonPrimitive("kinds").getAsString());
res += properties.getAsJsonPrimitive("name") + "\n";
for (String j:types){
if (j.equals("cemeteries,historic,burial_places,interesting_places")){
count += 1;
}
}
}
List<String> Unique = types.stream().distinct().collect(Collectors.toList());
for (String s: Unique) {
System.out.println(s + ": " + Collections.frequency(types, s));
}
return res;
}
}
}