Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws IOException {
- Scanner scanner = new Scanner(System.in);
- System.out.print("From base: ");
- String from = scanner.nextLine();
- System.out.print("To base: ");
- String to = scanner.nextLine();
- System.out.print("Value: ");
- double value = Double.parseDouble(scanner.nextLine());
- String request = "https://api.fixer.io/latest?symbols=%s,%s";
- URL url = new URL(String.format(request, from.toUpperCase(), to.toUpperCase()));
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- BufferedReader in = new BufferedReader(
- new InputStreamReader(connection.getInputStream()));
- String inputLine;
- StringBuffer content = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- content.append(inputLine);
- }
- in.close();
- connection.disconnect();
- String base = content.substring(content.indexOf("base") + 7, content.indexOf("base") + 10);
- String rate = content.substring(content.indexOf("rates") + 14);
- double respondValue = 0.0;
- if (base.equals(from.toUpperCase())) {
- respondValue = value * Double.parseDouble(rate.substring(0, rate.length() - 2));
- } else {
- respondValue = value / Double.parseDouble(rate.substring(0, rate.length() - 2));
- }
- System.out.print("Exchanged value: ");
- System.out.println(String.format("%.3f", respondValue));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement