added solution with tariff options as enum and map
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.pfefferminzia.petinsurance.tariffs;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public enum AgeTariff {
|
||||
UP_TO_TWO(2, new BigDecimal("0.2")), UP_TO_FIVE(5, new BigDecimal("0.25")), UP_TO_SEVEN(7,
|
||||
new BigDecimal("0.28")), OVER_SEVEN(Integer.MAX_VALUE, new BigDecimal("0.3"));
|
||||
|
||||
private final int maxAge;
|
||||
|
||||
private final BigDecimal factor;
|
||||
|
||||
private AgeTariff(int maxAge, BigDecimal factor) {
|
||||
this.maxAge = maxAge;
|
||||
this.factor = factor;
|
||||
}
|
||||
|
||||
public static BigDecimal getFactorForAge(int age) {
|
||||
for (AgeTariff rate : values()) {
|
||||
if (age <= rate.maxAge) {
|
||||
return rate.factor;
|
||||
}
|
||||
}
|
||||
throw new AssertionError("Unexpected age: " + age);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user