refactor(main): clean main
This commit is contained in:
parent
50a424c4b3
commit
f72b30311c
@ -1,70 +1,10 @@
|
||||
package eirb.pg203;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
String APIKey = "cef8e1b6ea364994b5072423240111";
|
||||
System.out.println("Args: " + Arrays.toString(args));
|
||||
|
||||
JSONObject bordeauxWeather = fetchBordeauxWeather(APIKey, "Bordeaux", 3);
|
||||
JSONArray forecast = bordeauxWeather.getJSONObject("forecast").getJSONArray("forecastday");
|
||||
System.out.println("Bordeaux Weather:");
|
||||
for (int i = 0 ; i < 3 ; i++) {
|
||||
System.out.println(
|
||||
"\tTemp (j + " + i + "): "
|
||||
+ forecast.getJSONObject(i).getJSONObject("day").getFloat("avgtemp_c")
|
||||
+ "°C"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONObject fetchChuckNorrisJoke() throws IOException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
URL url = URI.create("https://api.chucknorris.io/jokes/random").toURL();
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream()))) {
|
||||
for (String line; (line = reader.readLine()) != null; ) {
|
||||
result.append(line);
|
||||
}
|
||||
}
|
||||
return new JSONObject(result.toString());
|
||||
}
|
||||
|
||||
public static JSONObject fetchBordeauxWeather(String WeatherAPIKey, String city, int days) throws IOException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
URL url = URI.create(
|
||||
String.format("https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d",
|
||||
WeatherAPIKey,
|
||||
city,
|
||||
days
|
||||
)
|
||||
).toURL();
|
||||
System.out.println(url.toString());
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(conn.getInputStream()))) {
|
||||
for (String line; (line = reader.readLine()) != null; ) {
|
||||
result.append(line);
|
||||
}
|
||||
}
|
||||
return new JSONObject(result.toString());
|
||||
}
|
||||
|
||||
public static JSONObject fetchBordeauxWeather(String WeatherAPIKey, String city) throws IOException {
|
||||
return fetchBordeauxWeather(WeatherAPIKey, city, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user