-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather.java
More file actions
35 lines (29 loc) · 1.22 KB
/
Weather.java
File metadata and controls
35 lines (29 loc) · 1.22 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
import org.json.simple.JSONValue;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public abstract class Weather {
private Location location;
private final String API_KEY = "3d17b1c61439d291f3e3d9d02ddaeaa1";
private RecursiveJSON data;
public Weather(Location location, String type) throws IOException, InterruptedException {
this.location = location;
SimpleAPI api = new SimpleAPI("https://api.openweathermap.org/data/2.5/" + type + "?q=" + URLEncoder.encode(location.toString(), StandardCharsets.UTF_8.toString()) + "&appid=" + API_KEY + "&units=metric");
this.data = new RecursiveJSON(JSONValue.parse(api.getResponse().body()));
}
public Location getLocation() {
return location;
}
public RecursiveJSON getData() {
return this.data;
};
public abstract void display();
public void safeDisplay() {
if (data.getValue("cod").toString().equals("200")) {
display();
} else {
System.out.println("Warning! The Weather API has returned a " + data.getValue("cod") + " status code.");
System.out.println("Status Message: " + data.getValue("message"));
}
}
}