Source of HourlyEmployee.java


  1: // Fig. 10.6: HourlyEmployee.java
  2: // HourlyEmployee class extends Employee.
  3: 
  4: public class HourlyEmployee extends Employee 
  5: {
  6:    private double wage; // wage per hour
  7:    private double hours; // hours worked for week
  8: 
  9:    // five-argument constructor
 10:    public HourlyEmployee( String first, String last, String ssn, 
 11:       double hourlyWage, double hoursWorked )
 12:    {
 13:       super( first, last, ssn );
 14:       setWage( hourlyWage ); // validate and store hourly wage
 15:       setHours( hoursWorked ); // validate and store hours worked
 16:    } // end five-argument HourlyEmployee constructor
 17: 
 18:    // set wage
 19:    public void setWage( double hourlyWage )
 20:    {
 21:       wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
 22:    } // end method setWage
 23: 
 24:    // return wage
 25:    public double getWage()
 26:    {
 27:       return wage;
 28:    } // end method getWage
 29: 
 30:    // set hours worked
 31:    public void setHours( double hoursWorked )
 32:    {
 33:       hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
 34:          hoursWorked : 0.0;
 35:    } // end method setHours
 36: 
 37:    // return hours worked
 38:    public double getHours()
 39:    {
 40:       return hours;
 41:    } // end method getHours
 42: 
 43:    // calculate earnings; override abstract method earnings in Employee
 44:    public double earnings()
 45:    {
 46:       if ( getHours() <= 40 ) // no overtime
 47:          return getWage() * getHours();
 48:       else
 49:          return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
 50:    } // end method earnings
 51: 
 52:    // return String representation of HourlyEmployee object
 53:    public String toString()
 54:    {
 55:       return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", 
 56:          super.toString(), "hourly wage", getWage(), 
 57:          "hours worked", getHours() );
 58:    } // end method toString
 59: } // end class HourlyEmployee
 60: 
 61: 
 62: /**************************************************************************
 63:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 64:  * Pearson Education, Inc. All Rights Reserved.                           *
 65:  *                                                                        *
 66:  * DISCLAIMER: The authors and publisher of this book have used their     *
 67:  * best efforts in preparing the book. These efforts include the          *
 68:  * development, research, and testing of the theories and programs        *
 69:  * to determine their effectiveness. The authors and publisher make       *
 70:  * no warranty of any kind, expressed or implied, with regard to these    *
 71:  * programs or to the documentation contained in these books. The authors *
 72:  * and publisher shall not be liable in any event for incidental or       *
 73:  * consequential damages in connection with, or arising out of, the       *
 74:  * furnishing, performance, or use of these programs.                     *
 75:  *************************************************************************/