Premium Calculator is a homework project where requirement is to create a calculator that calculates premium for given policy.
- Java 14
Use PremiumCalculatorFactory
to retrieve PremiumCalculator
for provided Currency
.
Currency
is needed to set scale for BigDecimal used for currency operations.
Currency currency = Currency.getInstance("EUR");
PremiumCalculator premiumCalculator = PremiumCalculatorFactory.getPremiumCalculator(currency);
To calculate premium provide or build Policy
object and use PremiumCalculator#calculate(Policy policy)
.
Policy policy = Policy.builder()
.withPolicyNumber("LV20-02-100000-5")
.withStatus(PolicyStatus.APPROVED)
.withPolicyObject(PolicyObject.builder()
.withObjectName("A House")
.withPolicySubObject(PolicySubObject.builder()
.withName("TV")
.withRiskType(RiskType.FIRE)
.withSumInsured(500.00)
.build())
.withPolicySubObject(PolicySubObject.builder()
.withName("TV")
.withRiskType(RiskType.THEFT)
.withSumInsured(102.51)
.build())
.build())
.build();
BigDecimal premium = premiumCalculator.calculate(policy);
Overall calculation logic is following:
- Grouping all policy sub-objects by risk type
- Summing the sum insured for grouped sub-objects
- Applying coefficient based on risk type
- Summing premiums from every risk type
In order to apply this logic calculation is split into two types: policy-based and risk-based calculation.
Policy-based calculation is handled by PremiumCalculatior
which groups policy sub-objects by risk type, aggregates their sum insured, and applying risk-based logic on it.
Risk-based calculation is handled by RiskCalculator
that applies a different coefficient based on a given sum insured.
In order to add new RiskCalculaor
following steps should be done:
- Create new risk calculator class by implementing
RiskCalculator
interface - Add new risk type into
RiskType
enum - Add newly created risk calculator to
RiskCalculatorFactory
Adding new risk type into
RiskType
enum without adding risk calculator to the factory with trigger compile error. This will ensure thatRiskCalculator
will be added as well.