How to avoid too many if condition when the return type is dynamic?
I understand that we can create a hashmap with key and value if the condition and the value is static too avoid too many if conditions.
In the below example the price for each month is different from customer to
customer…i need to add 12 if statements for the same…assume if the field is something
other than the month…they i may endup with more than 100 or 200 if’s…ex: for year…1900 to 2000
Example,
public String getPrice(int Month) {
String price = "";
switch (month) {
case 1:
price = customerTable.getJanPrice();
break;
case 2:
price = customerTable.getFebPrice();
break;
...........
}
return price;
}
Could you please help me out with your suggestions/comments?
Thanks,
Kathir
Create an Enum with the prices that you want.
Then to access that price:
EDIT
Because the data is changed at runtime and their are an ‘infinite’ amount of possibilities using Classes would be a more manageable option.