Skip to content

Commit 5af25bc

Browse files
authored
Add files via upload
1 parent 7bffdf0 commit 5af25bc

File tree

3 files changed

+261
-0
lines changed

3 files changed

+261
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Fig. 9.11: BasePlusCommissionEmployee.java
2+
// BasePlusCommissionEmployee class inherits from CommissionEmployee
3+
// and accesses the superclass�s private data via inherited
4+
// public methods.
5+
6+
public class BasePlusCommissionEmployee
7+
{
8+
private CommissionEmployee CE;
9+
private double baseSalary; // base salary per week
10+
11+
// six-argument constructor
12+
public BasePlusCommissionEmployee(String firstName, String lastName,
13+
String socialSecurityNumber, double grossSales,
14+
double commissionRate, double baseSalary)
15+
{
16+
CE = new CommissionEmployee(firstName, lastName, socialSecurityNumber,
17+
grossSales, commissionRate);
18+
19+
// if baseSalary is invalid throw exception
20+
if (baseSalary < 0.0)
21+
throw new IllegalArgumentException(
22+
"Base salary must be >= 0.0");
23+
24+
this.baseSalary = baseSalary;
25+
}
26+
27+
// get first name
28+
public String getFirstName(){
29+
return CE.getFirstName();
30+
}
31+
32+
// get last name
33+
public String getLastName(){
34+
return CE.getLastName();
35+
}
36+
37+
// get social security number
38+
public String getSocialSecurityNumber()
39+
{
40+
return CE.getSocialSecurityNumber();
41+
}
42+
43+
//set gross sales
44+
public void setGrossSales(double grossSales)
45+
{
46+
CE.setGrossSales(grossSales);
47+
}
48+
49+
// get gross sales
50+
public double getGrossSales()
51+
{
52+
return CE.getGrossSales();
53+
}
54+
55+
// set commission rate
56+
public void setCommissionRate(double commissionRate)
57+
{
58+
CE.setCommissionRate(commissionRate);
59+
}
60+
61+
// get commision rate
62+
public double getCommissionRate()
63+
{
64+
return CE.getCommissionRate();
65+
}
66+
67+
// set base salary
68+
public void setBaseSalary(double baseSalary)
69+
{
70+
if (baseSalary < 0.0)
71+
throw new IllegalArgumentException(
72+
"Base salary must be >= 0.0");
73+
74+
this.baseSalary = baseSalary;
75+
}
76+
77+
// return base salary
78+
public double getBaseSalary()
79+
{
80+
return baseSalary;
81+
}
82+
83+
// calculate earnings
84+
public double earnings()
85+
{
86+
return getBaseSalary() + CE.earnings();
87+
}
88+
89+
// return String representation of BasePlusCommissionEmployee
90+
public String toString()
91+
{
92+
return String.format("%s %s%n%s: %.2f", "base-salaried",
93+
CE.toString(), "base salary", getBaseSalary());
94+
}
95+
} // end class BasePlusCommissionEmployee
96+
97+
98+
/**************************************************************************
99+
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
100+
* Pearson Education, Inc. All Rights Reserved. *
101+
* *
102+
* DISCLAIMER: The authors and publisher of this book have used their *
103+
* best efforts in preparing the book. These efforts include the *
104+
* development, research, and testing of the theories and programs *
105+
* to determine their effectiveness. The authors and publisher make *
106+
* no warranty of any kind, expressed or implied, with regard to these *
107+
* programs or to the documentation contained in these books. The authors *
108+
* and publisher shall not be liable in any event for incidental or *
109+
* consequential damages in connection with, or arising out of, the *
110+
* furnishing, performance, or use of these programs. *
111+
*************************************************************************/
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
*************************************************************************/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Main
2+
{
3+
public static void main(String[] args)
4+
{
5+
// instantiate BasePlusCommissionEmployee object
6+
BasePlusCommissionEmployee employee =
7+
new BasePlusCommissionEmployee(
8+
"Bob", "Lewis", "333-33-3333", 5000, .04, 300);
9+
10+
// get base-salaried commission employee data
11+
System.out.println(
12+
"Employee information obtained by get methods:");
13+
System.out.printf("%n%s %s%n", "First name is",
14+
employee.getFirstName());
15+
System.out.printf("%s %s%n", "Last name is",
16+
employee.getLastName());
17+
System.out.printf("%s %s%n", "Social security number is",
18+
employee.getSocialSecurityNumber());
19+
System.out.printf("%s %.2f%n", "Gross sales is",
20+
employee.getGrossSales());
21+
System.out.printf("%s %.2f%n", "Commission rate is",
22+
employee.getCommissionRate());
23+
System.out.printf("%s %.2f%n", "Base salary is",
24+
employee.getBaseSalary());
25+
26+
employee.setBaseSalary(1000);
27+
28+
System.out.printf("%n%s:%n%n%s%n",
29+
"Updated employee information obtained by toString",
30+
employee.toString());
31+
} // end main
32+
} // end class

0 commit comments

Comments
 (0)