diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index 43fdddf..92c0dbb 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -1,43 +1,48 @@ package rental; 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) { - 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; - - if (clazz >=4 && age <= 25 && season != false) { - rentalprice = rentalprice * 2; - } - - if (licence < 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 (acc == true && age < 30) { - rentalprice += 15; - } - - if (rentalprice > 1000) { - return 1000.00; - } - return rentalprice; - } + + private static final int MIN_DRIVER_AGE = 18; + private static final int MIN_HIGH_CLASS_DRIVER_AGE = 21; + private static final int FIRST_HIGH_CLASS_INDEX = 2; + private static final int MIN_LICENSE_AGE = 1; + private static final int LICENCE_PENALTY_AGE = 3; + private static final int MAX_RENTAL_PRICE = 1000; + private static final double LICENCE_PENALTY = 1.3; + private static final int CAR_CLASS_PENALTY = 2; + private static final int ACCIDENT_PENALTY = 15; + private static final int FIRST_LUXURY_CLASS_INDEX = 4; + + public double price(int driverAge, int licenceAge, int carClass, boolean wasAccidentLastYear, boolean isSeasonHigh) { + + if (driverAge < MIN_DRIVER_AGE) { + throw new UnsupportedOperationException("Driver too young - cannot quote the price"); + } + if (driverAge <= MIN_HIGH_CLASS_DRIVER_AGE && carClass > FIRST_HIGH_CLASS_INDEX) { + throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles"); + } + if (licenceAge < MIN_LICENSE_AGE) { + throw new UnsupportedOperationException("Driver must hold driving licence at least for one year. Can not rent a car!"); + } + + double rentalPrice = driverAge; + + if (carClass >= FIRST_LUXURY_CLASS_INDEX && driverAge <= 25 && !isSeasonHigh) { + rentalPrice *= CAR_CLASS_PENALTY; + } + + if (licenceAge < LICENCE_PENALTY_AGE) { + rentalPrice *= LICENCE_PENALTY; + } + + if (wasAccidentLastYear && driverAge < 30) { + rentalPrice += ACCIDENT_PENALTY; + } + + if (rentalPrice > MAX_RENTAL_PRICE) { + return MAX_RENTAL_PRICE; + } + return rentalPrice; + } + } \ No newline at end of file diff --git a/test/rental/RentalPriceCalculatorTest.java b/test/rental/RentalPriceCalculatorTest.java index 6654c4c..284ace6 100644 --- a/test/rental/RentalPriceCalculatorTest.java +++ b/test/rental/RentalPriceCalculatorTest.java @@ -1,19 +1,36 @@ package rental; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertEquals; 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 ifPriceGreaterThanMaxReturnMax() { + assertEquals(1000, calculator.price(29, 2, 5, true, false), 0.1); } + + @Test(expected = UnsupportedOperationException.class) + public void youngerThan18Test() { + calculator.price(1, 1, 1, true, true); + } + + @Test(expected = UnsupportedOperationException.class) + public void illegalDriverClazzTest() { + calculator.price(21, 2, 2, false, true); + } + + @Test(expected = UnsupportedOperationException.class) + public void checkNumberOfFullYearsTest() { + calculator.price(1, 0, 1, true, false); + } }