Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions RefactoringAssignment.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,22 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="testng">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/testng/testng/7.1.0/testng-7.1.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/beust/jcommander/1.72/jcommander-1.72.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/inject/guice/4.1.0/guice-4.1.0-no_aop.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/javax/inject/javax.inject/1/javax.inject-1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/aopalliance/aopalliance/1.0/aopalliance-1.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/19.0/guava-19.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/yaml/snakeyaml/1.21/snakeyaml-1.21.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="bluecatcode.junit.4.12.extended" level="project" />
<orderEntry type="library" name="junit" level="project" />
</component>
</module>
19 changes: 19 additions & 0 deletions src/HighInsuranceStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class HighInsuranceStrategy extends InsuranceStrategy {
public HighInsuranceStrategy() {
}

@Override
public int getConstant() {
return 76500;
}

@Override
public double getWeight() {
return 0.1;
}

@Override
public int getAdjustment() {
return 30000;
}
}
15 changes: 10 additions & 5 deletions src/InsuranceCalculator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
public class InsuranceCalculator {

private InsuranceStrategy strategy;

public double calculateInsurance(double income) {
if (income <= 10000) {
return income*0.365;
strategy = new LowInsuranceStrategy();
return strategy.calculate(income);
} else if (income <= 30000) {
return (income-10000)*0.2+35600;
strategy = new MediumInsuranceStrategy();
return strategy.calculate(income);
} else if (income <= 60000) {
return (income-30000)*0.1+76500;
strategy = new HighInsuranceStrategy();
return strategy.calculate(income);
} else {
return (income-60000)*0.02+105600;
strategy = new VeryHighInsuranceStrategy();
return strategy.calculate(income);
}
}

}
11 changes: 11 additions & 0 deletions src/InsuranceStrategy.java
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 19 additions & 0 deletions src/LowInsuranceStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class LowInsuranceStrategy extends InsuranceStrategy {
public LowInsuranceStrategy() {
}

@Override
public int getConstant() {
return 0;
}

@Override
public double getWeight() {
return 0.365;
}

@Override
public int getAdjustment() {
return 0;
}
}
19 changes: 19 additions & 0 deletions src/MediumInsuranceStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class MediumInsuranceStrategy extends InsuranceStrategy {
public MediumInsuranceStrategy() {
}

@Override
public int getConstant() {
return 35600;
}

@Override
public double getWeight() {
return 0.2;
}

@Override
public int getAdjustment() {
return 10000;
}
}
19 changes: 19 additions & 0 deletions src/VeryHighInsuranceStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class VeryHighInsuranceStrategy extends InsuranceStrategy {
public VeryHighInsuranceStrategy() {
}

@Override
public int getConstant() {
return 105600;
}

@Override
public double getWeight() {
return 0.02;
}

@Override
public int getAdjustment() {
return 60000;
}
}
33 changes: 33 additions & 0 deletions test/TestInsuranceCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import org.junit.Assert;
import org.junit.Test;


public class TestInsuranceCalculator{
@Test
public void calculateInsurance() {
Assert.assertNotNull(insuranceFor(30000));
}

@Test
public void testLow() {
Assert.assertEquals(1825, insuranceFor(5000), 0.01);
}

@Test
public void testMedium() { Assert.assertEquals(38600, insuranceFor(25000), 0.01);
}

@Test
public void testHigh() {
Assert.assertEquals(78500, insuranceFor(50000), 0.01);
}

@Test
public void testVeryHigh() {
Assert.assertEquals(106400, insuranceFor(100_000), 0.01);
}

private double insuranceFor(double income) {
return new InsuranceCalculator().calculateInsurance(income);
}
}