Results 1 to 8 of 8
Thread: HELP!! Parking Garage program
- 03-13-2017, 01:20 AM #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.
I can't figure out how to get the total charge for this program. Please HELP me
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(); hoursList.add(hours); totalCost = 5; if (hours > 2) { hours = hours - 2; totalCost = (totalCost + hours); costList.add(totalCost); if (hours > 12) { totalCost = 12; costList.add(totalCost); } } } 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)); } }
- 03-13-2017, 03:22 AM #2
Re: HELP!! Parking Garage program
how to get the total chargeIf you don't understand my response, don't ignore it, ask a question.
- 03-13-2017, 01:55 PM #3
Member
- Join Date
- Mar 2017
- Posts
- 4
- Rep Power
- 0
Re: HELP!! Parking Garage program
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
- 03-13-2017, 02:10 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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.Please do not ask for code as refusal often offends.
** This space for rent **
- 03-13-2017, 02:56 PM #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; } costList.add(totalCost);
- 03-13-2017, 03:39 PM #6
Re: HELP!! Parking Garage program
(totalCost + roundHours);If you don't understand my response, don't ignore it, ask a question.
- 03-13-2017, 04:20 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: HELP!! Parking Garage program
Well, when each hour always costs $1...
:)Please do not ask for code as refusal often offends.
** This space for rent **
- 03-13-2017, 05:13 PM #8
Member
- Join Date
- Mar 2017
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Help!! Parking garage display
By CodeD in forum New To JavaReplies: 3Last Post: 02-08-2017, 08:06 PM -
Help | Parking program
By XSkyline in forum New To JavaReplies: 11Last Post: 11-08-2014, 08:42 PM -
help with program that calculates parking fee
By d2idan in forum New To JavaReplies: 3Last Post: 10-23-2014, 06:46 PM -
Creating a "Parking Garage" with cars as objects
By ajfonty in forum New To JavaReplies: 2Last Post: 05-23-2012, 03:47 PM -
a parking garage program
By jihad in forum New To JavaReplies: 3Last Post: 12-24-2011, 03:06 PM
Bookmarks