Results 1 to 8 of 8
  1. #1
    crentist505 is offline Member
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0

    Question 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));
    
        }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: HELP!! Parking Garage program

    how to get the total charge
    What does the program print out? What should it print out?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    crentist505 is offline Member
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0

    Default Re: HELP!! Parking Garage program

    Quote Originally Posted by Norm View Post
    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. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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 **

  5. #5
    crentist505 is offline Member
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0

    Default 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);

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default 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???
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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 **

  8. #8
    crentist505 is offline Member
    Join Date
    Mar 2017
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Help!! Parking garage display
    By CodeD in forum New To Java
    Replies: 3
    Last Post: 02-08-2017, 07:06 PM
  2. Help | Parking program
    By XSkyline in forum New To Java
    Replies: 11
    Last Post: 11-08-2014, 07:42 PM
  3. help with program that calculates parking fee
    By d2idan in forum New To Java
    Replies: 3
    Last Post: 10-23-2014, 05:46 PM
  4. Creating a "Parking Garage" with cars as objects
    By ajfonty in forum New To Java
    Replies: 2
    Last Post: 05-23-2012, 02:47 PM
  5. a parking garage program
    By jihad in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •