From 6197213781af921dc39fbc67c13e2683c24733b6 Mon Sep 17 00:00:00 2001 From: lakada Date: Tue, 13 Mar 2018 13:31:57 +0200 Subject: [PATCH 1/2] Refactored the if-sentences --- src/rental/RentalPriceCalculator.java | 50 +++++++++++++++++++++------ 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index 43fdddf..b9aaf19 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -8,36 +8,64 @@ public class RentalPriceCalculator { // 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 price(int driverAge, int driversLicenseAge, int carType, boolean accidentsCaused, boolean isHighSeason) { - if (age < 18) { + if (isUnderage(driverAge)) { throw new IllegalArgumentException("Driver too young - cannot quote the price"); } - if (age <= 21 && clazz > 2) { + if (canRentOnlyClassOneCars(driverAge, carType)) { 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) { + double rentalprice = driverAge; + + if (isCarLargeWithYoungDriverDuringHighSeason(driverAge, carType, isHighSeason)) { rentalprice = rentalprice * 2; } - - if (licence < 1) { + + if (isLicenseNew(driversLicenseAge)) { throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!"); } - if (licence < 3) { + if (isLicenseStillFresh(driversLicenseAge)) { rentalprice = rentalprice * 1.3; } - if (acc == true && age < 30) { + if (hasYoungerDriverCausedAccidents(driverAge, accidentsCaused)) { rentalprice += 15; } - if (rentalprice > 1000) { + if (isRentalPriceTooHigh(rentalprice)) { return 1000.00; } return rentalprice; } + + private boolean isRentalPriceTooHigh(double rentalprice) { + return rentalprice > 1000; + } + + private boolean hasYoungerDriverCausedAccidents(int driverAge, boolean accidentsCaused) { + return accidentsCaused && driverAge < 30; + } + + private boolean isLicenseStillFresh(int driversLicenseAge) { + return driversLicenseAge < 3; + } + + private boolean isLicenseNew(int driversLicenseAge) { + return driversLicenseAge < 1; + } + + private boolean isCarLargeWithYoungDriverDuringHighSeason(int driverAge, int carType, boolean isHighSeason) { + return carType >=4 && driverAge <= 25 && isHighSeason; + } + + private boolean canRentOnlyClassOneCars(int driverAge, int carType) { + return driverAge <= 21 && carType > 2; + } + + private boolean isUnderage(int driverAge) { + return driverAge < 18; + } } \ No newline at end of file From 9c06b4b9d5811f212682c60ddc217b61f3887934 Mon Sep 17 00:00:00 2001 From: lakada Date: Tue, 13 Mar 2018 13:39:10 +0200 Subject: [PATCH 2/2] Refactored the if-sentences --- src/rental/RentalPriceCalculator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rental/RentalPriceCalculator.java b/src/rental/RentalPriceCalculator.java index b9aaf19..235c4c4 100644 --- a/src/rental/RentalPriceCalculator.java +++ b/src/rental/RentalPriceCalculator.java @@ -31,7 +31,7 @@ public double price(int driverAge, int driversLicenseAge, int carType, boolean a rentalprice = rentalprice * 1.3; } - if (hasYoungerDriverCausedAccidents(driverAge, accidentsCaused)) { + if (youngerDrivreHasCausedAccidentsInLastYear(driverAge, accidentsCaused)) { rentalprice += 15; } @@ -45,7 +45,7 @@ private boolean isRentalPriceTooHigh(double rentalprice) { return rentalprice > 1000; } - private boolean hasYoungerDriverCausedAccidents(int driverAge, boolean accidentsCaused) { + private boolean youngerDrivreHasCausedAccidentsInLastYear(int driverAge, boolean accidentsCaused) { return accidentsCaused && driverAge < 30; }