creation of the main loop

This commit is contained in:
Marcin Hutek 2023-12-08 17:39:27 +01:00
parent 392460a794
commit c6c55f0dd9

View File

@ -1,7 +1,106 @@
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.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;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
String API_key = "4c991761f17e10358c944a3c64a3e24c";
String fileCities = "cities.json";
JsonToMap jsonToMap = new JsonToMap();
Map<String, Map<String, Double>> cityMap =
jsonToMap.createCityMap(fileCities);
CityNames cityNames = new CityNames();
cityNames.cityNamesRead(fileCities);
Scanner scanner = new Scanner(System.in);
TemporaryFile temporaryFile = new TemporaryFile();
temporaryFile.createEmptyFile();
HttpClient httpClient = new HttpClient();
WeatherString weatherString = new WeatherString();
System.out.println("Program rozpoczal dzialanie.");
while (true) {
System.out.println("Sprawdz pogode - y \n" +
"Zakoncz i zapisz wynik - z\n" +
"Wyjdz bez zapisywania - x");
String input = scanner.nextLine().toLowerCase();
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");
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(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,"x")) {
System.out.println("Program zakonczyl dzialanie.");
break;
} else {
System.out.println("Wprowadz poprawna opcje.");
}
}
}
}
}