Source of CommissionEmployee4.java


  1: // Fig. 9.15: CommissionEmployee4.java
  2: // CommissionEmployee4 class represents a commission employee.
  3: 
  4: public class CommissionEmployee4
  5: {
  6:    private String firstName;
  7:    private String lastName;
  8:    private String socialSecurityNumber;
  9:    private double grossSales; // gross weekly sales
 10:    private double commissionRate; // commission percentage
 11: 
 12:    // five-argument constructor
 13:    public CommissionEmployee4( String first, String last, String ssn, 
 14:       double sales, double rate )
 15:    {
 16:       // implicit call to Object constructor occurs here
 17:       firstName = first;
 18:       lastName = last;
 19:       socialSecurityNumber = ssn;
 20:       setGrossSales( sales ); // validate and store gross sales
 21:       setCommissionRate( rate ); // validate and store commission rate
 22: 
 23:       System.out.printf( 
 24:          "\nCommissionEmployee4 constructor:\n%s\n", this );
 25:    } // end five-argument CommissionEmployee4 constructor
 26: 
 27:    // set first name
 28:    public void setFirstName( String first )
 29:    {
 30:       firstName = first;
 31:    } // end method setFirstName
 32: 
 33:    // return first name
 34:    public String getFirstName()
 35:    {
 36:       return firstName;
 37:    } // end method getFirstName
 38: 
 39:    // set last name
 40:    public void setLastName( String last )
 41:    {
 42:       lastName = last;
 43:    } // end method setLastName
 44: 
 45:    // return last name
 46:    public String getLastName()
 47:    {
 48:       return lastName;
 49:    } // end method getLastName
 50: 
 51:    // set social security number
 52:    public void setSocialSecurityNumber( String ssn )
 53:    {
 54:       socialSecurityNumber = ssn; // should validate
 55:    } // end method setSocialSecurityNumber
 56: 
 57:    // return social security number
 58:    public String getSocialSecurityNumber()
 59:    {
 60:       return socialSecurityNumber;
 61:    } // end method getSocialSecurityNumber
 62: 
 63:    // set gross sales amount
 64:    public void setGrossSales( double sales )
 65:    {
 66:       grossSales = ( sales < 0.0 ) ? 0.0 : sales;
 67:    } // end method setGrossSales
 68: 
 69:    // return gross sales amount
 70:    public double getGrossSales()
 71:    {
 72:       return grossSales;
 73:    } // end method getGrossSales
 74:    
 75:    // set commission rate
 76:    public void setCommissionRate( double rate )
 77:    {
 78:       commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
 79:    } // end method setCommissionRate
 80: 
 81:    // return commission rate
 82:    public double getCommissionRate()
 83:    {
 84:       return commissionRate;
 85:    } // end method getCommissionRate
 86: 
 87:    // calculate earnings
 88:    public double earnings()
 89:    {
 90:       return getCommissionRate() * getGrossSales();
 91:    } // end method earnings
 92: 
 93:    // return String representation of CommissionEmployee4 object
 94:    public String toString()
 95:    {
 96:       return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f", 
 97:          "commission employee", getFirstName(), getLastName(), 
 98:          "social security number", getSocialSecurityNumber(), 
 99:          "gross sales", getGrossSales(), 
100:          "commission rate", getCommissionRate() );
101:    } // end method toString
102: } // end class CommissionEmployee4
103: 
104: 
105: /**************************************************************************
106:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
107:  * Pearson Education, Inc. All Rights Reserved.                           *
108:  *                                                                        *
109:  * DISCLAIMER: The authors and publisher of this book have used their     *
110:  * best efforts in preparing the book. These efforts include the          *
111:  * development, research, and testing of the theories and programs        *
112:  * to determine their effectiveness. The authors and publisher make       *
113:  * no warranty of any kind, expressed or implied, with regard to these    *
114:  * programs or to the documentation contained in these books. The authors *
115:  * and publisher shall not be liable in any event for incidental or       *
116:  * consequential damages in connection with, or arising out of, the       *
117:  * furnishing, performance, or use of these programs.                     *
118:  *************************************************************************/