-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePlusCommissionEmployeeComposition.java
More file actions
100 lines (86 loc) · 3.18 KB
/
BasePlusCommissionEmployeeComposition.java
File metadata and controls
100 lines (86 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* BasePlusCommissionEmployeeComposition.java
*/
/*
9.3 (Recommended: Using Composition Rather Than Inheritance) Many
programs written with inheritance could be written with composition
instead, and vice versa. Rewrite class BasePlus–
CommissionEmployee (Fig. 9.11 ) of the
CommissionEmployee–BasePlusCommissionEmployee
hierarchy so that it contains a reference to a CommissionEmployee
object, rather than inheriting from class CommissionEmployee. Retest
BasePlusCommissionEmployee to demonstrate that it still provides
the same functionality.
*/
public class BasePlusCommissionEmployeeComposition {
IllegalArgumentException IAE = new IllegalArgumentException();
private double baseSalary; // base salary per week
// Demonstrate composition of (as opposed to inheritance from) a CommissionEmployee object
// six-argument constructor
public BasePlusCommissionEmployeeComposition( String first, String last,
String ssn, double sales, double rate, double salary) {
// implemented the 6 argument constructor
this.first = first;
this.last = last;
this.ssn = ssn;
this.sales = sales;
this.rate = rate;
this.salary = salary;
baseSalary = salary;
CommissionEmployee ce = new CommissionEmployee(first, last, ssn, gross, commission);
}
// Implement accessors and mutators for all six attributes (stubs appear below)
// As per the text (demonstrated in its BasePlusCommissionEmployee implementation), throw an IllegalArgumentException if the salary argument is negative
public void setBaseSalary( double salary ) {
// implemented this method
if (salary < 0){
throw IAE;
}
else{
this.salary = salary;
}
}
public double getBaseSalary() {
// implemented this method
return salary;
}
public String getFirstName() {
// implemented this method
return first;
}
public String getLastName() {
// implemented this method
return last;
}
public String getSocialSecurityNumber() {
// implemented this method
return ssn;
}
public double getGrossSales() {
// implemented this method
return ce.gross;
}
public void setGrossSales(double sales) {
// implemented this method
this.sales = sales;
}
public double getCommissionRate() {
// implemented this method
return ce.commission;
}
public void setCommissionRate(double rate) {
// implemented this method
this.commission = commission;
}
public double earnings() {
// TODO: implement this method
return double earnings;
}
@Override
public String toString() {
// TODO: implement this method
return String.format("%s: %s%n%s: %s%n%s: %s%n%s: %f%n%s: %f%n%s: %f%n","First Name", getFirstName(),
"Last Name", getLastName(), "SSN", getSocialSecurityNumber(), "Commission", getCommissionRate(),
"Gross", getGrossSales(), "Base Salary", getBaseSalary());
}
}