structure rebuild of forecast structure so it's contains string preparation and format conversion types

This commit is contained in:
Marcin Hutek 2023-12-09 12:24:50 +01:00
parent 9c2314917f
commit f49ad0ae3b
6 changed files with 142 additions and 81 deletions

BIN
results.pdf Normal file

Binary file not shown.

View File

@ -1,8 +1,4 @@
package model; package model;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.itextpdf.text.Document; import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph; import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.PdfWriter;
@ -11,15 +7,12 @@ import java.io.BufferedReader;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ConvertResult { public class ConvertResultPdf {
private String inputFile; private String inputFile;
private String outputFile; private String outputFile;
public ConvertResult(String inputFile, String outputFile) { public ConvertResultPdf(String inputFile, String outputFile) {
this.inputFile = inputFile; this.inputFile = inputFile;
this.outputFile = outputFile; this.outputFile = outputFile;
} }

View File

@ -1,2 +0,0 @@
package model;public class Forecast {
}

View File

@ -5,31 +5,20 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException; import java.io.IOException;
public class WeatherString { public class ForecastPreparation {
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);
public static weatherForecast fetchForecastData(String cityName, String response) throws IOException {
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();
return new weatherForecast(cityName, description, temperature, pressure, humidity);
} catch (IOException e) {
System.out.println("Blad przy generowaniu prognozy.");
}
return weatherForecast;
} }
} }

View File

@ -0,0 +1,75 @@
package model;
import java.util.ArrayList;
import java.util.List;
public class weatherForecast {
public static final List<weatherForecast> allEntries = new ArrayList<>();
private final String cityName;
private final String description;
private final double temperature;
private final int pressure;
private final int humidity;
public weatherForecast(String cityName, String description, double temperature,
int pressure, int humidity) {
this.cityName = cityName;
this.description = description;
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
allEntries.add(this);
}
public String prepareForecastString() {
String weatherForecastString = String.format("""
city: %s
description: %s
temperature[K]: %.2f
pressure[hPa]: %d
humidity[%%]: %d
--------------------""", this.cityName, this.description, this.temperature,
this.pressure, this.humidity);
return weatherForecastString;
}
// public static void saveToJson() throws IOException {
// File jsonFile = new File(generateFileName(".json"));
// ObjectMapper mapper = new ObjectMapper();
// mapper.writeValue(jsonFile, allEntries);
// }
// public static void saveToXml() throws IOException {
// File xmlFile = new File(generateFileName(".xml"));
// XmlMapper mapper = new XmlMapper();
// mapper.writeValue(xmlFile, allEntries);
// }
public static List<weatherForecast> getAllEntries() {
return new ArrayList<>(allEntries);
}
public String getCityName() {
return cityName;
}
public String getDescription() {
return description;
}
public double getTemperature() {
return temperature;
}
public int getPressure() {
return pressure;
}
public int getHumidity() {
return humidity;
}
}

View File

@ -1,31 +1,32 @@
package org.example; package org.example;
import com.fasterxml.jackson.core.type.TypeReference; //import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; //import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; //import com.fasterxml.jackson.databind.ObjectMapper;
import model.*; import model.*;
import org.apache.http.client.ClientProtocolException; //import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse; //import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest; //import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient; //import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.CookieSpecRegistries; //import org.apache.http.impl.client.CookieSpecRegistries;
import java.util.*;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLOutput; import java.util.*;
import java.util.List; //import java.io.File;
//import java.io.IOException;
//import java.sql.SQLOutput;
//import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner; import java.util.Scanner;
import org.apache.http.Header; //import org.apache.http.Header;
import org.apache.http.HttpEntity; //import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; //import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet; //import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients; //import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; //import org.apache.http.util.EntityUtils;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
String API_key = "4c991761f17e10358c944a3c64a3e24c"; String API_key = "4c991761f17e10358c944a3c64a3e24c";
String fileCities = "cities.json"; String fileCities = "cities.json";
@ -44,8 +45,8 @@ public class Main {
HttpClient httpClient = new HttpClient(); HttpClient httpClient = new HttpClient();
WeatherString weatherString = new WeatherString(); // ForecastPreparation weatherString = new ForecastPreparation();
ForecastPreparation forecastPreparation = new ForecastPreparation();
System.out.println("Program rozpoczal dzialanie."); System.out.println("Program rozpoczal dzialanie.");
while (true) { while (true) {
@ -65,36 +66,41 @@ public class Main {
String urlString = "https://api.openweathermap.org/data/2.5/weather?lat=" + latitude + String urlString = "https://api.openweathermap.org/data/2.5/weather?lat=" + latitude +
"&lon=" + longitude + "&appid=" + API_key; "&lon=" + longitude + "&appid=" + API_key;
String response = httpClient.executeGetRequest(urlString); String response = httpClient.executeGetRequest(urlString);
// System.out.println(response); // System.out.println(response);]
String forecastString = weatherString.forecastString(inputCity, response);
weatherForecast weatherForecast = forecastPreparation.fetchForecastData(inputCity, response);
String forecastString = weatherForecast.prepareForecastString();
System.out.println(forecastString); System.out.println(forecastString);
temporaryFile.addToFile(forecastString); // String forecastString = weatherString.prepareForecast(inputCity, response);
// System.out.println(forecastString);
// temporaryFile.addToFile(forecastString);
} else { } else {
System.out.println("Podanego miasta nie ma na liscie."); System.out.println("Podanego miasta nie ma na liscie.");
} }
} else if (Objects.equals(input, "z")) { // } else if (Objects.equals(input, "z")) {
System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml"); // System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
String whichFormat = scanner.nextLine().toLowerCase(); // String whichFormat = scanner.nextLine().toLowerCase();
if (Objects.equals(whichFormat, "pdf")) { // if (Objects.equals(whichFormat, "pdf")) {
String outputFile = "results.pdf"; // String outputFile = "results.pdf";
ConvertResult convertResult = new ConvertResult(temporaryFile.FILE_NAME, // ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
"results.pdf"); // "results.pdf");
convertResult.convertToPdf(); // convertResult.convertToPdf();
} else if (Objects.equals(whichFormat, "json")) { // } else if (Objects.equals(whichFormat, "json")) {
String outputFile = "results.json"; // String outputFile = "results.json";
ConvertResult convertResult = new ConvertResult(temporaryFile.FILE_NAME, // ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
"results.json"); // "results.json");
// convertResult.convertToJson(); //// convertResult.convertToJson();
} else if (Objects.equals(whichFormat, "xml")) { // } else if (Objects.equals(whichFormat, "xml")) {
String outputFile = "results.xml"; // String outputFile = "results.xml";
ConvertResult convertResult = new ConvertResult(temporaryFile.FILE_NAME, // ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
"results.xml"); // "results.xml");
// convertResult.convertToXml(); //// convertResult.convertToXml();
} else { // } else {
System.out.println("Wprowadz poprawna opcje."); // System.out.println("Wprowadz poprawna opcje.");
} // }
System.out.println("Program zakonczyl dzialanie."); // System.out.println("Program zakonczyl dzialanie.");
break; // break;
} else if (Objects.equals(input,"x")) { } else if (Objects.equals(input,"x")) {
System.out.println("Program zakonczyl dzialanie."); System.out.println("Program zakonczyl dzialanie.");
break; break;