I am writing a little program which converts all kind of units into other ones. I have the gui and the program working fine but I feel like there is a better way to do it since I have a lot of if statements and switches. For example if the user wants to convert from one currency to another, he choses both currencies with a dropbox. Lets call them fromCurrency
and toCurrency
. solution
will be the result at the end and amount
is the amount of money which should be converted. The code for the calculation looks like this:
double convertIt(String fromCurrency, String toCurrency, double amount, double solution)
switch (fromCurrency) {
case "Euro":
if(toCurrency == "US-Dollar"){
solution = amount*(1.2407);
}
if(toCurrency == "Canadian Dollar"){
solution = amount*(1.5492);
}
// ...
// ... checking all possible currencies in which you could convert, then next case
I know this might be pretty basic for most of you but I am really working alot on learning java for a while now and want to understand how to solve problems efficient and elegant. Because of that I would apprecciate any kind of hints on how to solve this problem more efficient since it doesnt feel that way, or at least it doesnt feel elegant. For example 10 currencies would mean 10 switches with 9 if statements each and there will probably be more