Menu
Question Index
...


Write a program to convert amount from one currency to another. The exchange rates between various currencies are given.

Input (List, String, double, String) Output (double)
[CNY-USD:6.16, JPY-USD:99.079, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], EGP, 1264.00, EUR 141.07143
[CNY-USD:6.16, JPY-USD:99.079, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], CNY, 499.00, LKR 10277.29383
[CNY-USD:6.16, JPY-USD:99.079, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], JPY, 2500.00, GBP 16.27896
[INR-USD:54.18, JPY-USD:99.079, CNY-USD:6.16, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], INR, 5000.00, USD 92.28498
[INR-USD:54.18, JPY-USD:99.079, CNY-USD:6.16, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], INR, 5000.00, EUR 70.86168
INR-USD:54.18, JPY-USD:99.079, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], EGP, 1264.00, JPY 18202.88605
INR-USD:54.18, JPY-USD:99.079, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], GBP, 2500.00, JPY 383931.125

class AdvancedExchangeCurrency

{    public static void main(String s[])
    {
        List exchangeRates = new ArrayList();
        exchangeRates.add(new ExchangeRate("INR", "USD", 54.18));
        exchangeRates.add(new ExchangeRate("JPY", "USD", 99.079));
        exchangeRates.add(new ExchangeRate("CNY", "USD", 6.16));
        exchangeRates.add(new ExchangeRate("USD", "EGP", 0.15));
        exchangeRates.add(new ExchangeRate("EGP", "EUR", 8.96));
        exchangeRates.add(new ExchangeRate("LKR", "USD", 126.87));
        exchangeRates.add(new ExchangeRate("USD", "GBP", 1.55));
        double pounds = exchange(exchangeRates, "INR", 500.0, "GBP");
        System.out.println("500 INR is same as " + pounds + " GBP");

    }


public static Double exchange(List<ExchangeRate> exchangeRates, String sourceCurrency, double amount, String targetCurrency) {
//Write code here to convert from one currency to another
}
}
class ExchangeRate {

String targetCurrency;
String baseCurrency;
double rate;

public ExchangeRate(String targetCurrency, String baseCurrency, double rate) {
this.targetCurrency = targetCurrency;
this.baseCurrency = baseCurrency;
this.rate = rate;
}

@Override
public String toString() {
return targetCurrency + "-" + baseCurrency + ":" + rate;
}

//If required, write any additional methods here
}

Doubts

Problems

Topic: Java ArrayList

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App