structure rebuild of forecast structure so it's contains string preparation and format conversion types
This commit is contained in:
parent
9c2314917f
commit
f49ad0ae3b
BIN
results.pdf
Normal file
BIN
results.pdf
Normal file
Binary file not shown.
@ -1,8 +1,4 @@
|
||||
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.Paragraph;
|
||||
import com.itextpdf.text.pdf.PdfWriter;
|
||||
@ -11,15 +7,12 @@ import java.io.BufferedReader;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
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 outputFile;
|
||||
|
||||
public ConvertResult(String inputFile, String outputFile) {
|
||||
public ConvertResultPdf(String inputFile, String outputFile) {
|
||||
this.inputFile = inputFile;
|
||||
this.outputFile = outputFile;
|
||||
}
|
||||
|
@ -1,2 +0,0 @@
|
||||
package model;public class Forecast {
|
||||
}
|
@ -5,31 +5,20 @@ 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);
|
||||
public class ForecastPreparation {
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
75
src/main/java/model/weatherForecast.java
Normal file
75
src/main/java/model/weatherForecast.java
Normal 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;
|
||||
}
|
||||
}
|
@ -1,31 +1,32 @@
|
||||
package org.example;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
//import com.fasterxml.jackson.core.type.TypeReference;
|
||||
//import com.fasterxml.jackson.databind.JsonNode;
|
||||
//import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import model.*;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.CookieSpecRegistries;
|
||||
//import org.apache.http.client.ClientProtocolException;
|
||||
//import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
//import org.apache.http.client.methods.HttpUriRequest;
|
||||
//import org.apache.http.impl.client.CloseableHttpClient;
|
||||
//import org.apache.http.impl.client.CookieSpecRegistries;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLOutput;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
//import java.io.File;
|
||||
//import java.io.IOException;
|
||||
//import java.sql.SQLOutput;
|
||||
//import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
//import org.apache.http.Header;
|
||||
//import org.apache.http.HttpEntity;
|
||||
//import org.apache.http.HttpResponse;
|
||||
//import org.apache.http.client.methods.HttpGet;
|
||||
//import org.apache.http.impl.client.HttpClients;
|
||||
//import org.apache.http.util.EntityUtils;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
String API_key = "4c991761f17e10358c944a3c64a3e24c";
|
||||
String fileCities = "cities.json";
|
||||
@ -44,8 +45,8 @@ public class Main {
|
||||
|
||||
|
||||
HttpClient httpClient = new HttpClient();
|
||||
WeatherString weatherString = new WeatherString();
|
||||
|
||||
// ForecastPreparation weatherString = new ForecastPreparation();
|
||||
ForecastPreparation forecastPreparation = new ForecastPreparation();
|
||||
System.out.println("Program rozpoczal dzialanie.");
|
||||
|
||||
while (true) {
|
||||
@ -65,36 +66,41 @@ public class Main {
|
||||
String urlString = "https://api.openweathermap.org/data/2.5/weather?lat=" + latitude +
|
||||
"&lon=" + longitude + "&appid=" + API_key;
|
||||
String response = httpClient.executeGetRequest(urlString);
|
||||
// System.out.println(response);
|
||||
String forecastString = weatherString.forecastString(inputCity, response);
|
||||
// System.out.println(response);]
|
||||
|
||||
weatherForecast weatherForecast = forecastPreparation.fetchForecastData(inputCity, response);
|
||||
|
||||
String forecastString = weatherForecast.prepareForecastString();
|
||||
System.out.println(forecastString);
|
||||
temporaryFile.addToFile(forecastString);
|
||||
// String forecastString = weatherString.prepareForecast(inputCity, response);
|
||||
// System.out.println(forecastString);
|
||||
// temporaryFile.addToFile(forecastString);
|
||||
} else {
|
||||
System.out.println("Podanego miasta nie ma na liscie.");
|
||||
}
|
||||
} else if (Objects.equals(input, "z")) {
|
||||
System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
|
||||
String whichFormat = scanner.nextLine().toLowerCase();
|
||||
if (Objects.equals(whichFormat, "pdf")) {
|
||||
String outputFile = "results.pdf";
|
||||
ConvertResult convertResult = new ConvertResult(temporaryFile.FILE_NAME,
|
||||
"results.pdf");
|
||||
convertResult.convertToPdf();
|
||||
} else if (Objects.equals(whichFormat, "json")) {
|
||||
String outputFile = "results.json";
|
||||
ConvertResult convertResult = new ConvertResult(temporaryFile.FILE_NAME,
|
||||
"results.json");
|
||||
// convertResult.convertToJson();
|
||||
} else if (Objects.equals(whichFormat, "xml")) {
|
||||
String outputFile = "results.xml";
|
||||
ConvertResult convertResult = new ConvertResult(temporaryFile.FILE_NAME,
|
||||
"results.xml");
|
||||
// convertResult.convertToXml();
|
||||
} else {
|
||||
System.out.println("Wprowadz poprawna opcje.");
|
||||
}
|
||||
System.out.println("Program zakonczyl dzialanie.");
|
||||
break;
|
||||
// } else if (Objects.equals(input, "z")) {
|
||||
// System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
|
||||
// String whichFormat = scanner.nextLine().toLowerCase();
|
||||
// if (Objects.equals(whichFormat, "pdf")) {
|
||||
// String outputFile = "results.pdf";
|
||||
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
|
||||
// "results.pdf");
|
||||
// convertResult.convertToPdf();
|
||||
// } else if (Objects.equals(whichFormat, "json")) {
|
||||
// String outputFile = "results.json";
|
||||
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
|
||||
// "results.json");
|
||||
//// convertResult.convertToJson();
|
||||
// } else if (Objects.equals(whichFormat, "xml")) {
|
||||
// String outputFile = "results.xml";
|
||||
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
|
||||
// "results.xml");
|
||||
//// convertResult.convertToXml();
|
||||
// } else {
|
||||
// System.out.println("Wprowadz poprawna opcje.");
|
||||
// }
|
||||
// System.out.println("Program zakonczyl dzialanie.");
|
||||
// break;
|
||||
} else if (Objects.equals(input,"x")) {
|
||||
System.out.println("Program zakonczyl dzialanie.");
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user