diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index 43fdddf..7b44f0c 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -1,43 +1,79 @@ 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 final int ADULT_AGE = 18; + private final int CAR_CLASS_FOUR = 4; + private final int MAX_YOUTH_AGE = 25; + private final int YOUNG_ADULT = 21; + private final double PRICE_RATE_FIFTY = 1.5; + private final String MESSAGES_DRIVER_TOO_YOUNG = "Driver too young - cannot quote the price"; + private final String YOUTH_DRIVE_MESSAGES = "Drivers 21 y/o or less can only rent Class 1 vehicles"; + private final double PRICE_RATE_THIRTY = 1.3; + private final int CAR_LICENCE_THREE_YEARS = 3; + private final int MAX_PRICE = 1000; + private final int ADULT_AGE_THIRTY = 30; + private final int FIFTEEN_EUROS = 15; + private final int CAR_CLASS_TWO = 2; + private final int CAR_LICENCE_MIN_YEAR = 1; + private final String ONE_YEAR_LICENCE_MESSAGES = "Driver must hold driving licence at least for one year. Can not rent a car!"; + + public double calculatePrice(int age, int licence, int carClass, boolean hasCausedAccidents, boolean isHighSeason) { + double rentalPrice = age; + + isUnderageDriver(age); + isYoungDriver(age, carClass); + rentalPrice = doublePrice(age, carClass, isHighSeason, rentalPrice); + isUnderageDriver(licence); + oneYearLicence(licence); + rentalPrice = higherPrice(licence, rentalPrice); + rentalPrice = hasCausedAccidents(age, hasCausedAccidents, rentalPrice); + + if (isMaxRentalPrice(rentalPrice)) { + return MAX_PRICE; + } + return rentalPrice; + } + + private void isUnderageDriver(int age) { + if (age < ADULT_AGE) { + throw new IllegalArgumentException(MESSAGES_DRIVER_TOO_YOUNG); + } + } + + private void isYoungDriver(int age, int carClass) { + if (age <= YOUNG_ADULT && carClass > CAR_CLASS_TWO) { + throw new UnsupportedOperationException(YOUTH_DRIVE_MESSAGES); + } + } + + private boolean isMaxRentalPrice(double rentalPrice) { + return rentalPrice > MAX_PRICE; + } + + private double doublePrice(int age, int carClass, boolean isHighSeason, double rentalPrice) { + if (carClass >= CAR_CLASS_FOUR && age <= MAX_YOUTH_AGE && isHighSeason) { + rentalPrice = rentalPrice * PRICE_RATE_FIFTY; + } + return rentalPrice; + } + + private void oneYearLicence (int licence){ + if(licence <= CAR_LICENCE_MIN_YEAR) { + throw new IllegalArgumentException(ONE_YEAR_LICENCE_MESSAGES);} + } + + private double higherPrice(int licence, double rentalPrice) { + if (licence < CAR_LICENCE_THREE_YEARS) { + rentalPrice = rentalPrice * PRICE_RATE_THIRTY; + } + return rentalPrice; + } + + private double hasCausedAccidents(int age, boolean hasCausedAccidents, double rentalPrice) { + if (hasCausedAccidents && age < ADULT_AGE_THIRTY) { + rentalPrice += FIFTEEN_EUROS; + } + return rentalPrice; + } } \ No newline at end of file diff --git a/test/rental/RentalPriceCalculatorTest.java b/test/rental/RentalPriceCalculatorTest.java index 6654c4c..d3486b0 100644 --- a/test/rental/RentalPriceCalculatorTest.java +++ b/test/rental/RentalPriceCalculatorTest.java @@ -1,19 +1,60 @@ 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 rentalPriceCalculator; + @Before public void beforeEachTest() { - // this method is called before each test + this.rentalPriceCalculator = new RentalPriceCalculator(); + } + + @Test(expected = IllegalArgumentException.class) + public void testThatUnderageDriverCannotDrive() { + rentalPriceCalculator.calculatePrice(16, 0, 1, false, false); + } + + @Test(expected = IllegalArgumentException.class) + public void testThatDriverEdgeCaseWithLegalCar() { + double price = rentalPriceCalculator.calculatePrice(18, 1, 1, false, false); + assertEquals( 18.0, price, 0.01); + } + + @Test(expected = IllegalArgumentException.class) + public void testThatAdultCanDriveACar() { + double price = rentalPriceCalculator.calculatePrice(30, 5, 2, false, false); + assertEquals(30.0, price, 0.01); + } + + @Test(expected = IllegalArgumentException.class) + public void testThatYouthCanRentOnlyFirstClass() { + double price = rentalPriceCalculator.calculatePrice(20, 5, 2, false, false); + } + + @Test(expected = IllegalArgumentException.class) + public void testThatPriceDoesNotExceedMax() { + double price = rentalPriceCalculator.calculatePrice(25, 5, 2, true, true); + + } + + @Test(expected = IllegalArgumentException.class) + public void testThatPriceIsDoubleWhenItISHighSeason() { + double price = rentalPriceCalculator.calculatePrice(24, 5, 2, false, true); + assertEquals(36.0, price, 0.01); + } + + @Test(expected = IllegalArgumentException.class) + public void testThatRentIsPermittedWhenLicenceISLessThan1YearOld() { + double price = rentalPriceCalculator.calculatePrice(24, 0, 2, false, false); } - @Test - public void test() { - fail("Not yet implemented"); + @Test(expected = IllegalArgumentException.class) + public void testThatPriceIsHigherWhenDriverHasCausedAccidents() { + double price = rentalPriceCalculator.calculatePrice(24, 5, 2, true, false); + assertEquals(39.0, price, 0.01); } }