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
76 changes: 54 additions & 22 deletions src/rental/RentalPriceCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,72 @@ 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
// carClass - class of the car from 1 (smallest) to 5 (largest) that person wishes to rent
// lastYearAccidents - 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) {
// isHotSeasonTime - if it is high season or not

//public double price(int age, int licence, int carClass, boolean acc, boolean acc2, boolean season) {

double rentalprice;

public double calculatePrice(int age, int carClass, int licence, boolean isHighSeasonTime, boolean lastYearAccidents) {
if(isLegalToRent(age, licence, carClass)) {
rentalprice = age;
}

applyPriceModifiers(rentalprice, licence, age, lastYearAccidents, carClass, isHighSeasonTime);

if (rentalprice > 1000) {
return 1000.00;
} else {
return rentalprice;
}
}

public double applyPriceModifiers(double rentalprice, int licence, int age, boolean lastYearAccidents, int carClass, boolean isHighSeasonTime) {
rentalprice = checkLicense(licence, rentalprice);
rentalprice = checkAccidents(lastYearAccidents, age, rentalprice);
rentalprice = adaptPriceByAge(rentalprice, age, carClass, isHighSeasonTime);
return rentalprice;
}

public double adaptPriceByAge(double rentalprice, int age, int carClass, boolean isHighSeasonTime) {
if (carClass >=4 && age <= 25 && isHighSeasonTime != false) {
rentalprice = rentalprice * 2;
}
return rentalprice;
}

public boolean isLegalToRent(int age, int licence, int carClass) {
if (age < 18) {
throw new IllegalArgumentException("Driver too young - cannot quote the price");
}
if (age <= 21 && clazz > 2) {
if (age <= 21 && carClass > 1) {
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!");
}

return true;
}

public double checkLicense(int licence, double rentalprice) {
if (licence < 3) {
rentalprice = rentalprice * 1.3;
}

if (acc == true && age < 30) {
rentalprice += 15;
return rentalprice * 1.3;
} else {
return rentalprice;
}

if (rentalprice > 1000) {
return 1000.00;
}

public double checkAccidents(boolean lastYearAccidents, int age, double rentalprice) {
if (lastYearAccidents == true && age < 30) {
return rentalprice + 15;
} else {
return rentalprice;
}
return rentalprice;
}



}
35 changes: 33 additions & 2 deletions test/rental/RentalPriceCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
package rental;

import static org.junit.Assert.fail;
import static 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
calculator = new RentalPriceCalculator();
}

@Test
public void test() {
fail("Not yet implemented");
}

@Test
public void priceHigherForUnexperiencedDriver() {
assertEquals(130, calculator.checkLicense(2, 100), 0.0001);
}

@Test
public void priceNormalFor3YearsOfExperience() {
assertEquals(100, calculator.checkLicense(3, 100), 0.0001);
}

@Test
public void priceNormalFor13YearsOfExperience() {
assertEquals(100, calculator.checkLicense(13, 100), 0.0001);
}

@Test
public void isAge20LegalForFirstCarClass() {
assertEquals(true, calculator.isLegalToRent(20, 2, 1));
}

@Test
public void isAge20LegalForThirdCarClass() {
assertEquals(false, calculator.isLegalToRent(20, 2, 3));
}

@Test
public void isPriceHigherForAge25orLessWhenClass4() {
assertEquals(200, calculator.adaptPriceByAge(100, 25, 4, true), 0.0001);
}
}