Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
4.7 kB
2
Indexable
Never
/**
 *
 *  @author Grzegorczyk Jakub S20813
 *
 */


package zad2;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.*;
import com.sun.media.jfxmediaimpl.HostUtils;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.*;
import java.util.Locale;
import java.util.Map;

public class Service {
    Map<String, String> countries = new HashMap<>();
    Map<String, String> currencies = new HashMap<>();

    private String country;
    private String currency;
    private String countryCode;
    private String apiKey = "bc61c0fbfb7db10e88608ab42731a230";

    public Service(String country) {
        this.country = country;
        // Generate the Map with the iso and country name
        for (String iso : Locale.getISOCountries()) {
            Locale l = new Locale("", iso);
            countries.put(l.getDisplayCountry(Locale.ENGLISH), iso);
        }
        countryCode = countries.get(country);
    }

    // api key
    // bc61c0fbfb7db10e88608ab42731a230
    public String getWeather(String cityWeather){
        try {
            String urlFormat = String.format("https://api.openweathermap.org/data/2.5/weather?q=%s,%s&appid=%s",cityWeather, countryCode, apiKey); // Prepare the URL with all needed stuff
            URL weatherUrl = new URL(urlFormat); // Create the URL object

            HttpURLConnection con = (HttpURLConnection) weatherUrl.openConnection(); // Create/Open the connection
            con.setRequestMethod("GET"); // Setup the request method
            int status = con.getResponseCode(); // Status of the response, activate the request

            // Part for getting the output
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            String jsonResponse = content.toString();
            return jsonResponse;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    };

    public static String getCurrencyCode(String countryCode) {
        return Currency.getInstance(new Locale("", countryCode)).getCurrencyCode();
    }

    public Double getRateFor(String currency) {


        try {
            String apiKey = "5912e69ce275f1d1b111672201532740";
            String currencyCode = getCurrencyCode(countryCode);
            String currencyFormat = String.format("http://api.exchangeratesapi.io/latest?access_key=%s&symbols=%s,%s",apiKey,currencyCode,currency);
            URL currencyUrl = new URL(currencyFormat);
            HttpURLConnection con = (HttpURLConnection) currencyUrl.openConnection(); // Create/Open the connection
            con.setRequestMethod("GET"); // Setup the request method

            int status = con.getResponseCode();

            // Part for getting the output
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer content = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();

            System.out.println(content.toString());
            String stringJson = content.toString();

            ObjectMapper objectMapper = new ObjectMapper(); // Create objectMapper
            JsonNode jsonNode = objectMapper.readTree(stringJson);

            System.out.println(jsonNode.get("rates"));
            JsonNode rates = jsonNode.get("rates");
            double currencyCodeRate = rates.get(currencyCode).asDouble();
            double currencyRate = rates.get(currency).asDouble();
            double result = (double) Math.round(currencyCodeRate / currencyRate * 100000d) / 100000d;
            System.out.println(result);

            System.out.println(rates.get(currencyCode));
            System.out.println(rates.get(currency));

            String s = jsonNode.get("rates").toString();

            return 1.0;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return -1.0;
    }



}