Monday, September 12, 2016

JAVA HOW TO PROGRAM, NINTH EDITION, DEITEL: CHAPTER SIX SOLUTIONS



Question: Parking Charges

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’s receipts. It should use the method calculateCharges to determine the charge for each customer.  


Solution: Parking Charges

//Author: Free Coder
//www.freecoder247.blogspot.com

//Parking Charges.java
//This program calculates customers parking charges 
import java.util.Scanner;

public class ParkingCharges
      {
     
    public static void main ( String [] args)
    {
      Scanner input = new Scanner (System.in);
     
      double totalReceipts=0.0;
      double fee;
      double hours;
     
      System.out.print("Enter number of hours (a negative to quit): " );
      hours=input.nextDouble();
     
      while (hours >=0.0)
      {
      fee=calculateCharge(hours);
      totalReceipts += fee;
      System.out.printf("Current charge: N%.2f, Total receipts: N%.2f\n", fee, totalReceipts );
      System.out.print("Enter number of hours (a negative to quit)");
      hours=input.nextDouble();
      }//end while
    }// method main     
    

    private static double calculateCharge(double hours)
    {
       /*final*/ double minPark= 2.0;

       /*final*/ double maxPark= 10.0;

       /*final*/ double maxHours= 24.0;

       /*final*/ double minHours= 3.0;

       /*final*/ double hourEx= 0.5;

       double fee;
     
       fee= minPark;
       
      if ( hours <= minHours )

          fee = minPark;
          
     
      if ( hours > minHours && hours < maxHours )
          fee = hourEx*(Math.ceil(hours) - minHours) + minPark;
     
       if ( hours == maxHours )

          fee = maxPark;
        
      
       return fee;
    } // method calculateCharge

 } // class ParkingCharges


5 comments:

  1. it very nice code
    but need some addition
    when hours is 19 then its come $10
    and above 19 hours to 24h should be $10

    this program give result for 20, 21, 22,23 hours not $10.


    ReplyDelete
  2. hi
    we cannot get the hours calculated?
    #include
    #include

    using namespace std;

    double calculateCharges( double );

    int main()
    {
    double first, second, third;


    cin >> first >> second >> third;
    cout << "Car\tHours\tCharge\n";
    cout << "1\t" << fixed << setprecision( 1) << first
    << setprecision( 2 ) << "\t" << calculateCharges( first ) << endl;

    cout << "2\t" << fixed << setprecision( 1 ) << second
    << setprecision( 2 ) << "\t" << calculateCharges( second ) << endl;

    cout << "3\t" << fixed << setprecision( 1 ) << third
    << setprecision( 2 ) << "\t" << calculateCharges( third ) << endl;

    cout << "TOTAL\t" << fixed << setprecision( 1 ) << first + second + third
    << fixed << setprecision( 2 ) << "\t"
    << calculateCharges( first ) + calculateCharges( second ) + calculateCharges( third ) << endl;

    //for pause
    system("PAUSE");
    return 0;
    }

    //function charge calculation
    double calculateCharges( double hours )
    {
    if( hours <= 3 )
    return 2.00;

    if( hours <= 3 )
    return 2.00;
    else
    {
    if ( hours < 24 )
    return 12.00 + ( hours - 3 ) * 0.90;
    else
    return 12.00;
    }
    }

    ReplyDelete