Advertisement
ivanov_ivan

RateValues

Jan 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.     public static void main(String[] args) throws IOException {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         System.out.print("From base: ");
  13.         String from = scanner.nextLine();
  14.         System.out.print("To base: ");
  15.         String to = scanner.nextLine();
  16.         System.out.print("Value: ");
  17.         double value = Double.parseDouble(scanner.nextLine());
  18.         String request = "https://api.fixer.io/latest?symbols=%s,%s";
  19.         URL url = new URL(String.format(request, from.toUpperCase(), to.toUpperCase()));
  20.  
  21.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  22.  
  23.         connection.setRequestMethod("GET");
  24.  
  25.         BufferedReader in = new BufferedReader(
  26.                 new InputStreamReader(connection.getInputStream()));
  27.         String inputLine;
  28.         StringBuffer content = new StringBuffer();
  29.         while ((inputLine = in.readLine()) != null) {
  30.             content.append(inputLine);
  31.         }
  32.         in.close();
  33.         connection.disconnect();
  34.         String base = content.substring(content.indexOf("base") + 7, content.indexOf("base") + 10);
  35.         String rate = content.substring(content.indexOf("rates") + 14);
  36.  
  37.         double respondValue = 0.0;
  38.         if (base.equals(from.toUpperCase())) {
  39.             respondValue = value * Double.parseDouble(rate.substring(0, rate.length() - 2));
  40.         } else {
  41.             respondValue = value / Double.parseDouble(rate.substring(0, rate.length() - 2));
  42.         }
  43.         System.out.print("Exchanged value: ");
  44.         System.out.println(String.format("%.3f", respondValue));
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement