From b91f2e1ca4c3d752ac817c41f32596ce1216d327 Mon Sep 17 00:00:00 2001 From: sander541 Date: Thu, 15 Mar 2018 09:51:23 +0200 Subject: [PATCH 1/2] Set constants --- src/rental/RentalPriceCalculator.java | 28 +++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index 43fdddf..ae9a87a 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -2,40 +2,48 @@ public class RentalPriceCalculator { + private final int MIN_AGE = 18; + private final int MAX_RENTAL = 1000; + private final int MIN_LICENSE_YEAR = 1; + private final int YOUNG_DRIVER = 21; + + // 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) { + public double calc_rental_price(int age, int licence_year, int car_to_rent, boolean caused_accidents, boolean season) { - if (age < 18) { + double rental_price = age; + + if (age < MIN_AGE) { throw new IllegalArgumentException("Driver too young - cannot quote the price"); } - if (age <= 21 && clazz > 2) { + if (age <= YOUNG_DRIVER && car_to_rent > 2) { throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); } - double rentalprice = age; - if (clazz >=4 && age <= 25 && season != false) { - rentalprice = rentalprice * 2; + + if (car_to_rent >=4 && age <= 25 && season != false) { + rental_price = rental_price * 2; } - if (licence < 1) { + if (licence_year < 1) { throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); } - if (licence < 3) { + if (licence_year < 3) { rentalprice = rentalprice * 1.3; } - if (acc == true && age < 30) { + if (caused_accidents == true && age < 30) { rentalprice += 15; } - if (rentalprice > 1000) { + if (rentalprice > MAX_RENTAL) { return 1000.00; } return rentalprice; From ce885ee2e7be34ee88aa75721cae98367d208cb0 Mon Sep 17 00:00:00 2001 From: sander541 Date: Fri, 16 Mar 2018 16:30:00 +0200 Subject: [PATCH 2/2] Refaktooritud ja testitud kood --- src/rental/RentalPriceCalculator.java | 78 +++++++++++++--------- test/rental/RentalPriceCalculatorTest.java | 42 ++++++++++-- 2 files changed, 85 insertions(+), 35 deletions(-) diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index ae9a87a..dea093c 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -5,47 +5,65 @@ public class RentalPriceCalculator { private final int MIN_AGE = 18; private final int MAX_RENTAL = 1000; private final int MIN_LICENSE_YEAR = 1; - private final int YOUNG_DRIVER = 21; + private final int UNEXPERIENCED_DRIVER_AGE = 21; + private final int EXPERIENCED_DRIVER_AGE = 30; - - // 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 calc_rental_price(int age, int licence_year, int car_to_rent, boolean caused_accidents, boolean season) { - - double rental_price = age; - - if (age < MIN_AGE) { - throw new IllegalArgumentException("Driver too young - cannot quote the price"); - } - if (age <= YOUNG_DRIVER && car_to_rent > 2) { - throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); - } + + public double calc_rental_price(int driverAge, int licence_year, int carToRent, boolean caused_accidents, boolean high_season) { + double rentalPrice = driverAge; + isAllowedToRent(driverAge,licence_year); + unexperiencedDriverCar(driverAge,carToRent); + rentalPrice = maximumPriceCalc(rentalPrice); + rentalPrice = unExperiencedRenter(rentalPrice,licence_year); + rentalPrice= highSeasonPricing(driverAge, rentalPrice, carToRent, high_season); + rentalPrice = riskyRent(rentalPrice,driverAge, caused_accidents); - if (car_to_rent >=4 && age <= 25 && season != false) { - rental_price = rental_price * 2; - } + return rentalPrice; - if (licence_year < 1) { - throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); + } + private double highSeasonPricing(int driverAge,double rentalPrice, int carToRent, boolean high_season) { + if (carToRent >=4 && driverAge <= 25 && high_season != false) { + rentalPrice = rentalPrice * 2; } - - if (licence_year < 3) { - rentalprice = rentalprice * 1.3; + return rentalPrice; + } + private double unExperiencedRenter(double rentalPrice, int license_year) { + + if (license_year < 3) { + rentalPrice = rentalPrice * 1.3; } + return rentalPrice; + } - if (caused_accidents == true && age < 30) { - rentalprice += 15; + private double riskyRent(double rentalPrice, int driverAge,boolean caused_accidents) { + if (caused_accidents == true && driverAge < EXPERIENCED_DRIVER_AGE) { + rentalPrice += 15; } + return rentalPrice; + } + - if (rentalprice > MAX_RENTAL) { + private double maximumPriceCalc(double rentalPrice) { + if (rentalPrice > MAX_RENTAL) { return 1000.00; } - return rentalprice; + return rentalPrice; } + + private void unexperiencedDriverCar(int driverAge, int carToRent){ + if (driverAge <= UNEXPERIENCED_DRIVER_AGE && carToRent > 2) + throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); + } + + private void isAllowedToRent(int driverAge, int licence_year){ + if (driverAge < MIN_AGE){ + throw new IllegalArgumentException("Driver too young - cannot quote the price"); + } + if (licence_year < MIN_LICENSE_YEAR) { + throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); + } + } + } \ No newline at end of file diff --git a/test/rental/RentalPriceCalculatorTest.java b/test/rental/RentalPriceCalculatorTest.java index 6654c4c..a64b366 100644 --- a/test/rental/RentalPriceCalculatorTest.java +++ b/test/rental/RentalPriceCalculatorTest.java @@ -1,7 +1,6 @@ package rental; -import static org.junit.Assert.fail; - +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -9,11 +8,44 @@ public class RentalPriceCalculatorTest { @Before public void beforeEachTest() { - // this method is called before each test + this.rentalPriceCalculator = new RentalPriceCalculator(); } + + private RentalPriceCalculator rentalPriceCalculator; + + @Test (expected = IllegalArgumentException.class) + public void isAllowedToRentTest() { + rentalPriceCalculator.calc_rental_price(16, 0 ,1,false,false); + rentalPriceCalculator.calc_rental_price(22, 0 ,1,false,false); + } + + @Test + public void highSeasonPricingTest() { + double price = rentalPriceCalculator.calc_rental_price(24,12,20,false,true); + Assert.assertEquals(48, price, 0.1); + } + + @Test + public void unExperiencedRenterTest() { + double price = rentalPriceCalculator.calc_rental_price(20, 2, 2, false, false); + Assert.assertEquals(26,price, 0.1); + } + + @Test + public void maximumPriceCalcTest() { + double price = rentalPriceCalculator.calc_rental_price(1210, 100 ,1,false,false); + Assert.assertEquals(1000, price, 0.1); + } + + @Test (expected = UnsupportedOperationException.class) + public void unexperiencedDriverCarTest() { + rentalPriceCalculator.calc_rental_price(20, 2 ,3,false,false); + } + @Test - public void test() { - fail("Not yet implemented"); + public void riskyRentTest() { + double price = rentalPriceCalculator.calc_rental_price(24, 3 ,1,true,false); + Assert.assertEquals(39,price,0.1); } }