diff --git a/.idea/misc.xml b/.idea/misc.xml index 0792e77..c56f193 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/InsuranceCalculator.java b/src/InsuranceCalculator.java index 558e0a0..d136920 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -1,15 +1,24 @@ +import java.lang.Math; + public class InsuranceCalculator { + private InsuranceStrategy strategy; + public double calculateInsurance(double income) { - if (income <= 10000) { - return income*0.365; - } else if (income <= 30000) { - return (income-10000)*0.2+35600; - } else if (income <= 60000) { - return (income-30000)*0.1+76500; - } else { - return (income-60000)*0.02+105600; + InsuranceStrategy[] hashBrown = new InsuranceStrategy[6]; + hashBrown[0] = new InsuranceStrategyLow(); + hashBrown[1] = new InsuranceStrategyMedium(); + hashBrown[2] = new InsuranceStrategyMedium(); + hashBrown[3] = new InsuranceStrategyHigh(); + hashBrown[4] = new InsuranceStrategyHigh(); + hashBrown[5] = new InsuranceStrategyHigh(); + int deviousNum = (int)Math.floor(Math.abs(income / 10000 - 0.1)); + try { + strategy = hashBrown[deviousNum]; + return strategy.calculate(income); + } catch(ArrayIndexOutOfBoundsException error) { + strategy = new InsuranceStrategyVeryHigh(); + return strategy.calculate(income); } } - } diff --git a/src/InsuranceStrategy.java b/src/InsuranceStrategy.java new file mode 100644 index 0000000..1ebf362 --- /dev/null +++ b/src/InsuranceStrategy.java @@ -0,0 +1,11 @@ +public abstract class InsuranceStrategy { + public double calculate(double income) { + return (income - getAdjustment()) * getWeight() + getConstant(); + } + + public abstract int getConstant(); + + public abstract double getWeight(); + + public abstract int getAdjustment(); +} diff --git a/src/InsuranceStrategyHigh.java b/src/InsuranceStrategyHigh.java new file mode 100644 index 0000000..e316b15 --- /dev/null +++ b/src/InsuranceStrategyHigh.java @@ -0,0 +1,16 @@ +public class InsuranceStrategyHigh extends InsuranceStrategy { + @Override + public int getConstant() { + return 76500; + } + + @Override + public double getWeight() { + return 0.1; + } + + @Override + public int getAdjustment() { + return 30000; + } +} diff --git a/src/InsuranceStrategyLow.java b/src/InsuranceStrategyLow.java new file mode 100644 index 0000000..58278e8 --- /dev/null +++ b/src/InsuranceStrategyLow.java @@ -0,0 +1,16 @@ +public class InsuranceStrategyLow extends InsuranceStrategy { + @Override + public int getConstant() { + return 0; + } + + @Override + public double getWeight() { + return 0.365; + } + + @Override + public int getAdjustment() { + return 0; + } +} diff --git a/src/InsuranceStrategyMedium.java b/src/InsuranceStrategyMedium.java new file mode 100644 index 0000000..6994a01 --- /dev/null +++ b/src/InsuranceStrategyMedium.java @@ -0,0 +1,16 @@ +public class InsuranceStrategyMedium extends InsuranceStrategy { + @Override + public int getConstant() { + return 35600; + } + + @Override + public double getWeight() { + return 0.2; + } + + @Override + public int getAdjustment() { + return 10000; + } +} diff --git a/src/InsuranceStrategyVeryHigh.java b/src/InsuranceStrategyVeryHigh.java new file mode 100644 index 0000000..1a71ba1 --- /dev/null +++ b/src/InsuranceStrategyVeryHigh.java @@ -0,0 +1,16 @@ +public class InsuranceStrategyVeryHigh extends InsuranceStrategy { + @Override + public int getConstant() { + return 105600; + } + + @Override + public double getWeight() { + return 0.02; + } + + @Override + public int getAdjustment() { + return 60000; + } +} diff --git a/test/EMPTY b/test/EMPTY deleted file mode 100644 index e69de29..0000000 diff --git a/test/InsuranceCalculatorTest.java b/test/InsuranceCalculatorTest.java new file mode 100644 index 0000000..54f8a32 --- /dev/null +++ b/test/InsuranceCalculatorTest.java @@ -0,0 +1,44 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class InsuranceCalculatorTest { + @Test + public void low() { + assertEquals( + 1825, + insuranceFor(5000), + 0.01 + ); + } + + @Test + public void medium() { + assertEquals( + 38600, + insuranceFor(25000), + 0.01 + ); + } + + @Test + public void high() { + assertEquals( + 78500, + insuranceFor(50000), + 0.01 + ); + } + + @Test + public void veryHigh() { + assertEquals( + 106400, + insuranceFor(100000), + 0.01 + ); + } + + private double insuranceFor(double income) { + return new InsuranceCalculator().calculateInsurance(income); + } +} \ No newline at end of file