|
| 1 | +// Fig. 9.10: CommissionEmployee.java |
| 2 | +// CommissionEmployee class uses methods to manipulate its |
| 3 | +// private instance variables. |
| 4 | +public class CommissionEmployee |
| 5 | +{ |
| 6 | + private final String firstName; |
| 7 | + private final String lastName; |
| 8 | + private final String socialSecurityNumber; |
| 9 | + private double grossSales; // gross weekly sales |
| 10 | + private double commissionRate; // commission percentage |
| 11 | + |
| 12 | + // five-argument constructor |
| 13 | + public CommissionEmployee(String firstName, String lastName, |
| 14 | + String socialSecurityNumber, double grossSales, |
| 15 | + double commissionRate) |
| 16 | + { |
| 17 | + // implicit call to Object constructor occurs here |
| 18 | + |
| 19 | + // if grossSales is invalid throw exception |
| 20 | + if (grossSales < 0.0) |
| 21 | + throw new IllegalArgumentException( |
| 22 | + "Gross sales must be >= 0.0"); |
| 23 | + |
| 24 | + // if commissionRate is invalid throw exception |
| 25 | + if (commissionRate <= 0.0 || commissionRate >= 1.0) |
| 26 | + throw new IllegalArgumentException( |
| 27 | + "Commission rate must be > 0.0 and < 1.0"); |
| 28 | + |
| 29 | + this.firstName = firstName; |
| 30 | + this.lastName = lastName; |
| 31 | + this.socialSecurityNumber = socialSecurityNumber; |
| 32 | + this.grossSales = grossSales; |
| 33 | + this.commissionRate = commissionRate; |
| 34 | + } // end constructor |
| 35 | + |
| 36 | + // return first name |
| 37 | + public String getFirstName() |
| 38 | + { |
| 39 | + return firstName; |
| 40 | + } |
| 41 | + |
| 42 | + // return last name |
| 43 | + public String getLastName() |
| 44 | + { |
| 45 | + return lastName; |
| 46 | + } |
| 47 | + |
| 48 | + // return social security number |
| 49 | + public String getSocialSecurityNumber() |
| 50 | + { |
| 51 | + return socialSecurityNumber; |
| 52 | + } |
| 53 | + |
| 54 | + // set gross sales amount |
| 55 | + public void setGrossSales(double grossSales) |
| 56 | + { |
| 57 | + if (grossSales < 0.0) |
| 58 | + throw new IllegalArgumentException( |
| 59 | + "Gross sales must be >= 0.0"); |
| 60 | + |
| 61 | + this.grossSales = grossSales; |
| 62 | + } |
| 63 | + |
| 64 | + // return gross sales amount |
| 65 | + public double getGrossSales() |
| 66 | + { |
| 67 | + return grossSales; |
| 68 | + } |
| 69 | + |
| 70 | + // set commission rate |
| 71 | + public void setCommissionRate(double commissionRate) |
| 72 | + { |
| 73 | + if (commissionRate <= 0.0 || commissionRate >= 1.0) |
| 74 | + throw new IllegalArgumentException( |
| 75 | + "Commission rate must be > 0.0 and < 1.0"); |
| 76 | + |
| 77 | + this.commissionRate = commissionRate; |
| 78 | + } |
| 79 | + |
| 80 | + // return commission rate |
| 81 | + public double getCommissionRate() |
| 82 | + { |
| 83 | + return commissionRate; |
| 84 | + } |
| 85 | + |
| 86 | + // calculate earnings |
| 87 | + public double earnings() |
| 88 | + { |
| 89 | + return getCommissionRate() * getGrossSales(); |
| 90 | + } |
| 91 | + |
| 92 | + // return String representation of CommissionEmployee object |
| 93 | + @Override |
| 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 | + } |
| 102 | +} // end class CommissionEmployee |
| 103 | + |
| 104 | + |
| 105 | +/************************************************************************** |
| 106 | + * (C) Copyright 1992-2014 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 | + *************************************************************************/ |
0 commit comments