Main.java cleared and commented out

This commit is contained in:
Marcin Hutek 2023-12-12 20:38:29 +01:00
parent 8eab3cd07c
commit dfeb251d16
5 changed files with 68 additions and 78 deletions

2
.gitignore vendored
View File

@ -36,3 +36,5 @@ build/
### Mac OS ###
.DS_Store
temporary_file.txt

41
results/.gitignore vendored Normal file
View File

@ -0,0 +1,41 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
*
!.gitignore

View File

@ -1,41 +0,0 @@
//package model;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.itextpdf.text.Document;
//import com.itextpdf.text.Paragraph;
//import com.itextpdf.text.pdf.PdfWriter;
//
//import java.io.BufferedReader;
//import java.io.FileOutputStream;
//import java.io.FileReader;
//import java.io.IOException;
//import java.util.List;
//
//public class ConvertResultPdf {
// private String inputFile;
// private String outputFile;
//
//
// public ConvertResultPdf(String inputFile, String outputFile) {
// this.inputFile = inputFile;
// this.outputFile = outputFile;
// }
//
// public void convertToPdf() {
// try (BufferedReader br = new BufferedReader(new FileReader(inputFile))) {
// Document document = new Document();
// PdfWriter.getInstance(document, new FileOutputStream(outputFile));
// document.open();
//
// String line;
// while ((line = br.readLine()) != null) {
// document.add(new Paragraph(line));
// }
//
// document.close();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//}

View File

@ -1,32 +1,15 @@
package org.example;
//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 java.io.IOException;
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
//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;
public class Main {
public static void main(String[] args) throws IOException {
@ -34,6 +17,7 @@ public class Main {
String API_key = "4c991761f17e10358c944a3c64a3e24c";
String fileCities = "cities.json";
// utworzenie listy miast wraz z ich koordynatami
JsonToMap jsonToMap = new JsonToMap();
Map<String, Map<String, Double>> cityMap =
jsonToMap.createCityMap(fileCities);
@ -45,67 +29,70 @@ public class Main {
Scanner scanner = new Scanner(System.in);
// utworzenie pustego pliku tymczasowego przechowujacego wyniki zapytan
TemporaryFile temporaryFile = new TemporaryFile();
temporaryFile.createEmptyFile();
HttpClient httpClient = new HttpClient();
// ForecastPreparation weatherString = new ForecastPreparation();
ForecastPreparation forecastPreparation = new ForecastPreparation();
System.out.println("Program rozpoczal dzialanie.");
// glowna petla programu
while (true) {
System.out.println("Sprawdz pogode - y \n" +
"Zakoncz i zapisz wynik - z\n" +
"Wyjdz bez zapisywania - x");
String input = scanner.nextLine().toLowerCase();
// wyswietlenie prognozy dla wskazanego miasta
if (Objects.equals(input, "y")) {
System.out.println("Podaj nazwe miasta: ");
String inputCity = scanner.nextLine().toLowerCase();
if (cityMap.containsKey(inputCity)) {
Map<String, Double> specificCity = (Map<String, Double>) cityMap.get(inputCity);
double latitude = (double) specificCity.get("latitude");
double longitude = (double) specificCity.get("longitude");
// wygenerowanie odpowiedniego adresu url
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);]
weatherForecast weatherForecast = forecastPreparation.fetchForecastData(inputCity, response);
String forecastString = weatherForecast.prepareForecastString();
System.out.println(forecastString);
// String forecastString = weatherString.prepareForecast(inputCity, response);
// System.out.println(forecastString);
// zapisanie prognozy do pliku tymczasowego
temporaryFile.addToFile(forecastString);
} else {
System.out.println("Podanego miasta nie ma na liscie.");
}
// zapis wynikow
} else if (Objects.equals(input, "z")) {
System.out.println("W jakim formacie chcesz zapisac wyniki?\n pdf/json/xml");
String whichFormat = scanner.nextLine().toLowerCase();
// odczyt czasu na potrzeby wygenerowania nazwy pliku
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss");
String sformatowanyCzas = czas.format(formatter);
if (Objects.equals(whichFormat, "pdf")) {
// ConvertResultPdf convertResult = new ConvertResultPdf(temporaryFile.FILE_NAME,
// "results.pdf");
// convertResult.convertToPdf();
// zapis do pdf
if (Objects.equals(whichFormat, "pdf")) {
String outputFile = "results/results" + sformatowanyCzas + ".pdf" ;
// System.out.println(sformatowanyCzas);
// System.out.println(outputFile);
weatherForecast.saveToPdf(temporaryFile.FILE_NAME, outputFile);
//zapis do json
} else if (Objects.equals(whichFormat, "json")) {
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss");
// String sformatowanyCzas = czas.format(formatter);
String outputFile = "results/results" + sformatowanyCzas + ".json";
weatherForecast.saveToJson(outputFile);
// zapis do xml
} else if (Objects.equals(whichFormat, "xml")) {
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss");
// String sformatowanyCzas = czas.format(formatter);
String outputFile = "results/results" + sformatowanyCzas + ".xml";
weatherForecast.saveToXml(outputFile);
} else {
@ -114,6 +101,7 @@ public class Main {
}
System.out.println("Program zakonczyl dzialanie.");
break;
// wyjscie z programu
} else if (Objects.equals(input,"x")) {
System.out.println("Program zakonczyl dzialanie.");
break;

View File

@ -1,6 +1,6 @@
city: warsaw
description: broken clouds
temperature[K]: 272,49
pressure[hPa]: 1006
humidity[%]: 87
temperature[K]: 276,80
pressure[hPa]: 1005
humidity[%]: 95
--------------------