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
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

10 changes: 10 additions & 0 deletions RefactoringAssignment.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
20 changes: 14 additions & 6 deletions src/InsuranceCalculator.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
public class InsuranceCalculator {

private calculate strategy;

public double calculateInsurance(double income) {


if (income <= 10000) {
return income*0.365;
strategy = new InsuranceStrategyLow();
return strategy.calculateInsuranceVeryHigh(income);
} else if (income <= 30000) {
return (income-10000)*0.2+35600;
strategy = new InsuranceStrategyMedium();
return strategy.calculateInsuranceVeryHigh(income);
} else if (income <= 60000) {
return (income-30000)*0.1+76500;
strategy = new InsuranceStrategyHigh();
return strategy.calculateInsuranceVeryHigh(income);
} else {
return (income-60000)*0.02+105600;
strategy = new InsuranceStrategyVeryHigh();
return strategy.calculateInsuranceVeryHigh(income);
}
}

}
}
}
16 changes: 16 additions & 0 deletions src/InsuranceStrategyHigh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class InsuranceStrategyHigh extends calculate {
@Override
public int getConstant() {
return 78500;
}

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

@Override
public int getAdjustment() {
return 50000;
}
}
16 changes: 16 additions & 0 deletions src/InsuranceStrategyLow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class InsuranceStrategyLow extends calculate {
@Override
public int getConstant() {
return 1825;
}

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

@Override
public int getAdjustment() {
return 5000;
}
}
16 changes: 16 additions & 0 deletions src/InsuranceStrategyMedium.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class InsuranceStrategyMedium extends calculate {
@Override
public int getConstant() {
return 38600;
}

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

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

public double calculateInsuranceVeryHigh(double income) {
return (income - getAdjustment()) * getWeight() + getConstant();
}

public int getConstant() {
return 105600;
}

public double getWeight() {
return 0.02;
}

public int getAdjustment() {
return 60000;
}
}
12 changes: 12 additions & 0 deletions src/calculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public abstract class calculate {

public double calculateInsuranceVeryHigh(double income) {
return (income - getAdjustment()) * getWeight() + getConstant();
}

public abstract int getConstant();

public abstract double getWeight();

public abstract int getAdjustment();
}
Empty file removed test/EMPTY
Empty file.
27 changes: 27 additions & 0 deletions test/TestDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import org.junit.Test;
import static org.junit.Assert.*;
public class TestDemo {
@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(100_000), 0.01);
}

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