Untitled
unknown
java
3 years ago
4.7 kB
2
Indexable
/** * * @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; import java.util.function.DoubleToIntFunction; import java.util.regex.Matcher; import java.util.regex.Pattern; 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); } public String getWeather(String cityWeather){ 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 return getStringFromUrl(urlFormat); }; 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); String stringJson = getStringFromUrl(currencyFormat); ObjectMapper objectMapper = new ObjectMapper(); // Create objectMapper JsonNode jsonNode = objectMapper.readTree(stringJson); // Get the tree from the String JsonNode rates = jsonNode.get("rates"); // Get the rates map from the json object 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); return result; } catch (IOException e) { e.printStackTrace(); } return 0.0; } public String getStringFromUrl(String urlString){ try { URL url = new URL(urlString); HttpURLConnection con = (HttpURLConnection) url.openConnection(); // Create/Open the connection con.setRequestMethod("GET"); // Setup the request method int status = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); return content.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public Double getNBPRate() { String kursyA = getStringFromUrl("https://www.nbp.pl/kursy/kursya.html").replaceAll("\\s", "_"); String kursyB = getStringFromUrl("https://www.nbp.pl/kursy/kursyb.html").replaceAll("\\s", "_"); String pattern = String.format("<td_class=\"right\">.*%s</td>_*<td_class=\"right\">([0-9]*,[0-9]*)</td>", getCurrencyCode(countryCode)); // Regex for getting the Kurs Pattern pA = Pattern.compile(pattern); Pattern pB = Pattern.compile(pattern); Matcher mA = pA.matcher(kursyA); Matcher mB = pB.matcher(kursyB); if( mA.find() ){ return Double.parseDouble(mA.group(1).replaceAll(",",".")); }else if ( mB.find() ){ return Double.parseDouble(mB.group(1).replaceAll(",", ".")); }else{ return 0.0; } } }
Editor is loading...