Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 63 additions & 38 deletions src/rental/RentalPriceCalculator.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,68 @@
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;
}
// driverAge - age of driver
// driverLicence - number of full years person holds driving license
// carClass - class of the car from 1 (smallest) to 5 (largest) that person wishes to rent
// causedAccident - has s/he caused any accidents within last year
// beenInAccidents - has s/he participated (but not caused) in any accidents within last year
// isHighSeason - if it is high season or not

if (rentalprice > 1000) {
return 1000.00;
}
return rentalprice;
public double getRentalPrice(int driverAge, int driverLicenceAge, int carClass, boolean isHighSeason, boolean causedAccident) {
if (isEligibleToRent(driverAge, carClass, driverLicenceAge)) {
return rentalPriceCalculator(carClass, driverAge, isHighSeason, driverLicenceAge, causedAccident);
} else {
return 0;
}
}
}
public double rentalPriceCalculator(int carClass, int driverAge, boolean isHighSeason, int driverLicence, boolean causedAccident){
double rentalPrice = driverAge;
if (isCarClassHighModifier(carClass,driverAge,isHighSeason)) {
rentalPrice = rentalPrice * 2;
}
if (isDriversLicenceModifier(driverLicence)) {
rentalPrice = rentalPrice * 1.3;
}
if (isCausedAccidentModifier(causedAccident,driverAge)) {
rentalPrice += 15;
}
if (isRentalPriceOverThousand(rentalPrice)) {
return 1000;
}
return rentalPrice;
}
private boolean isDriversLicenceModifier(int driverLicenceAge) {
return driverLicenceAge < 3;
}
private boolean isCausedAccidentModifier(boolean causedAccident, int driverAge) {
return causedAccident && driverAge < 30;
}
private boolean isRentalPriceOverThousand(double rentalPrice) {
return rentalPrice > 1000;
}
private boolean isCarClassHighModifier( int carClass, int driverAge, boolean highSeason) {
return carClass >=4 && driverAge <= 25 && highSeason;
}
public boolean isAdult(int driverAge) {
if (driverAge < 18) {
throw new IllegalArgumentException("Driver too young - cannot quote the price");
} else {
return true;
}
}
public boolean isDriverLicenseValid( int driverLicenceAge) {
if (driverLicenceAge < 1) {
throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!");
} else {
return true;
}
}
public void canOnlyRentClassOne(int driverAge, int carClass) {
if (driverAge <= 21 && carClass > 2) {
throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles");
}
}
public boolean isEligibleToRent (int driverAge, int carClass, int driverLicenceAge) {
return isAdult(driverAge) && isDriverLicenseValid(driverLicenceAge);
}
}
23 changes: 17 additions & 6 deletions test/rental/RentalPriceCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package rental;

import static org.junit.Assert.fail;

import 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
this.calculator = new RentalPriceCalculator();
}

@Test
public void test() {
fail("Not yet implemented");
public void canRentCarLegalAge() {
calculator.isEligibleToRent(18,1,2);
}
@Test
public void cantRentCarIllegalAge() {
calculator.isEligibleToRent(16, 1, 1);
}
@Test
public void calculatorGivesCorrectPrice(){
Assert.assertEquals(calculator.getRentalPrice(24,2,3,true,
true),46.2,0);
}
@Test

}