PRA2024/src/main/java/model/ForecastPreparation.java

36 lines
1.2 KiB
Java

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;
}
}