Taxi Meter in Java | AskTheCode

Taxi Meter question in Java | #CodeChum | Solutions

Question: It's the middle of the night, you just finished your shift, and the rain is pouring. Luckily, you find a vacant taxi. As you attempt to go in, the driver stops you and tells you that he can't take you anywhere because his taxi meter is broken. Since, you desperately want to go home, you make a deal with the taxi driver. You propose to manually calculate the total fare of your ride, luckily enough the driver trusts you and explains you how the fair is calculated. The taxi has a base fee of P40.00 for the first 250 meters. An additional P2.50 is added for every succeeding 200 meters. Compute and print the total fare that you would need to pay.


Input Format:

A single line containing the total distance traveled.

Input Sample:

250


Output Format:

A single line containing the total cost of the ride.

Output Sample:

P40.00

Code:


import java.util.Scanner;

public class TaxiMeter{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the total distance: ");

int totalDistance = sc.nextInt();

double cost;

if (totalDistance <= 250) {

if (totalDistance == 0) {

cost = 0.00;

System.out.println("Cost= P"+ cost);

}

else{

cost = 40.00;

System.out.println("Cost= P"+ cost);

}

}

else{

totalDistance = totalDistance - 250;

cost = 40;

if (totalDistance % 200 == 0) {

double costPer200Meter = (totalDistance / 200) * 2.50;

cost += costPer200Meter;

System.out.println("Total cost= P"+cost);

}

else{

double costPer200Meter = ((totalDistance / 200) * 2.50) + 2.50;

cost += costPer200Meter;

System.out.println("Total cost= P"+cost);

}

}

}

}



36 views0 comments

Recent Posts

See All

"Believe you can and you're halfway there"