I'm writing a small Android application that deals with payments and, in my function that handles transactions, I need to translate a number of installments (1-12) and whether or not it has interest (0-1) into the de facto objects for those of the SDK I'm using.
Right now I have a switch that looks like the following:
InstalmentTransactionEnum install = InstalmentTransactionEnum.ONE_INSTALMENT;
//TODO: Implement interest
switch (installments) {
case "1":
install = InstalmentTransactionEnum.ONE_INSTALMENT;
break;
case "2":
install = InstalmentTransactionEnum.TWO_INSTALMENT_NO_INTEREST;
break;
case "3":
install = InstalmentTransactionEnum.THREE_INSTALMENT_NO_INTEREST;
break;
case "4":
install = InstalmentTransactionEnum.FOUR_INSTALMENT_NO_INTEREST;
break;
case "5":
install = InstalmentTransactionEnum.FIVE_INSTALMENT_NO_INTEREST;
break;
case "6":
install = InstalmentTransactionEnum.SIX_INSTALMENT_NO_INTEREST;
break;
case "7":
install = InstalmentTransactionEnum.SEVEN_INSTALMENT_NO_INTEREST;
break;
case "8":
install = InstalmentTransactionEnum.EIGHT_INSTALMENT_NO_INTEREST;
break;
case "9":
install = InstalmentTransactionEnum.NINE_INSTALMENT_NO_INTEREST;
break;
case "10":
install = InstalmentTransactionEnum.TEN_INSTALMENT_NO_INTEREST;
break;
case "11":
install = InstalmentTransactionEnum.ELEVEN_INSTALMENT_NO_INTEREST;
break;
case "12":
install = InstalmentTransactionEnum.TWELVE_INSTALMENT_NO_INTEREST;
break;
}
stoneTransaction.setInstalmentTransactionEnum(install);
Now this is without me having implemented interest which in this method would require another massive switch statement where install
would be equal to TWO_INSTALLMENT_WITH_INTEREST
and so on.
My thinking is that there must be a smarter way of doing this that doesn't involve an if statement to check the interest
variable (a boolean) and the massive switch for installments
. What is the best way of achieving this?
enum.valueOf(String)
effectively will be useful as a general programmer, even if it doesn't help this particular scenario. \$\endgroup\$