# [SOLVED] How to create a large scale money conversion algorithm in java

```
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.
```

# Issue

I was just trying to create this small android app in android studio to convert currency.

I used 2 `Spinner` objects to hold only 3 values (USD, POUND, EURO) :

``````                if(actualType.equals("USD")){
if(wantedType.equals("POUND")){
montantConv = montantNonConv * 0.87;
}
else if(wantedType.equals("EURO")){
montantConv = montantNonConv;
}
}
else if(actualType.equals("POUND")){
if(wantedType.equals("EURO")){
montantConv = montantNonConv * 1.15;
}
else if(wantedType.equals("USD")){
montantConv = montantNonConv * 1.15;
}
}
else if(actualType.equals("EURO")){
if(wantedType.equals("USD")){
montantConv = montantNonConv * 1;
}
else if(wantedType.equals("POUND")){
montantConv = montantNonConv * 0.87;
}
}
``````

With if-else the code is too long for a combination of only 3 choices (input + output).

i was just wondering is there a better algorithm to do this ? How does the online ones do it that have 50+ currencies to chose from ?

# Solution

As you are finding out, putting names of the currencies in the conversion logic is not a good idea. The same applies with the conversion values. You should want to write code to minimize things like that. You want the actual conversion logic coded without explicitly knowing (“hard coding”) the names of the currencies or their exchange rates.

Rather than have something like `actualType.equals("POUND")` in your code, you want to try to have code that has each value represented by a variable: `actualType.equals(otherType)`.

In real life, your data would come from an external source, such as a text file, a database, or another computer. In your small demo app, you might want to have the data load when the program starts, and be stored using array(s) and or (a) `Collection` object(s). At this time, you would also load the values in the `Spinner` objects, with the same data.

You might think about whether it is worth having another class or not:

``````class Money {
String name;
double conversionValue;

Money (String name, double value) {
this.name = name;
conversionValue = value;
}

``````

And so on. (Note: I omitted visibility specifiers.)

In your conversion app, you could simulate the external source with an array:

`````` Money [] market = { new Money ("Euro", 1.00), new Money ("USD", 1.00),
new Money ("Pound", 1.15), new Money ("Yen", 0.007),
new Money ("Rupee", 0.0125), ... };
``````

An alternative to creating another `class` to glue a currency and exchange factor is to use a `Map <String, Double>`, with the name of the currency as the key and the exchange factor as the value.

So, the conversion app might have a method like this:

``````public double convertMoney (String selling, int amt, String buying)
{ ...
``````

Depending on how the names and values were stored, it would use the string values to look up 2 conversion values. The exchange rate would be conversion value associated with `selling` divided by the conversion value associated with `buying`.

Answered By – Old Dog Programmer