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>
15 changes: 0 additions & 15 deletions src/InsuranceCalculator.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/ifs/IfElseDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ifs;

public class IfElseDemo {

private InsuranceStrategy strategy;


public double calculateInsurance(double income) {
if (income <= 10000) {
strategy = new InsuranceStrategyLow();
return strategy.calculate(income);
} else if (income <= 30000) {
strategy = new InsuranceStrategyMedium();
return strategy.calculate(income);
} else if (income <= 60000) {
strategy = new InsuranceStrategyHigh();
return strategy.calculate(income);
} else {
strategy = new InsuranceStrategyVeryHigh();
return strategy.calculate(income);
}
}
}
13 changes: 13 additions & 0 deletions src/ifs/InsuranceStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ifs;

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();
}
18 changes: 18 additions & 0 deletions src/ifs/InsuranceStrategyHigh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ifs;

public class InsuranceStrategyHigh extends InsuranceStrategy {
@Override
public int getConstant() {
return 76500;
}

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

@Override
public int getAdjustment() {
return 30000;
}
}
18 changes: 18 additions & 0 deletions src/ifs/InsuranceStrategyLow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ifs;

public class InsuranceStrategyLow extends InsuranceStrategy {
@Override
public int getConstant() {
return 0;
}

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

@Override
public int getAdjustment() {
return 0;
}
}
18 changes: 18 additions & 0 deletions src/ifs/InsuranceStrategyMedium.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ifs;

public class InsuranceStrategyMedium extends InsuranceStrategy {
@Override
public int getConstant() {
return 35600;
}

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

@Override
public int getAdjustment() {
return 10000;
}
}
22 changes: 22 additions & 0 deletions src/ifs/InsuranceStrategyVeryHigh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ifs;

public class InsuranceStrategyVeryHigh extends InsuranceStrategy {
public InsuranceStrategyVeryHigh(){

}

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

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

@Override
public int getAdjustment() {
return 60000;
}
}
Empty file removed test/EMPTY
Empty file.
32 changes: 32 additions & 0 deletions test/ifs/IfElseDemoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ifs;

import org.junit.Test;

import static org.junit.Assert.*;

public class IfElseDemoTest {
@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 IfElseDemo().calculateInsurance(income);
}

}