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
98 changes: 69 additions & 29 deletions src/rental/RentalPriceCalculator.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
60 changes: 56 additions & 4 deletions test/rental/RentalPriceCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,71 @@
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() {
// this method is called before each test
}

@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) {
}
}





}