-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrencyConverter.java
More file actions
73 lines (65 loc) · 2.35 KB
/
CurrencyConverter.java
File metadata and controls
73 lines (65 loc) · 2.35 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package InternSavy;
import java.util.Scanner;
public class CurrencyConverter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// prompt the user to enter the amount and currency code
System.out.print("Enter the amount: ");
double amount = input.nextDouble();
System.out.print("Enter the currency code (e.g. USD, EUR, GBP): ");
String currencyCode = input.next().toUpperCase();
// prompt the user to enter the target currency code
System.out.print("Enter the target currency code (e.g. USD, EUR, GBP): ");
String targetCode = input.next().toUpperCase();
// calculate the exchange rate
double exchangeRate;
switch (currencyCode) {
case "USD":
switch (targetCode) {
case "EUR":
exchangeRate = 0.83;
break;
case "GBP":
exchangeRate = 0.72;
break;
default:
exchangeRate = 1.0;
break;
}
break;
case "EUR":
switch (targetCode) {
case "USD":
exchangeRate = 1.21;
break;
case "GBP":
exchangeRate = 0.86;
break;
default:
exchangeRate = 1.0;
break;
}
break;
case "GBP":
switch (targetCode) {
case "USD":
exchangeRate = 1.39;
break;
case "EUR":
exchangeRate = 1.17;
break;
default:
exchangeRate = 1.0;
break;
}
break;
default:
exchangeRate = 1.0;
break;
}
// calculate the result
double result = amount * exchangeRate;
// display the result
System.out.printf("%.2f %s = %.2f %s", amount, currencyCode, result, targetCode);
}
}