HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Calculating Parking Fees

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatingfeesparking

Problem

Here's the challenge description:


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 a Java program that calculates and displays the parking charges for each customer who parked in the garage yesterday.

Input the number of hours parked for each customer. The number of customers can be fixed using a final constant.


The program should display the charge for the current customer and should calculate and display the running total of the day's receipts.

Use a method called calculateCharges to determine the charge for each customer.

Here's my code:

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

public class Hwk4B {

    public static void main(String[] args) 
    {

        Scanner keyboard = new Scanner(System.in);
        DecimalFormat formatter = new DecimalFormat("$##.00");

        System.out.print("Enter the number of cars parked yesterday: ");
        final int NUMBER_OF_CARS = keyboard.nextInt();

        double hoursParked=0;
        double currentCost=0;   
        double totalCost=0;

        for(int count = 1; count3 && numHours19)
            garageCost = 10;

        return garageCost;
    }

}

Solution

Your code should be as similar to the spec as possible so:

else if (numHours >19)
     garageCost = 10;


Should be removed and you should add:

return Math.min(garageCost, 10)


My final version is:

public static double calculateCharges (double numHours)
{
    if (numHours <= 3)
    {
         return 2;
    }
    double garageCost = 2.0 + 0.5 * (numHours - 3);
    return Math.min(garageCost, 10);
}

Code Snippets

else if (numHours >19)
     garageCost = 10;
return Math.min(garageCost, 10)
public static double calculateCharges (double numHours)
{
    if (numHours <= 3)
    {
         return 2;
    }
    double garageCost = 2.0 + 0.5 * (numHours - 3);
    return Math.min(garageCost, 10);
}

Context

StackExchange Code Review Q#97384, answer score: 13

Revisions (0)

No revisions yet.