Sunday, April 14, 2024
HomeSoftware EngineeringCalculate Transport on Trip in Java

Calculate Transport on Trip in Java


The problem

After a tough quarter within the workplace you resolve to get some relaxation on a trip. So you’ll e book a flight for you and your girlfriend and attempt to depart all of the mess behind you.

You will want a rental automobile so as so that you can get round in your trip. The supervisor of the automobile rental makes you some good gives.

Day-after-day you lease the automobile prices $40. In case you lease the automobile for 7 or extra days, you get $50 off your complete. Alternatively, if you happen to lease the automobile for 3 or extra days, you get $20 off your complete.

Write a code that provides out the entire quantity for various days(d).

The answer in Java code

Possibility 1:

public class Resolution {
  public static int rentalCarCost(int d) {
    int totalBefore = d * 40;
    if (d>=7) return totalBefore-50;
    if (d>=3) return totalBefore-20;
    return totalBefore;
  }
}

Possibility 2:

public class Resolution {
  non-public static remaining 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;
  }
}

Possibility 3:

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

Check circumstances to validate our answer

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments