feat: finish iteration 1 without interface
This commit is contained in:
parent
21f6c8b1a6
commit
243b613c79
@ -1,5 +1,6 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -12,16 +13,19 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
String APIKey = args[0];
|
String APIKey = "cef8e1b6ea364994b5072423240111";
|
||||||
System.out.println("Args: " + Arrays.toString(args));
|
System.out.println("Args: " + Arrays.toString(args));
|
||||||
|
|
||||||
JSONObject bordeauxWeather = fetchBordeauxWeather(APIKey);
|
JSONObject bordeauxWeather = fetchBordeauxWeather(APIKey, "Bordeaux", 3);
|
||||||
|
JSONArray forecast = bordeauxWeather.getJSONObject("forecast").getJSONArray("forecastday");
|
||||||
System.out.println("Bordeaux Weather:");
|
System.out.println("Bordeaux Weather:");
|
||||||
System.out.println(
|
for (int i = 0 ; i < 3 ; i++) {
|
||||||
"\tTemp: "
|
System.out.println(
|
||||||
+ bordeauxWeather.getJSONObject("current").getFloat("feelslike_c")
|
"\tTemp (j + " + i + "): "
|
||||||
+ "°C"
|
+ forecast.getJSONObject(i).getJSONObject("day").getFloat("avgtemp_c")
|
||||||
);
|
+ "°C"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JSONObject fetchChuckNorrisJoke() throws IOException {
|
public static JSONObject fetchChuckNorrisJoke() throws IOException {
|
||||||
@ -38,12 +42,16 @@ public class Main {
|
|||||||
return new JSONObject(result.toString());
|
return new JSONObject(result.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JSONObject fetchBordeauxWeather(String WeatherAPIKey) throws IOException {
|
public static JSONObject fetchBordeauxWeather(String WeatherAPIKey, String city, int days) throws IOException {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
URL url = URI.create(
|
URL url = URI.create(
|
||||||
"https://api.weatherapi.com/v1/current.json?q=Bordeaux&key="
|
String.format("https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d",
|
||||||
+ WeatherAPIKey
|
WeatherAPIKey,
|
||||||
|
city,
|
||||||
|
days
|
||||||
|
)
|
||||||
).toURL();
|
).toURL();
|
||||||
|
System.out.println(url.toString());
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
conn.setRequestMethod("GET");
|
conn.setRequestMethod("GET");
|
||||||
try (BufferedReader reader = new BufferedReader(
|
try (BufferedReader reader = new BufferedReader(
|
||||||
@ -54,4 +62,9 @@ public class Main {
|
|||||||
}
|
}
|
||||||
return new JSONObject(result.toString());
|
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