Get Relevant Exchange Rate - Java Quiz Question


Write a program to get the exchange rate of the given currency pair. The exchange rates between various currencies are given.

Input (List, String, String) Output (ExchangeRate)
[USD-INR:54.17, USD-JPY:97.94, USD-GBP:0.65], INR, USD INR-USD:0.01846
[USD-INR:54.17, USD-JPY:97.94, USD-GBP:0.65], USD, INR USD-INR:54.17
[USD-INR:54.17, USD-JPY:97.94, USD-GBP:0.65], USD, JPY USD-JPY:97.94
[USD-INR:54.17, USD-JPY:97.94, USD-GBP:0.65], JPY, INR null
[USD-INR:54.17, USD-JPY:97.94, USD-GBP:0.65], USD, GBP USD-GBP:0.65
[CNY-USD:0.16, EUR-USD:1.31, USD-LKR:126.67, USD-GBP:0.65], CNY, USD CNY-USD:0.16
[CNY-USD:0.16, EUR-USD:1.31, USD-LKR:126.67, USD-GBP:0.65], USD, EUR USD-EUR:0.76336
[CNY-USD:0.16, EUR-USD:1.31, USD-LKR:126.67, USD-GBP:0.65], LKR, USD LKR-USD:0.00789


import java.util.*;
import java.text.*;
class GetExchangeRateSimple
{
    public static void main(String s[])
    {
        List<ExchangeRate> exchangeRates = new ArrayList<ExchangeRate>();
        exchangeRates.add(new ExchangeRate("USD", "INR", 54.17));
        exchangeRates.add(new ExchangeRate("USD", "JPY", 97.94));
        exchangeRates.add(new ExchangeRate("CNY", "USD", 0.16));
        ExchangeRate dollarToRupeeRate = getExchangeRate(exchangeRates, "USD", "INR");
        System.out.println("Exchange Rate is : " + dollarToRupeeRate);

    }


    public static ExchangeRate getExchangeRate(List<ExchangeRate> exchangeRates, String sourceCurrency, String targetCurrency) {
    }

}
class ExchangeRate {

String targetCurrency;
String baseCurrency;
double rate;

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

@Override
public String toString() {
DecimalFormat df = new DecimalFormat("##.#####");
return baseCurrency + "-" + targetCurrency + ":" + df.format(rate);
}
}