diff --git a/src/main/java/model/WeatherString.java b/src/main/java/model/WeatherString.java new file mode 100644 index 0000000..95abfb3 --- /dev/null +++ b/src/main/java/model/WeatherString.java @@ -0,0 +1,35 @@ +package model; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; + +public class WeatherString { + public static String forecastString(String cityName, String response) { + String weatherForecast = ""; + + try { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode rootNode = objectMapper.readTree(response); + + String description = rootNode.get("weather").get(0).get("description").asText(); + double temperature = rootNode.get("main").get("temp").asDouble(); + int pressure = rootNode.get("main").get("pressure").asInt(); + int humidity = rootNode.get("main").get("humidity").asInt(); + + weatherForecast = String.format("city: %s\n" + + "description: %s\n" + + "temperature[K]: %.2f\n" + + "pressure[hPa]: %d\n" + + "humidity[%%]: %d\n" + + "--------------------", cityName, description, temperature, pressure, humidity); + + + + } catch (IOException e) { + System.out.println("Blad przy generowaniu prognozy."); + } + return weatherForecast; + } +}