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
78 changes: 52 additions & 26 deletions src/rental/RentalPriceCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,68 @@

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) {
private final int MIN_AGE = 18;
private final int MAX_RENTAL = 1000;
private final int MIN_LICENSE_YEAR = 1;
private final int UNEXPERIENCED_DRIVER_AGE = 21;
private final int EXPERIENCED_DRIVER_AGE = 30;


public double calc_rental_price(int driverAge, int licence_year, int carToRent, boolean caused_accidents, boolean high_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 = driverAge;

double rentalprice = age;
isAllowedToRent(driverAge,licence_year);
unexperiencedDriverCar(driverAge,carToRent);
rentalPrice = maximumPriceCalc(rentalPrice);
rentalPrice = unExperiencedRenter(rentalPrice,licence_year);
rentalPrice= highSeasonPricing(driverAge, rentalPrice, carToRent, high_season);
rentalPrice = riskyRent(rentalPrice,driverAge, caused_accidents);

if (clazz >=4 && age <= 25 && season != false) {
rentalprice = rentalprice * 2;
}
return rentalPrice;

if (licence < 1) {
throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!");
}
private double highSeasonPricing(int driverAge,double rentalPrice, int carToRent, boolean high_season) {
if (carToRent >=4 && driverAge <= 25 && high_season != false) {
rentalPrice = rentalPrice * 2;
}

if (licence < 3) {
rentalprice = rentalprice * 1.3;
return rentalPrice;
}
private double unExperiencedRenter(double rentalPrice, int license_year) {

if (license_year < 3) {
rentalPrice = rentalPrice * 1.3;
}
return rentalPrice;
}

if (acc == true && age < 30) {
rentalprice += 15;
private double riskyRent(double rentalPrice, int driverAge,boolean caused_accidents) {
if (caused_accidents == true && driverAge < EXPERIENCED_DRIVER_AGE) {
rentalPrice += 15;
}
return rentalPrice;
}


if (rentalprice > 1000) {
private double maximumPriceCalc(double rentalPrice) {
if (rentalPrice > MAX_RENTAL) {
return 1000.00;
}
return rentalprice;
return rentalPrice;
}

private void unexperiencedDriverCar(int driverAge, int carToRent){
if (driverAge <= UNEXPERIENCED_DRIVER_AGE && carToRent > 2)
throw new UnsupportedOperationException("Drivers 21 y/o or less can only rent Class 1 vehicles");
}

private void isAllowedToRent(int driverAge, int licence_year){
if (driverAge < MIN_AGE){
throw new IllegalArgumentException("Driver too young - cannot quote the price");
}
if (licence_year < MIN_LICENSE_YEAR) {
throw new IllegalArgumentException("Driver must hold driving licence at least for one year. Can not rent a car!");
}
}

}
42 changes: 37 additions & 5 deletions test/rental/RentalPriceCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
package rental;

import static org.junit.Assert.fail;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class RentalPriceCalculatorTest {

@Before
public void beforeEachTest() {
// this method is called before each test
this.rentalPriceCalculator = new RentalPriceCalculator();
}

private RentalPriceCalculator rentalPriceCalculator;


@Test (expected = IllegalArgumentException.class)
public void isAllowedToRentTest() {
rentalPriceCalculator.calc_rental_price(16, 0 ,1,false,false);
rentalPriceCalculator.calc_rental_price(22, 0 ,1,false,false);
}

@Test
public void highSeasonPricingTest() {
double price = rentalPriceCalculator.calc_rental_price(24,12,20,false,true);
Assert.assertEquals(48, price, 0.1);
}

@Test
public void unExperiencedRenterTest() {
double price = rentalPriceCalculator.calc_rental_price(20, 2, 2, false, false);
Assert.assertEquals(26,price, 0.1);
}

@Test
public void maximumPriceCalcTest() {
double price = rentalPriceCalculator.calc_rental_price(1210, 100 ,1,false,false);
Assert.assertEquals(1000, price, 0.1);
}

@Test (expected = UnsupportedOperationException.class)
public void unexperiencedDriverCarTest() {
rentalPriceCalculator.calc_rental_price(20, 2 ,3,false,false);
}

@Test
public void test() {
fail("Not yet implemented");
public void riskyRentTest() {
double price = rentalPriceCalculator.calc_rental_price(24, 3 ,1,true,false);
Assert.assertEquals(39,price,0.1);
}
}