From bb865e0fc6a0d90bb0c35f771e34a178d5c525a0 Mon Sep 17 00:00:00 2001 From: Justyn Fox Date: Wed, 17 Jan 2024 20:56:27 -0500 Subject: [PATCH 1/5] Creating and running test --- RefactoringAssignment.iml | 10 ++++++++++ test/EMPTY | 0 test/InsuranceCalculatorTest.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) delete mode 100644 test/EMPTY create mode 100644 test/InsuranceCalculatorTest.java diff --git a/RefactoringAssignment.iml b/RefactoringAssignment.iml index 7e880ab..454125c 100644 --- a/RefactoringAssignment.iml +++ b/RefactoringAssignment.iml @@ -24,5 +24,15 @@ + + + + + + + + + + \ No newline at end of file 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..9a0b197 --- /dev/null +++ b/test/InsuranceCalculatorTest.java @@ -0,0 +1,29 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +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(100_000), 0.01); + } + + private double insuranceFor(double income) { + return new InsuranceCalculator().calculateInsurance(income); + } +} From d47cdf35fe98ca9188acd9949ebefc48a7891afe Mon Sep 17 00:00:00 2001 From: Justyn Fox Date: Wed, 17 Jan 2024 21:04:05 -0500 Subject: [PATCH 2/5] Extracting methods --- src/InsuranceCalculator.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/InsuranceCalculator.java b/src/InsuranceCalculator.java index 558e0a0..0b34e3d 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -8,8 +8,24 @@ public double calculateInsurance(double income) { } else if (income <= 60000) { return (income-30000)*0.1+76500; } else { - return (income-60000)*0.02+105600; + return calculateInsuranceVeryHigh(income); } } + private static double calculateInsuranceVeryHigh(double income) { + return (income - getAdjustment()) * getWeight() + getConstant(); + } + + private static int getConstant() { + return 105600; + } + + private static double getWeight() { + return 0.02; + } + + private static int getAdjustment() { + return 60000; + } + } From ba5fa77b88e394301d53cfa61f9fdc5b72984a34 Mon Sep 17 00:00:00 2001 From: Justyn Fox Date: Wed, 17 Jan 2024 21:18:33 -0500 Subject: [PATCH 3/5] Using the Extract Delegate refactoring --- .idea/misc.xml | 2 +- src/InsuranceCalculator.java | 20 +++----------------- src/InsuranceStrategyVeryHigh.java | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 src/InsuranceStrategyVeryHigh.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 0792e77..537356d 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 0b34e3d..151abfd 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -1,5 +1,7 @@ public class InsuranceCalculator { + private final InsuranceStrategyVeryHigh insuranceStrategyVeryHigh = new InsuranceStrategyVeryHigh(); + public double calculateInsurance(double income) { if (income <= 10000) { return income*0.365; @@ -8,24 +10,8 @@ public double calculateInsurance(double income) { } else if (income <= 60000) { return (income-30000)*0.1+76500; } else { - return calculateInsuranceVeryHigh(income); + return InsuranceStrategyVeryHigh.calculateInsuranceVeryHigh(income); } } - private static double calculateInsuranceVeryHigh(double income) { - return (income - getAdjustment()) * getWeight() + getConstant(); - } - - private static int getConstant() { - return 105600; - } - - private static double getWeight() { - return 0.02; - } - - private static int getAdjustment() { - return 60000; - } - } diff --git a/src/InsuranceStrategyVeryHigh.java b/src/InsuranceStrategyVeryHigh.java new file mode 100644 index 0000000..f6b06a9 --- /dev/null +++ b/src/InsuranceStrategyVeryHigh.java @@ -0,0 +1,22 @@ +public class InsuranceStrategyVeryHigh { + + public InsuranceStrategyVeryHigh() { + + } + + static double calculateInsuranceVeryHigh(double income) { + return (income - getAdjustment()) * getWeight() + getConstant(); + } + + static int getConstant() { + return 105600; + } + + static double getWeight() { + return 0.02; + } + + static int getAdjustment() { + return 60000; + } +} \ No newline at end of file From 77167ea405fc30b068968ebec9e5071be029fcd9 Mon Sep 17 00:00:00 2001 From: Justyn Fox Date: Wed, 17 Jan 2024 21:41:06 -0500 Subject: [PATCH 4/5] Fine tuning --- src/InsuranceCalculator.java | 5 +++-- src/InsuranceStrategy.java | 17 +++++++++++++++++ src/InsuranceStrategyVeryHigh.java | 17 +---------------- 3 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 src/InsuranceStrategy.java diff --git a/src/InsuranceCalculator.java b/src/InsuranceCalculator.java index 151abfd..1dd1d3b 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -1,6 +1,6 @@ public class InsuranceCalculator { - private final InsuranceStrategyVeryHigh insuranceStrategyVeryHigh = new InsuranceStrategyVeryHigh(); + private InsuranceStrategy strategy; public double calculateInsurance(double income) { if (income <= 10000) { @@ -10,7 +10,8 @@ public double calculateInsurance(double income) { } else if (income <= 60000) { return (income-30000)*0.1+76500; } else { - return InsuranceStrategyVeryHigh.calculateInsuranceVeryHigh(income); + strategy = new InsuranceStrategyVeryHigh(); + return InsuranceStrategy.calculateInsuranceVeryHigh(income); } } diff --git a/src/InsuranceStrategy.java b/src/InsuranceStrategy.java new file mode 100644 index 0000000..0b8f1ba --- /dev/null +++ b/src/InsuranceStrategy.java @@ -0,0 +1,17 @@ +public abstract class InsuranceStrategy { + public static double calculateInsuranceVeryHigh(double income) { + return (income - InsuranceStrategy.getAdjustment()) * InsuranceStrategy.getWeight() + InsuranceStrategy.getConstant(); + } + + static int getConstant() { + return 105600; + } + + static double getWeight() { + return 0.02; + } + + static int getAdjustment() { + return 60000; + } +} diff --git a/src/InsuranceStrategyVeryHigh.java b/src/InsuranceStrategyVeryHigh.java index f6b06a9..591707e 100644 --- a/src/InsuranceStrategyVeryHigh.java +++ b/src/InsuranceStrategyVeryHigh.java @@ -1,22 +1,7 @@ -public class InsuranceStrategyVeryHigh { +public class InsuranceStrategyVeryHigh extends InsuranceStrategy { public InsuranceStrategyVeryHigh() { } - static double calculateInsuranceVeryHigh(double income) { - return (income - getAdjustment()) * getWeight() + getConstant(); - } - - static int getConstant() { - return 105600; - } - - static double getWeight() { - return 0.02; - } - - static int getAdjustment() { - return 60000; - } } \ No newline at end of file From d5be9b780a78bbe0f6888bd50cc56c0fa1bf7a96 Mon Sep 17 00:00:00 2001 From: Justyn Fox Date: Wed, 17 Jan 2024 22:05:36 -0500 Subject: [PATCH 5/5] Implementing the abstract class --- src/InsuranceStrategy.java | 6 +++--- src/InsuranceStrategyHigh.java | 2 ++ src/InsuranceStrategyLow.java | 2 ++ src/InsuranceStrategyMedium.java | 2 ++ 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 src/InsuranceStrategyHigh.java create mode 100644 src/InsuranceStrategyLow.java create mode 100644 src/InsuranceStrategyMedium.java diff --git a/src/InsuranceStrategy.java b/src/InsuranceStrategy.java index 0b8f1ba..621cc38 100644 --- a/src/InsuranceStrategy.java +++ b/src/InsuranceStrategy.java @@ -3,15 +3,15 @@ public static double calculateInsuranceVeryHigh(double income) { return (income - InsuranceStrategy.getAdjustment()) * InsuranceStrategy.getWeight() + InsuranceStrategy.getConstant(); } - static int getConstant() { + public static int getConstant() { return 105600; } - static double getWeight() { + public static double getWeight() { return 0.02; } - static int getAdjustment() { + public static int getAdjustment() { return 60000; } } diff --git a/src/InsuranceStrategyHigh.java b/src/InsuranceStrategyHigh.java new file mode 100644 index 0000000..404f980 --- /dev/null +++ b/src/InsuranceStrategyHigh.java @@ -0,0 +1,2 @@ +public class InsuranceStrategyHigh extends InsuranceStrategy { +} diff --git a/src/InsuranceStrategyLow.java b/src/InsuranceStrategyLow.java new file mode 100644 index 0000000..973bb9a --- /dev/null +++ b/src/InsuranceStrategyLow.java @@ -0,0 +1,2 @@ +public class InsuranceStrategyLow extends InsuranceStrategy { +} diff --git a/src/InsuranceStrategyMedium.java b/src/InsuranceStrategyMedium.java new file mode 100644 index 0000000..e4c2f05 --- /dev/null +++ b/src/InsuranceStrategyMedium.java @@ -0,0 +1,2 @@ +public class InsuranceStrategyMedium extends InsuranceStrategy { +}