mercredi 28 juin 2017

C# Currency Converter: Reduce the amount of if statements

So far I have created a Currency Converter that will convert between multiple different currencies however, to convert the currency, I have used 4 if statements per currency (so I can convert between all of the different currencies.) This has resulted in 20 if statements which doesn't seem very clean or efficient. Here is an example of my code:

if (currencyFrom == "B" && currencyTo == "U")
{
    currencyConversion = doubleInput * 1.29;
}
if (currencyFrom == "B" && currencyTo == "E")
{
    currencyConversion = doubleInput * 1.14;
}
if (currencyFrom == "B" && currencyTo == "J")
{
    currencyConversion = doubleInput * 145.10;
}
if (currencyFrom == "B" && currencyTo == "C")
{
    currencyConversion = doubleInput * 1.68;
}

Is there a better way to do the same calculations but without as many if statements?

Thanks for your help,

Josh

Aucun commentaire:

Enregistrer un commentaire