Source of CommissionEmployee.java


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