@Override
int getDiscountRate(float priceThisYear, float priceLastYear) {
float totalPrice;
totalPrice = priceThisYear + priceLastYear;
if (totalPrice >= 250 && totalPrice < 350) {
return 5;
} else if (totalPrice >= 350 && totalPrice < 450) {
return 6;
} else if (totalPrice >= 450 && totalPrice < 550) {
return 7;
} else if (totalPrice >= 550 && totalPrice < 650) {
return 8;
} else if (totalPrice >= 650 && totalPrice < 750) {
return 9;
} else if (totalPrice >= 750 && totalPrice < 850) {
return 10;
} else if (totalPrice >= 850 && totalPrice < 950) {
return 11;
} else if (totalPrice >= 950 && totalPrice < 1050) {
return 12;
} else if (totalPrice >= 1050 && totalPrice < 1150) {
return 13;
} else if (totalPrice >= 1150 && totalPrice < 1250) {
return 14;
} else if (totalPrice >= 1250 && totalPrice < 1350) {
return 15;
} else if (totalPrice >= 1350 && totalPrice < 1450) {
return 16;
} else if (totalPrice >= 1450 && totalPrice < 1550) {
return 17;
} else if (totalPrice >= 1550 && totalPrice < 1650) {
return 18;
} else if (totalPrice >= 1650 && totalPrice < 1750) {
return 19;
} else if (totalPrice >= 1750 && totalPrice < 1850) {
return 20;
}
else if (totalPrice > 1850){
return 20;
}
return 0;
}
Trying to make the code look neater, preferably by decreasing the number of else ifs and maybe implementing some form of loop.
The idea is to have a discount rate which starts at 5 percent when the user has spent over $350 and then a further 1 percent with every $100 spent giving a maximum discount of 20 percent
I understand to experienced programmers this is most likely trivial. The program itself works but I know that can't be the best way to code it.