1 // Fig. 9.4: CommissionEmployee.java
2 // CommissionEmployee class represents a commission employee.
3
4 public class CommissionEmployee extends Object
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 CommissionEmployee( 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 } // end five-argument CommissionEmployee constructor
23
24 // set first name
25 public void setFirstName( String first )
26 {
27 firstName = first;
28 } // end method setFirstName
29
30 // return first name
31 public String getFirstName()
32 {
33 return firstName;
34 } // end method getFirstName
35
36 // set last name
37 public void setLastName( String last )
38 {
39 lastName = last;
40 } // end method setLastName
41
42 // return last name
43 public String getLastName()
44 {
45 return lastName;
46 } // end method getLastName
47
48 // set social security number
49 public void setSocialSecurityNumber( String ssn )
50 {
51 socialSecurityNumber = ssn; // should validate
52 } // end method setSocialSecurityNumber
53
54 // return social security number
55 public String getSocialSecurityNumber()
56 {
57 return socialSecurityNumber;
58 } // end method getSocialSecurityNumber
59
60 // set gross sales amount
61 public void setGrossSales( double sales )
62 {
63 grossSales = ( sales < 0.0 ) ? 0.0 : sales;
64 } // end method setGrossSales
65
66 // return gross sales amount
67 public double getGrossSales()
68 {
69 return grossSales;
70 } // end method getGrossSales
71
72 // set commission rate
73 public void setCommissionRate( double rate )
74 {
75 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
76 } // end method setCommissionRate
77
78 // return commission rate
79 public double getCommissionRate()
80 {
81 return commissionRate;
82 } // end method getCommissionRate
83
84 // calculate earnings
85 public double earnings()
86 {
87 return commissionRate * grossSales;
88 } // end method earnings
89
90 // return String representation of CommissionEmployee object
91 public String toString()
92 {
93 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
94 "commission employee", firstName, lastName,
95 "social security number", socialSecurityNumber,
96 "gross sales", grossSales,
97 "commission rate", commissionRate );
98 } // end method toString
99 } // end class CommissionEmployee
1 // Fig. 9.5: CommissionEmployeeTest.java
2 // Testing class CommissionEmployee.
3
4 public class CommissionEmployeeTest
5 {
6 public static void main( String args[] )
7 {
8 // instantiate CommissionEmployee object
9 CommissionEmployee employee = new CommissionEmployee(
10 "Sue", "Jones", "222-22-2222", 10000, .06 );
11
12 // get commission employee data
13 System.out.println(
14 "Employee information obtained by get methods: \n" );
15 System.out.printf( "%s %s\n", "First name is",
16 employee.getFirstName() );
17 System.out.printf( "%s %s\n", "Last name is",
18 employee.getLastName() );
19 System.out.printf( "%s %s\n", "Social security number is",
20 employee.getSocialSecurityNumber() );
21 System.out.printf( "%s %.2f\n", "Gross sales is",
22 employee.getGrossSales() );
23 System.out.printf( "%s %.2f\n", "Commission rate is",
24 employee.getCommissionRate() );
25
26 employee.setGrossSales( 500 ); // set gross sales
27 employee.setCommissionRate( .1 ); // set commission rate
28
29 System.out.printf( "\n%s:\n\n%s\n",
30 "Updated employee information obtained by toString", employee );
31 } // end main
32 } // end class CommissionEmployeeTest
1 // Fig. 9.12: CommissionEmployee3.java
2 // CommissionEmployee3 class represents a commission employee.
3
4 public class CommissionEmployee3
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 CommissionEmployee3( 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 } // end five-argument CommissionEmployee3 constructor
23
24 // set first name
25 public void setFirstName( String first )
26 {
27 firstName = first;
28 } // end method setFirstName
29
30 // return first name
31 public String getFirstName()
32 {
33 return firstName;
34 } // end method getFirstName
35
36 // set last name
37 public void setLastName( String last )
38 {
39 lastName = last;
40 } // end method setLastName
41
42 // return last name
43 public String getLastName()
44 {
45 return lastName;
46 } // end method getLastName
47
48 // set social security number
49 public void setSocialSecurityNumber( String ssn )
50 {
51 socialSecurityNumber = ssn; // should validate
52 } // end method setSocialSecurityNumber
53
54 // return social security number
55 public String getSocialSecurityNumber()
56 {
57 return socialSecurityNumber;
58 } // end method getSocialSecurityNumber
59
60 // set gross sales amount
61 public void setGrossSales( double sales )
62 {
63 grossSales = ( sales < 0.0 ) ? 0.0 : sales;
64 } // end method setGrossSales
65
66 // return gross sales amount
67 public double getGrossSales()
68 {
69 return grossSales;
70 } // end method getGrossSales
71
72 // set commission rate
73 public void setCommissionRate( double rate )
74 {
75 commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
76 } // end method setCommissionRate
77
78 // return commission rate
79 public double getCommissionRate()
80 {
81 return commissionRate;
82 } // end method getCommissionRate
83
84 // calculate earnings
85 public double earnings()
86 {
87 return getCommissionRate() * getGrossSales();
88 } // end method earnings
89
90 // return String representation of CommissionEmployee3 object
91 public String toString()
92 {
93 return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
94 "commission employee", getFirstName(), getLastName(),
95 "social security number", getSocialSecurityNumber(),
96 "gross sales", getGrossSales(),
97 "commission rate", getCommissionRate() );
98 } // end method toString
99 } // end class CommissionEmployee3
1 // Fig. 9.13: BasePlusCommissionEmployee4.java
2 // BasePlusCommissionEmployee4 class inherits from CommissionEmployee3 and
3 // accesses CommissionEmployee3's private data via CommissionEmployee3's
4 // public methods.
5
6 public class BasePlusCommissionEmployee4 extends CommissionEmployee3
7 {
8 private double baseSalary; // base salary per week
9
10 // six-argument constructor
11 public BasePlusCommissionEmployee4( String first, String last,
12 String ssn, double sales, double rate, double salary )
13 {
14 super( first, last, ssn, sales, rate );
15 setBaseSalary( salary ); // validate and store base salary
16 } // end six-argument BasePlusCommissionEmployee4 constructor
17
18 // set base salary
19 public void setBaseSalary( double salary )
20 {
21 baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
22 } // end method setBaseSalary
23
24 // return base salary
25 public double getBaseSalary()
26 {
27 return baseSalary;
28 } // end method getBaseSalary
29
30 // calculate earnings
31 public double earnings()
32 {
33 return getBaseSalary() + super.earnings();
34 } // end method earnings
35
36 // return String representation of BasePlusCommissionEmployee4
37 public String toString()
38 {
39 return String.format( "%s %s\n%s: %.2f", "base-salaried",
40 super.toString(), "base salary", getBaseSalary() );
41 } // end method toString
42 } // end class BasePlusCommissionEmployee4
1 // Fig. 9.14: BasePlusCommissionEmployeeTest4.java
2 // Testing class BasePlusCommissionEmployee4.
3
4 public class BasePlusCommissionEmployeeTest4
5 {
6 public static void main( String args[] )
7 {
8 // instantiate BasePlusCommissionEmployee4 object
9 BasePlusCommissionEmployee4 employee =
10 new BasePlusCommissionEmployee4(
11 "Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
12
13 // get base-salaried commission employee data
14 System.out.println(
15 "Employee information obtained by get methods: \n" );
16 System.out.printf( "%s %s\n", "First name is",
17 employee.getFirstName() );
18 System.out.printf( "%s %s\n", "Last name is",
19 employee.getLastName() );
20 System.out.printf( "%s %s\n", "Social security number is",
21 employee.getSocialSecurityNumber() );
22 System.out.printf( "%s %.2f\n", "Gross sales is",
23 employee.getGrossSales() );
24 System.out.printf( "%s %.2f\n", "Commission rate is",
25 employee.getCommissionRate() );
26 System.out.printf( "%s %.2f\n", "Base salary is",
27 employee.getBaseSalary() );
28
29 employee.setBaseSalary( 1000 ); // set base salary
30
31 System.out.printf( "\n%s:\n\n%s\n",
32 "Updated employee information obtained by toString",
33 employee.toString() );
34 } // end main
35 } // end class BasePlusCommissionEmployeeTest4
Tidak ada komentar:
Posting Komentar