Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A parking garage charges a $2.00 minimum fee to park for up to three hours. The

ID: 3529945 • Letter: A

Question

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

Explanation / Answer

import java.util.Scanner;

public class ParkingGarage
{
public static void main( String[] argv )
{
Scanner keyboard= new Scanner(System.in);
double total = 0;
int i = 0; //tracks how many reciepts filled

//get and handle inputs
while(true)
{
//get input
System.out.print( "Enter hours parked, or "done": " );
String input = keyboard.nextLine();
if( input.equalsIgnoreCase("done") ) break;

//validate input
if( !input.matches("\d*") )
{
System.out.println( "Invalid input. " );
continue;
}

//process input and display current status
i++;
double crnt = calculateCharges( Integer.parseInt(input) );
total += crnt;
System.out.printf("Reciept %d: $%.2f for %s hours, total cost so far $%.2f " , i , crnt , input , total );
}

System.out.printf( " %d cars, total cost $%.2f " , i , total );
}


public static double calculateCharges( int hours )
{
if( hours == 0 ) return 0; //no charge if you didn't park
if( hours <= 3 ) return 2; //flat rate under 3 hours
if( hours > 19 ) return 10; //max rate is $10
return 2 + (hours-3)*0.5; //variable rate from $2 to $10
}
} ----- Sample Session -----

Enter hours parked, or "done": 1
Reciept 1: $2.00 for 1 hours, total cost so far $2.00

Enter hours parked, or "done": 2
Reciept 2: $2.00 for 2 hours, total cost so far $4.00

Enter hours parked, or "done": 3
Reciept 3: $2.00 for 3 hours, total cost so far $6.00

Enter hours parked, or "done": 4
Reciept 4: $2.50 for 4 hours, total cost so far $8.50

Enter hours parked, or "done": 10
Reciept 5: $5.50 for 10 hours, total cost so far $14.00

Enter hours parked, or "done": 18
Reciept 6: $9.50 for 18 hours, total cost so far $23.50

Enter hours parked, or "done": 19
Reciept 7: $10.00 for 19 hours, total cost so far $33.50

Enter hours parked, or "done": 20
Reciept 8: $10.00 for 20 hours, total cost so far $43.50

Enter hours parked, or "done": 100
Reciept 9: $10.00 for 100 hours, total cost so far $53.50

Enter hours parked, or "done": 10.3
Invalid input.

Enter hours parked, or "done": Marlboro
Invalid input.

Enter hours parked, or "done": done

9 cars, total cost $53.50
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote