diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index 43fdddf..e818e5e 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -2,42 +2,56 @@ 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) { + public void price(int driverAge, int yearsLicenceHeld, int carType, boolean isAccidentsCaused, + boolean isAccidentVictim, boolean isHighSeason) { - if (age < 18) { - throw new IllegalArgumentException("Driver too young - cannot quote the price"); - } - if (age <= 21 && clazz > 2) { - throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); - } - - double rentalprice = age; + isDriverOldEnough(driverAge, carType); + double rentalPrice = driverAge; + isDriverExperiencedEnough(yearsLicenceHeld); + rentalPrice = calculateRentalPrice(driverAge, carType, isAccidentsCaused, isHighSeason, rentalPrice, yearsLicenceHeld); + } + + double calculateRentalPrice(int driverAge, int carType, boolean isAccidentsCaused, boolean isHighSeason, + double rentalPrice, int yearsLicenceHeld) { - if (clazz >=4 && age <= 25 && season != false) { - rentalprice = rentalprice * 2; + if (yearsLicenceHeld < 3) { + rentalPrice = rentalPrice * 1.3; } - if (licence < 1) { - throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); + if (carType >=4 && driverAge <= 25 && isHighSeason(isHighSeason)) { + rentalPrice = rentalPrice * 2; } - if (licence < 3) { - rentalprice = rentalprice * 1.3; + if (isAccidentsCaused == true && driverAge < 30) { + rentalPrice += 15; + } + + if (rentalPrice > 1000) { + rentalPrice = 1000.00; } - if (acc == true && age < 30) { - rentalprice += 15; + return rentalPrice; + } + + boolean isHighSeason(boolean isHighSeason) { + return isHighSeason != false; + } + + void isDriverExperiencedEnough(int yearsLicenceHeld) throws IllegalArgumentException { + if (yearsLicenceHeld < 1) { + throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); } + } - if (rentalprice > 1000) { - return 1000.00; + void isDriverOldEnough(int driverAge, int carType) + throws IllegalArgumentException, UnsupportedOperationException { + if (driverAge < 18) { + throw new IllegalArgumentException("Driver too young - cannot quote the price"); + } + if (driverAge <= 21 && carType > 2) { + throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); } - return rentalprice; } + + } \ No newline at end of file diff --git a/test/rental/RentalPriceCalculatorTest.java b/test/rental/RentalPriceCalculatorTest.java index 6654c4c..0d5e04e 100644 --- a/test/rental/RentalPriceCalculatorTest.java +++ b/test/rental/RentalPriceCalculatorTest.java @@ -1,19 +1,21 @@ package rental; -import static org.junit.Assert.fail; +import static 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 + calculator= new RentalPriceCalculator(); } @Test - public void test() { - fail("Not yet implemented"); + public void maxRentalPrice() { + assertEquals(1000, calculator.calculateRentalPrice(25, 5, false, true, 2000, 3), 0.001); } }