string containing and presenting the forecast prepared
This commit is contained in:
parent
e42f0ac225
commit
f5ab4e64be
35
src/main/java/model/WeatherString.java
Normal file
35
src/main/java/model/WeatherString.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user