-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask4.java
More file actions
51 lines (46 loc) · 1.89 KB
/
Task4.java
File metadata and controls
51 lines (46 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;
public class Task4 {
static Scanner sc = new Scanner(System.in);
static String a[] = {"INR", "USD", "AUD", "BGN", "CAD", "CHF", "CNY", "EGP", "EUR", "GBP"};
static String targetCurrency;
static String baseCurrency;
static double amount;
static String apiurl;
public static void main(String[] args) throws Exception {
System.out.println("Enter an amount you want to conver:");
for (String a1 : a) {
System.out.println(a1);
}
System.out.println("Enter your base currency type");
baseCurrency = sc.next().toUpperCase();
System.out.println("Enter your target currency type");
targetCurrency = sc.next().toUpperCase();
apiurl = "https://v6.exchangerate-api.com/v6/8c529054bb48fd0a0b328308/latest/" + baseCurrency;
URL url = new URL(apiurl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String response = sb.toString();
br.close();
int index = response.indexOf("\"" + targetCurrency + "\":");
int start = index + targetCurrency.length() + 3;
int end = response.indexOf(",", start);
System.out.println(start);
System.out.println(end);
if (end == -1) {
end = response.indexOf("}", start);
}
double rate = Double.parseDouble(response.substring(start, end));
System.out.println("Enter an amount you want to conver:");
amount = sc.nextDouble();
double convertedAmount = amount * rate;
System.out.println("Converted Amount: " + convertedAmount);
System.out.println("Thank you");
}
}