I’m working on a class project and I cannot seem to figure out what to do next. The instructions read:
• it contains a public method named getFxRate that returns a double meant to represent the exchange rate between the currency codes passed in as the first and second String arguments. The first String is the source or “in” currency, whereas the second is the destination or “out” currency.
I know i’m supposed to use that space to find out how to call one of the rates from the array, but i’m not sure how to do it.
This class will be linked to an html page, where the user will pick two currencies and then type an amount in, so getting the class to read which option they chose is another story
public class fxDataModel {
/* create array for currency codes */
private static final String[] codes = {"CAD", "EUR", "GBP", "USD"};
/* create and populate 2d array for conversion */
private static final double[][] rates =
{ {1.0, 0.624514066, 0.588714763, 0.810307 },
{1.601244959, 1.0, 0.942676548, 1.2975},
{1.698615463, 1.060809248, 1.0, 1.3764},
{1.234100162, 0.772200772, 0.726532984, 1.0} };
public String[] getFxCurrencies(){
return codes;
}
public double getFxRate (final int inCode, final int outCode){
int inCurr, outCurr;
double rate;
for (inCurr = 0; inCurr < 4; inCurr++){
String inCurrency = codes[inCurr];
if (inCode.equals(inCurrency)) {
break;
}
}
for (outCurr = 0; outCurr < 4; outCurr++){
String outCurrency = codes[outCurr];
if (outCode.equals(outCurrency)) {
break;
}
}
rate = rates[inCurr][outCurr];
return rate;
}
}
EDIT: this is the hint my professor gave us
the prototype for getFxRate is
public double getFxRate( String inCurrency, String outCurrency )
So given the inCurrency ( for example, “USD” ) and the outCurrency ( for example, “CAD” ), the method getFxRate looks in the array F/X rates and returns the one that would translate USD to CAD. Now, the array of F/X rates is double[][], that is a two dimensional array. In my hypothetical example, the value at [ 3 ][ 0 ] answers the question as to which rate in the rates array converts USD -> CAD. That’s because in the currencies array which is String[], CAD exists at the 0th position and USD exist at the 3rd. So given inCurrency and outCurrency, find there indices in the String[] array of currencies and use those indexes in the rates array to return that one single value. It’s pretty straightforward Java to loop through the array of Strings to find the matching currency and it’s associated index. That’s Java I stuff and I leave that to you, but do think loops.
Currency rates are given in pairs. For example, the CADEUR rate gives you how many EUR (euros) are required to buy 1 CAD (Canadian dollar).
Lets consider a truncated
ratestable:So if your user picks CAD and CAD as the two inputs, you return 1. If your user picks CAD and USD, you return 0.81.
I think the problem is to take the two currency codes and to find the correct row and column number in your
ratestable and return the appropriate rate.EDIT: I would loop through the currencies array and find the index of the two currency codes given. I would use these two indexes to look up what the exchange rate should be.