string containing and presenting the forecast prepared

This commit is contained in:
Marcin Hutek 2023-12-08 17:37:26 +01:00
parent e42f0ac225
commit f5ab4e64be
1 changed files with 35 additions and 0 deletions

View File

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