Answer to Question #287245 in Java | JSP | JSF for Amul

Question #287245

Question: Peter runs a vehicle rental center. The vehicles are of three types; MICRO, MINI and SUV. The customer who travels in MICRO will pay Rs 15 per kilometer. The customer who travels in MINI will pay Rs 20 per kilometer and the customer whe travel in SUV will pay Rs 25 per kilometer. Write a Program in java to compute the cost of travelling by the customer with following requirements. a) The distance travelled by customer needs to be taken as input. b) The type of vehicle to be taken as input (M for Micro, m for mini, S for SUV). c) Check the type of vehicle and compute the final cost of the travel depending on the vehicle. d) Display the final cost with an appropriate message

1
Expert's answer
2022-01-13T07:47:21-0500


import java.util.Scanner;


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		final int RATE_MICRO = 15;
		final int RATE_MINI = 20;
		final int RATE_SUV = 25;


		double distance;
		double rate = 0;
		double cost;
		int flag = 1;
		char vehicleType;


		while (flag == 1) {
			System.out.printf("Press m for MICRO Vehicle (%d per Km.)", RATE_MICRO);
			System.out.printf("\nPress M for MINI  Vehicle (%d per Km.)", RATE_MINI);
			System.out.printf("\nPress S for SUV   Vehicle (%d per Km.)", RATE_SUV);
			System.out.print("\nSelect the type of vehicle(m/M/S): ");
			vehicleType = keyBoard.nextLine().charAt(0);
			if (vehicleType == 'm') {
				rate = RATE_MICRO;
			}
			if (vehicleType == 'M') {
				rate = RATE_MINI;
			}
			if (vehicleType == 'S') {
				rate = RATE_SUV;
			}


			System.out.print("\n\nEnter the distance travelled (Kms): ");
			distance = keyBoard.nextDouble();
			cost = distance * rate;
			System.out.printf("\nTotal Cost = $ %.2f", cost);
			System.out.printf("\n\nPress 1 to Continue or 0 to EXIT: ");
			flag = keyBoard.nextInt();
			keyBoard.nextLine();
		}


		keyBoard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog