diff --git a/TravelBuddyApp/src/main/java/domain/WetterService.java b/TravelBuddyApp/src/main/java/domain/WetterService.java index e5db229..235575b 100644 --- a/TravelBuddyApp/src/main/java/domain/WetterService.java +++ b/TravelBuddyApp/src/main/java/domain/WetterService.java @@ -1,6 +1,78 @@ package domain; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; +import org.json.JSONArray; +import org.json.JSONObject; public class WetterService { + private static final String API_KEY = "fc01dfd47f80659f12512c295e80b201"; + private static final HttpClient client = HttpClient.newHttpClient(); + + public static String getAktuellesWetter(String location) throws IOException, InterruptedException { + + String url = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + API_KEY + + "&units=metric"; + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + if (response.statusCode() != 200) { + throw new IOException("Unerwarteter Code " + response.statusCode()); + } + + JSONObject json = new JSONObject(response.body()); + double temp = json.getJSONObject("main").getDouble("temp"); + String weather = json.getJSONArray("weather").getJSONObject(0).getString("description"); + + return "Aktuelle Temperatur: " + temp + "°C, Wetter: " + weather; + } + public static String getWettervorhersage(String location) throws IOException, InterruptedException { + + String url = "http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&appid=" + API_KEY + + "&units=metric"; + HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + if (response.statusCode() != 200) { + throw new IOException("Unerwarteter Code " + response.statusCode()); + } + + JSONObject json = new JSONObject(response.body()); + Map tempByDay = new HashMap<>(); + Map weatherByDay = new HashMap<>(); + LocalDate today = LocalDate.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + JSONArray forecastList = json.getJSONArray("list"); + for (int i = 0; i < forecastList.length(); i++) { + JSONObject forecastEntry = forecastList.getJSONObject(i); + String dateTime = forecastEntry.getString("dt_txt"); + String date = dateTime.split(" ")[0]; + LocalDate forecastDate = LocalDate.parse(date, formatter); + + if (forecastDate.isAfter(today) && !tempByDay.containsKey(date)) { + double temp = forecastEntry.getJSONObject("main").getDouble("temp"); + String weather = forecastEntry.getJSONArray("weather").getJSONObject(0).getString("description"); + + tempByDay.put(date, temp); + weatherByDay.put(date, weather); + } + } + + StringBuilder forecast = new StringBuilder("3-Tage-Wettervorhersage:\n"); + tempByDay.keySet().stream().sorted().limit(3).forEach(date -> { + forecast.append(String.format("%s - Temperatur: %.2f°C, Wetter: %s\n", date, tempByDay.get(date), + weatherByDay.get(date))); + }); + + return forecast.toString(); + } }