From 1514dc7fab4de313ec85f591c1f75dbe961fb1b1 Mon Sep 17 00:00:00 2001 From: Gavin Neal Date: Thu, 18 Jan 2024 22:17:44 -0500 Subject: [PATCH 1/5] Creating and running test --- .idea/runConfigurations.xml | 10 ++++++++++ RefactoringAssignment.iml | 17 ++++++++++++++++ test/TestInsuranceCalculator.java | 33 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 .idea/runConfigurations.xml create mode 100644 test/TestInsuranceCalculator.java diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/RefactoringAssignment.iml b/RefactoringAssignment.iml index 7e880ab..4049286 100644 --- a/RefactoringAssignment.iml +++ b/RefactoringAssignment.iml @@ -24,5 +24,22 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/TestInsuranceCalculator.java b/test/TestInsuranceCalculator.java new file mode 100644 index 0000000..01587b8 --- /dev/null +++ b/test/TestInsuranceCalculator.java @@ -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); + } +} \ No newline at end of file From 94adcb99f7719473a83bddcb88bb5df34d2e141c Mon Sep 17 00:00:00 2001 From: Gavin Neal Date: Thu, 18 Jan 2024 22:25:25 -0500 Subject: [PATCH 2/5] Extracting methods --- src/InsuranceCalculator.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/InsuranceCalculator.java b/src/InsuranceCalculator.java index 558e0a0..2cc92e0 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -8,8 +8,22 @@ public double calculateInsurance(double income) { } else if (income <= 60000) { return (income-30000)*0.1+76500; } else { - return (income-60000)*0.02+105600; + return getVeryHighInsurance(income); } } + private double getVeryHighInsurance(double income) { + return (income- getAdjustment())* getWeight() + getConstant(); + } + public int getConstant() { + return 105600; + } + + public double getWeight() { + return 0.02; + } + public int getAdjustment() { + return 60000; + } + } From ac6aaa72a61dddc1e0d65ed01081732b247a1d67 Mon Sep 17 00:00:00 2001 From: Gavin Neal Date: Thu, 18 Jan 2024 22:29:43 -0500 Subject: [PATCH 3/5] Using the Extract Delegate refactoring --- src/InsuranceCalculator.java | 19 +++---------------- src/VeryHighInsuranceStrategy.java | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 src/VeryHighInsuranceStrategy.java diff --git a/src/InsuranceCalculator.java b/src/InsuranceCalculator.java index 2cc92e0..57f08bb 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -1,5 +1,7 @@ public class InsuranceCalculator { + private final VeryHighInsuranceStrategy veryHighInsuranceStrategy = new VeryHighInsuranceStrategy(); + public double calculateInsurance(double income) { if (income <= 10000) { return income*0.365; @@ -8,22 +10,7 @@ public double calculateInsurance(double income) { } else if (income <= 60000) { return (income-30000)*0.1+76500; } else { - return getVeryHighInsurance(income); + return veryHighInsuranceStrategy.getVeryHighInsurance(income); } } - - private double getVeryHighInsurance(double income) { - return (income- getAdjustment())* getWeight() + getConstant(); - } - public int getConstant() { - return 105600; - } - - public double getWeight() { - return 0.02; - } - public int getAdjustment() { - return 60000; - } - } diff --git a/src/VeryHighInsuranceStrategy.java b/src/VeryHighInsuranceStrategy.java new file mode 100644 index 0000000..ef1c404 --- /dev/null +++ b/src/VeryHighInsuranceStrategy.java @@ -0,0 +1,20 @@ +public class VeryHighInsuranceStrategy { + public VeryHighInsuranceStrategy() { + } + + public double getVeryHighInsurance(double income) { + return (income - getAdjustment()) * getWeight() + getConstant(); + } + + public int getConstant() { + return 105600; + } + + public double getWeight() { + return 0.02; + } + + public int getAdjustment() { + return 60000; + } +} \ No newline at end of file From 224cbc1d3f7e49359e20b4efb9f12b9e991d7b3d Mon Sep 17 00:00:00 2001 From: Gavin Neal Date: Thu, 18 Jan 2024 23:04:05 -0500 Subject: [PATCH 4/5] Fine tuning --- src/InsuranceCalculator.java | 5 +++-- src/InsuranceStrategy.java | 11 +++++++++++ src/VeryHighInsuranceStrategy.java | 9 ++++----- 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 src/InsuranceStrategy.java diff --git a/src/InsuranceCalculator.java b/src/InsuranceCalculator.java index 57f08bb..d01ad14 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -1,6 +1,6 @@ public class InsuranceCalculator { - private final VeryHighInsuranceStrategy veryHighInsuranceStrategy = new VeryHighInsuranceStrategy(); + 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 veryHighInsuranceStrategy.getVeryHighInsurance(income); + strategy = new VeryHighInsuranceStrategy(); + return strategy.getVeryHighInsurance(income); } } } diff --git a/src/InsuranceStrategy.java b/src/InsuranceStrategy.java new file mode 100644 index 0000000..f0d49dd --- /dev/null +++ b/src/InsuranceStrategy.java @@ -0,0 +1,11 @@ +public abstract class InsuranceStrategy { + public double getVeryHighInsurance(double income) { + return (income - getAdjustment()) * getWeight() + getConstant(); + } + + public abstract int getConstant(); + + public abstract double getWeight(); + + public abstract int getAdjustment(); +} diff --git a/src/VeryHighInsuranceStrategy.java b/src/VeryHighInsuranceStrategy.java index ef1c404..27f613a 100644 --- a/src/VeryHighInsuranceStrategy.java +++ b/src/VeryHighInsuranceStrategy.java @@ -1,19 +1,18 @@ -public class VeryHighInsuranceStrategy { +public class VeryHighInsuranceStrategy extends InsuranceStrategy { public VeryHighInsuranceStrategy() { } - public double getVeryHighInsurance(double income) { - return (income - getAdjustment()) * getWeight() + getConstant(); - } - + @Override public int getConstant() { return 105600; } + @Override public double getWeight() { return 0.02; } + @Override public int getAdjustment() { return 60000; } From 56b1f5a2e6ea4a27ab47e71a66519b08a21026e0 Mon Sep 17 00:00:00 2001 From: Gavin Neal Date: Thu, 18 Jan 2024 23:24:33 -0500 Subject: [PATCH 5/5] Implementing the abstract class --- src/HighInsuranceStrategy.java | 19 +++++++++++++++++++ src/InsuranceCalculator.java | 11 +++++++---- src/InsuranceStrategy.java | 2 +- src/LowInsuranceStrategy.java | 19 +++++++++++++++++++ src/MediumInsuranceStrategy.java | 19 +++++++++++++++++++ 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/HighInsuranceStrategy.java create mode 100644 src/LowInsuranceStrategy.java create mode 100644 src/MediumInsuranceStrategy.java diff --git a/src/HighInsuranceStrategy.java b/src/HighInsuranceStrategy.java new file mode 100644 index 0000000..5cf2e6b --- /dev/null +++ b/src/HighInsuranceStrategy.java @@ -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; + } +} \ No newline at end of file diff --git a/src/InsuranceCalculator.java b/src/InsuranceCalculator.java index d01ad14..2fb800d 100644 --- a/src/InsuranceCalculator.java +++ b/src/InsuranceCalculator.java @@ -4,14 +4,17 @@ public class InsuranceCalculator { 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 { strategy = new VeryHighInsuranceStrategy(); - return strategy.getVeryHighInsurance(income); + return strategy.calculate(income); } } } diff --git a/src/InsuranceStrategy.java b/src/InsuranceStrategy.java index f0d49dd..1ebf362 100644 --- a/src/InsuranceStrategy.java +++ b/src/InsuranceStrategy.java @@ -1,5 +1,5 @@ public abstract class InsuranceStrategy { - public double getVeryHighInsurance(double income) { + public double calculate(double income) { return (income - getAdjustment()) * getWeight() + getConstant(); } diff --git a/src/LowInsuranceStrategy.java b/src/LowInsuranceStrategy.java new file mode 100644 index 0000000..dc0d40d --- /dev/null +++ b/src/LowInsuranceStrategy.java @@ -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; + } +} \ No newline at end of file diff --git a/src/MediumInsuranceStrategy.java b/src/MediumInsuranceStrategy.java new file mode 100644 index 0000000..27c4fd8 --- /dev/null +++ b/src/MediumInsuranceStrategy.java @@ -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; + } +} \ No newline at end of file