diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index 43fdddf..2c751dd 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -1,43 +1,83 @@ 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) { - + public static void main(String[] args){ + RentalPriceCalculator test = new RentalPriceCalculator(); + System.out.println(test.quotePrice(27,5,5,false,false)); + } + + private static void isDriverUnderage(int age) { if (age < 18) { throw new IllegalArgumentException("Driver too young - cannot quote the price"); } - if (age <= 21 && clazz > 2) { + } + + private static void isYoungDriver(int age, int carSize) { + if (age <= 21 && carSize > 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) { + } + + private static double isHighSeason(double rentalPrice, int driverAge, int carSize, boolean highSeason) { + if (carSize >=4 && driverAge <= 25 && highSeason) { + return rentalPrice * 1.5; + } else return rentalPrice; + } + + private static void isNewDriver(int yearsWithDriversLicense) { + if (yearsWithDriversLicense < 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) { + private static double hasLicenceForThreeYears(double rentalPrice, int yearsWithDriversLicense) { + if (yearsWithDriversLicense < 3) { + return rentalPrice * 1.3; + } else return rentalPrice; + } + + private static double hadAccidentsLastYear(double rentalPrice, boolean anyAccidentsCausedLastYear, int driverAge) { + if (anyAccidentsCausedLastYear && driverAge < 30) { + return rentalPrice += 15; + } else return rentalPrice; + } + + private static double maxRentPrice(double rentalPrice) { + if (rentalPrice > 1000) { return 1000.00; - } - return rentalprice; + } else return rentalPrice; + } + + + + /** + * Returns the daily rental price of a car based on input parameters. + * + * @param driverAge age of driver + * @param yearsWithDriversLicense number of full years person holds driving licence + * @param carSize class of the car from 1 (smallest) to 5 (largest) that person wishes to rent + * @param anyAccidentsCausedLastYear has s/he caused any accidents within last year + * @param highSeason if it is high highSeason or not + * @return daily rental price + */ + public double quotePrice(int driverAge, + int yearsWithDriversLicense, + int carSize, + boolean anyAccidentsCausedLastYear, + // boolean involvedInAccidentsLastYear, <-- Not used in this equation + boolean highSeason) { + + double rentalPrice = driverAge; + + isDriverUnderage(driverAge); + isYoungDriver(driverAge, carSize); + isNewDriver(yearsWithDriversLicense); + rentalPrice = isHighSeason(rentalPrice, driverAge, carSize, highSeason); + rentalPrice = hasLicenceForThreeYears(rentalPrice, yearsWithDriversLicense); + rentalPrice = hadAccidentsLastYear(rentalPrice, anyAccidentsCausedLastYear, driverAge); + rentalPrice = maxRentPrice(rentalPrice); + + return rentalPrice; } + } \ No newline at end of file diff --git a/test/rental/RentalPriceCalculatorTest.java b/test/rental/RentalPriceCalculatorTest.java index 6654c4c..a4faf38 100644 --- a/test/rental/RentalPriceCalculatorTest.java +++ b/test/rental/RentalPriceCalculatorTest.java @@ -1,11 +1,11 @@ package rental; -import static org.junit.Assert.fail; - +import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class RentalPriceCalculatorTest { + RentalPriceCalculator test = new RentalPriceCalculator(); @Before public void beforeEachTest() { @@ -13,7 +13,59 @@ public void beforeEachTest() { } @Test - public void test() { - fail("Not yet implemented"); + public void basicTest() { + Assert.assertEquals(test.quotePrice(27,5,5,false,false),27,0.0001); + } + + @Test + public void highSeasonTest() { + Assert.assertEquals(test.quotePrice(24,4,5,false,true),36,0.0001); + } + + @Test + public void youngDriverTest() { + Assert.assertEquals(test.quotePrice(30,1,5,false,true),39,0.0001); } + + @Test + public void maxRentPrice() { + Assert.assertTrue(test.quotePrice(1005,1,5,true,true) <= 1000.00); + } + + @Test + public void accidentTest() { + Assert.assertEquals(test.quotePrice(25,5,5,true,false),40,0.0001); + } + + @Test + public void testDriverAge17() { + try { + test.quotePrice(17,2,3,true, true); + Assert.fail( "Driver is not underage" ); + } catch (IllegalArgumentException expectedException) { + } + } + + @Test + public void testDriverAge20() { + try { + test.quotePrice(20, 2, 3, true, true); + Assert.fail("Driver age not between 18-21"); + } catch (UnsupportedOperationException expectedException) { + } + } + + @Test + public void newDriverTest() { + try { + test.quotePrice(29, 0, 3, true, true); + Assert.fail("Not a new driver (has license for at least a year)"); + } catch (IllegalArgumentException expectedException) { + } + } + + + + + }