(Parking Charges)A parking garagecharges 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-hourperiod 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. It should use the method calculateCharges to determine the charge for each customer.
public class Main {
public static double[] calculateCharges(double[] hours) {
double[] charges = new double[hours.length];
for (int i = 0; i < hours.length; i++) {
if (hours[i] <= 3) {
charges[i] = 2;
} else if (hours[i] > 18) {
charges[i] = 10;
} else {
charges[i] = 2;
if (hours[i] - (int) hours[i] > 0) {
charges[i] += 0.5;
}
charges[i] += ((int) hours[i] - 3) * 0.5;
}
}
return charges;
}
public static void main(String[] args) {
double[] hours = {24, 2.5, 3, 3.5, 20.5, 21, 20, 19, 19.5, 6, 13, 18};
double total = 0;
double[] charges = calculateCharges(hours);
for (int i = 0; i < charges.length; i++) {
System.out.println("Customer " + (i + 1) + " " + charges[i]);
total += charges[i];
}
System.out.println("Total: " + total);
}
}
Comments
Leave a comment