feat: finish iteration 1 without interface

This commit is contained in:
Nemo D'ACREMONT 2024-11-04 09:40:55 +01:00
parent 21f6c8b1a6
commit 243b613c79
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276

View File

@ -1,5 +1,6 @@
package eirb.pg203;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
@ -12,17 +13,20 @@ import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
String APIKey = args[0];
String APIKey = "cef8e1b6ea364994b5072423240111";
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:");
for (int i = 0 ; i < 3 ; i++) {
System.out.println(
"\tTemp: "
+ bordeauxWeather.getJSONObject("current").getFloat("feelslike_c")
"\tTemp (j + " + i + "): "
+ forecast.getJSONObject(i).getJSONObject("day").getFloat("avgtemp_c")
+ "°C"
);
}
}
public static JSONObject fetchChuckNorrisJoke() throws IOException {
StringBuilder result = new StringBuilder();
@ -38,12 +42,16 @@ public class Main {
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();
URL url = URI.create(
"https://api.weatherapi.com/v1/current.json?q=Bordeaux&key="
+ WeatherAPIKey
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(
@ -54,4 +62,9 @@ public class Main {
}
return new JSONObject(result.toString());
}
public static JSONObject fetchBordeauxWeather(String WeatherAPIKey, String city) throws IOException {
return fetchBordeauxWeather(WeatherAPIKey, city, 1);
}
}