# Thread: HELP!! Parking Garage program

1. Member
Join Date
Mar 2017
Posts
4
Rep Power
0

## HELP!! Parking Garage program

I'm really new to Java!

This is the assignment:
A parking garage has 5 customers daily. The garage charges a \$5.00 minimum fee to park up to two hours. The garage charges an additional \$1.00 per hour for each hour (or part of an hour) over two hours. The maximum charge for any given day is \$12.00. All cars are gone by midnight. Write a program to calculate and print a summary of the charges for a day. For input the program will read the hours of usage for each of the 5 cars.

Java Code:
```package parkinggarage;

import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.ArrayList;

public class ParkingGarage {

public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
ArrayList<Double> hoursList = new ArrayList<Double>();
ArrayList<Double> costList = new ArrayList<Double>();
DecimalFormat dfHours = new DecimalFormat("#.0");
DecimalFormat dfCharge = new DecimalFormat("#.00");

int i;
double sumHours;
double sumCost;
double hours;
double totalCost;
double totalCharge;
double totalHours;

for (i = 1; i < 6; i++) {
System.out.print("Enter the hours parked for car " + i + ": ");
hours = stdin.nextDouble();

totalCost = 5;
if (hours > 2) {
hours = hours - 2;
totalCost = (totalCost + hours);
if (hours > 12) {
totalCost = 12;
}
}

}

sumHours = 0;
for ( double num1 : hoursList) {
sumHours = sumHours + num1;
}

sumCost = 0;
for (double num2 : costList) {
sumCost = sumCost + num2;
}

totalHours = sumHours;
totalCharge = sumCost;

System.out.println("Total Hours " + dfHours.format(totalHours));
System.out.println("Total Charge \$" + dfCharge.format(totalCharge));

}

}```

2. ## Re: HELP!! Parking Garage program

how to get the total charge
What does the program print out? What should it print out?

3. Member
Join Date
Mar 2017
Posts
4
Rep Power
0

## Re: HELP!! Parking Garage program

Originally Posted by Norm
What does the program print out? What should it print out?
This is what it should print out (underlined values are entered by the user):

Enter the hours parked for car 1: 1.5
Enter the hours parked for car 2: 4.1
Enter the hours parked for car 3: 12.0
Enter the hours parked for car 4: 9.3
Enter the hours parked for car 5: 0.5

Total Hours 27.4
Total Charge \$42.00

This is what my code prints out:

Enter the hours parked for car 1: 1.5
Enter the hours parked for car 2: 4.1
Enter the hours parked for car 3: 12.0
Enter the hours parked for car 4: 9.3
Enter the hours parked for car 5: 0.5

Total Hours 27.4
Total Charge \$34.40 // this is not the value that should print out, I don't how to fix it

4. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: HELP!! Parking Garage program

First off, the requirements say:
"The garage charges an additional \$1.00 per hour for each hour (or part of an hour) over two hours."
In your code you are adding the hours (-2) to the total, but you need to round up to the next whole hour.

That explains some of your missing money.

I also don't think your check for \$12 (the maximum amount) is correct.
You need to check the totalAmount, and reduce it to \$12 if it's greater than that, before adding it to the array.
Currently your check is against the hours, which isn't what the requirements say.

5. Member
Join Date
Mar 2017
Posts
4
Rep Power
0

## Re: HELP!! Parking Garage program

Thanks so much!! I made these changes to my for loop. The output is is correct now!
Java Code:
```        totalCost = 5;
roundHours = (float) Math.ceil(hours);
if (roundHours > 2) {
roundHours = roundHours - 2;
totalCost = (totalCost + roundHours);
}
if (totalCost > 12) {
totalCost = 12;
}

6. ## Re: HELP!! Parking Garage program

(totalCost + roundHours);
That makes an interesting expression. When does adding a cost (say in USDs) to the time in hours make sense???

7. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: HELP!! Parking Garage program

Well, when each hour always costs \$1...
:)

8. Member
Join Date
Mar 2017
Posts
4
Rep Power
0

Thanks Tolls