From 63b4f866efff18d961a6a8a7784ea37695d855f3 Mon Sep 17 00:00:00 2001 From: Kristjan Erik Pedak Date: Wed, 21 Mar 2018 16:00:06 +0200 Subject: [PATCH 1/2] Final --- src/rental/RentalPriceCalculator.java | 41 +++++++++++----------- test/rental/RentalPriceCalculatorTest.java | 1 + 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index 43fdddf..fddf122 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -2,42 +2,41 @@ public class RentalPriceCalculator { - // age - age of driver - // licence - number of full years person holds driving licence - // clazz - class of the car from 1 (smallest) to 5 (largest) that person wishes to rent - // acc - has s/he caused any accidents within last year - // acc2 - has s/he participated (but not caused) in any accidents within last year - // season - if it is high season or not - public double price(int age, int licence, int clazz, boolean acc, boolean acc2, boolean season) { - - if (age < 18) { + // driverAge - age of driver + // driverLicence - number of full years person holds driving license + // carClass - class of the car from 1 (smallest) to 5 (largest) that person wishes to rent + // causedAccident - has s/he caused any accidents within last year + // beenInAccidents - has s/he participated (but not caused) in any accidents within last year + // isHighSeason - if it is high season or not + public double getPrice(int driverAge, int driverLicence, int carClass, boolean causedAccident, boolean beenInAccidents, + boolean isHighSeason) { + if (driverAge < 18) { throw new IllegalArgumentException("Driver too young - cannot quote the price"); } - if (age <= 21 && clazz > 2) { + if (driverAge <= 21 && carClass > 2) { throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); } - double rentalprice = age; + double rentalPrice = driverAge; - if (clazz >=4 && age <= 25 && season != false) { - rentalprice = rentalprice * 2; + if (carClass >=4 && driverAge <= 25 && isHighSeason) { + rentalPrice = rentalPrice * 2; } - - if (licence < 1) { + if (driverLicence < 1) { throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); } - if (licence < 3) { - rentalprice = rentalprice * 1.3; + if (driverLicence < 3) { + rentalPrice = rentalPrice * 1.3; } - if (acc == true && age < 30) { - rentalprice += 15; + if (causedAccident && driverAge < 30) { + rentalPrice += 15; } - if (rentalprice > 1000) { + if (rentalPrice > 1000) { return 1000.00; } - return rentalprice; + return rentalPrice; } } \ No newline at end of file diff --git a/test/rental/RentalPriceCalculatorTest.java b/test/rental/RentalPriceCalculatorTest.java index 6654c4c..3b7de5f 100644 --- a/test/rental/RentalPriceCalculatorTest.java +++ b/test/rental/RentalPriceCalculatorTest.java @@ -1,5 +1,6 @@ package rental; +import static com.sun.tools.internal.ws.wsdl.parser.Util.fail; import static org.junit.Assert.fail; import org.junit.Before; From 0c2c820cadb1323878334dc34f0b388a23048483 Mon Sep 17 00:00:00 2001 From: Kristjan Erik Pedak Date: Wed, 21 Mar 2018 19:57:09 +0200 Subject: [PATCH 2/2] Final --- src/rental/RentalPriceCalculator.java | 88 ++++++++++++++-------- test/rental/RentalPriceCalculatorTest.java | 24 ++++-- 2 files changed, 74 insertions(+), 38 deletions(-) diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index fddf122..d6e12ab 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -1,42 +1,68 @@ package rental; public class RentalPriceCalculator { - // driverAge - age of driver // driverLicence - number of full years person holds driving license // carClass - class of the car from 1 (smallest) to 5 (largest) that person wishes to rent // causedAccident - has s/he caused any accidents within last year // beenInAccidents - has s/he participated (but not caused) in any accidents within last year // isHighSeason - if it is high season or not - public double getPrice(int driverAge, int driverLicence, int carClass, boolean causedAccident, boolean beenInAccidents, - boolean isHighSeason) { - if (driverAge < 18) { - throw new IllegalArgumentException("Driver too young - cannot quote the price"); - } - if (driverAge <= 21 && carClass > 2) { - throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); - } - - double rentalPrice = driverAge; - - if (carClass >=4 && driverAge <= 25 && isHighSeason) { - rentalPrice = rentalPrice * 2; - } - if (driverLicence < 1) { - throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); - } - - if (driverLicence < 3) { - rentalPrice = rentalPrice * 1.3; - } - - if (causedAccident && driverAge < 30) { - rentalPrice += 15; - } - if (rentalPrice > 1000) { - return 1000.00; - } - return rentalPrice; + public double getRentalPrice(int driverAge, int driverLicenceAge, int carClass, boolean isHighSeason, boolean causedAccident) { + if (isEligibleToRent(driverAge, carClass, driverLicenceAge)) { + return rentalPriceCalculator(carClass, driverAge, isHighSeason, driverLicenceAge, causedAccident); + } else { + return 0; + } } -} \ No newline at end of file + public double rentalPriceCalculator(int carClass, int driverAge, boolean isHighSeason, int driverLicence, boolean causedAccident){ + double rentalPrice = driverAge; + if (isCarClassHighModifier(carClass,driverAge,isHighSeason)) { + rentalPrice = rentalPrice * 2; + } + if (isDriversLicenceModifier(driverLicence)) { + rentalPrice = rentalPrice * 1.3; + } + if (isCausedAccidentModifier(causedAccident,driverAge)) { + rentalPrice += 15; + } + if (isRentalPriceOverThousand(rentalPrice)) { + return 1000; + } + return rentalPrice; + } + private boolean isDriversLicenceModifier(int driverLicenceAge) { + return driverLicenceAge < 3; + } + private boolean isCausedAccidentModifier(boolean causedAccident, int driverAge) { + return causedAccident && driverAge < 30; + } + private boolean isRentalPriceOverThousand(double rentalPrice) { + return rentalPrice > 1000; + } + private boolean isCarClassHighModifier( int carClass, int driverAge, boolean highSeason) { + return carClass >=4 && driverAge <= 25 && highSeason; + } + public boolean isAdult(int driverAge) { + if (driverAge < 18) { + throw new IllegalArgumentException("Driver too young - cannot quote the price"); + } else { + return true; + } + } + public boolean isDriverLicenseValid( int driverLicenceAge) { + if (driverLicenceAge < 1) { + throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); + } else { + return true; + } + } + public void canOnlyRentClassOne(int driverAge, int carClass) { + if (driverAge <= 21 && carClass > 2) { + throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); + } + } + public boolean isEligibleToRent (int driverAge, int carClass, int driverLicenceAge) { + return isAdult(driverAge) && isDriverLicenseValid(driverLicenceAge); + } +} diff --git a/test/rental/RentalPriceCalculatorTest.java b/test/rental/RentalPriceCalculatorTest.java index 3b7de5f..b044d13 100644 --- a/test/rental/RentalPriceCalculatorTest.java +++ b/test/rental/RentalPriceCalculatorTest.java @@ -1,20 +1,30 @@ package rental; -import static com.sun.tools.internal.ws.wsdl.parser.Util.fail; -import static org.junit.Assert.fail; - +import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class RentalPriceCalculatorTest { - + + private RentalPriceCalculator calculator; @Before public void beforeEachTest() { - // this method is called before each test + this.calculator = new RentalPriceCalculator(); } @Test - public void test() { - fail("Not yet implemented"); + public void canRentCarLegalAge() { + calculator.isEligibleToRent(18,1,2); + } + @Test + public void cantRentCarIllegalAge() { + calculator.isEligibleToRent(16, 1, 1); } + @Test + public void calculatorGivesCorrectPrice(){ + Assert.assertEquals(calculator.getRentalPrice(24,2,3,true, + true),46.2,0); + } + @Test + }