# 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:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```public class Solution {
public static int rentalCarCost(int d) {
int totalBefore = d * 40;
}
}
```Code language: Java (java)```

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;
}
}
```Code language: Java (java)```

Option 3:

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

## 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));
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of