Software Engineering

# How to Calculate Transport on Vacation in Java

## The challenge#

After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you.

You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers.

Every day you rent the car costs \$40. If you rent the car for 7 or more days, you get \$50 off your total. Alternatively, if you rent the car for 3 or more days, you get \$20 off your total.

Write a code that gives out the total amount for different days(d).

## The solution in Java code#

Option 1:

``````public class Solution {
public static int rentalCarCost(int d) {
int totalBefore = d * 40;
}
}
``````

Option 2:

``````public class Solution {
private static final int COST_PER_DAY = 40;
public static int rentalCarCost(int d) {
if (d < 3)       return d * COST_PER_DAY;
else if (d >= 7) return d * COST_PER_DAY - 50;
else             return d * COST_PER_DAY - 20;
}
}
``````

Option 3:

``````public class Solution {
public static int rentalCarCost(int d) {
return d < 7 ? d < 3 ? 40 * d : 40 * d - 20 : 40 * d - 50;
}
}
``````

## Test cases to validate our solution#

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RentalCarExampleTests {
@Test
public void under3Tests() {
assertEquals(40, Solution.rentalCarCost(1));
assertEquals(80, Solution.rentalCarCost(2));
}
@Test
public void under7Tests() {
assertEquals(100, Solution.rentalCarCost(3));
assertEquals(140, Solution.rentalCarCost(4));
assertEquals(180, Solution.rentalCarCost(5));
assertEquals(220, Solution.rentalCarCost(6));
}
@Test
public void over7Tests() {
assertEquals(230, Solution.rentalCarCost(7));
assertEquals(270, Solution.rentalCarCost(8));
assertEquals(310, Solution.rentalCarCost(9));
assertEquals(350, Solution.rentalCarCost(10));
}
}
``````