A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. • Write an application that calculates and displays the parking charges for each customer who parked in the garage yesterday. You should enter the hours parked for each customer. The program should display the charge for the current customer and should calculate and display the running total of yesterday's receipts. The program should use the method calculateCharges to determine the charge for each customer
package q182284;
import java.util.Scanner;
public class Q182284 {
/***
* Main method
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double []customerHours=new double[3];
for (int customer = 1; customer <= 3; customer++) {
System.out.print("Enter customer " + customer + " parking hours: ");
customerHours[customer-1]=input.nextDouble();
}
System.out.println(String.format("\n%-10s%-10s%-10s","Cars","Hours","Charges"));
double totalCharges=0;
double totalHours=0;
for(int i=0;i<customerHours.length;i++){
double charges=calculateCharge(customerHours[i]);
System.out.println(String.format("%-10d%-10.2f%-10.2f",(i+1),customerHours[i],charges));
totalCharges+=charges;
totalHours+=customerHours[i];
}
System.out.println(String.format("%-10s%-10.2f%-10.2f","Total",totalHours,totalCharges));
}
/**
* Calculates charge
* @param hours
* @return
*/
static double calculateCharge(double hours) {
double currentHours = hours;
double charge = 2.0;
if (currentHours > 0) {
if (currentHours <= 3) {
return charge;
} else if (currentHours <= 24) {
while (currentHours > 3) {
charge += 0.5;
currentHours--;
if (charge >= 10) {
charge = 10;
}
}
}
return charge;
}
System.out.println("No car parks for longer than 24 hours at a time.");
return 0;
}
}
Comments
Leave a comment