-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrencyConverter.java
More file actions
48 lines (42 loc) · 1.66 KB
/
Copy pathCurrencyConverter.java
File metadata and controls
48 lines (42 loc) · 1.66 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
public class CurrencyConverter {
private String[] currencies;
private double[] exchangeRates;
public CurrencyConverter() {
currencies = new String[] {
"PHP", "USD", "EUR", "GBP", "JPY", "CAD", "AFN", "BYR",
"COP", "KPW", "KRW", "SEK", "SAR", "SGD", "PKR", "MMK", "LBP", "KZT", "DOP", "BND",
"GNF", "MYR", "IDR", "ILS", "HKD", "INR", "LRD", "RUB", "AED", "VND", "YER"
};
exchangeRates = new double[] {
1.0, 0.018, 0.016, 0.014, 2.58, 0.024, 1.31, 0.045,
71.33, 15.886, 23.40, 0.19, 0.066, 0.024, 5.39, 36.94, 263.71, 8.01, 1.00, 0.024,
150.67, 0.082, 267.87, 0.067, 0.14, 1.45, 3.26, 1.71, 0.064, 422.50, 4.39
};
}
public double convert(double amount, String inputAmount, String toConvert) {
int fromIndex = -1;
int toIndex = -1;
for (int i = 0; i < currencies.length; i++) {
if (currencies[i].equalsIgnoreCase(inputAmount)) {
fromIndex = i;
}
if (currencies[i].equalsIgnoreCase(toConvert)) {
toIndex = i;
}
}
if (fromIndex != -1 && toIndex != -1) {
double fromRate = exchangeRates[fromIndex];
double toRate = exchangeRates[toIndex];
return amount * (toRate / fromRate);
} else {
System.out.println("Invalid currency.");
return -1.0;
}
}
public void printCurrencies() {
System.out.println("Available Currencies:");
for (String currency : currencies) {
System.out.println(currency);
}
}
}