public class PayrollSystemTest
1: // Fig. 10.9: PayrollSystemTest.java
2: // Employee hierarchy test program.
3:
4: public class PayrollSystemTest
5: {
6: public static void main( String args[] )
7: {
8: // create subclass objects
9: SalariedEmployee salariedEmployee =
10: new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
11: HourlyEmployee hourlyEmployee =
12: new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
13: CommissionEmployee commissionEmployee =
14: new CommissionEmployee(
15: "Sue", "Jones", "333-33-3333", 10000, .06 );
16: BasePlusCommissionEmployee basePlusCommissionEmployee =
17: new BasePlusCommissionEmployee(
18: "Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
19:
20: System.out.println( "Employees processed individually:\n" );
21:
22: System.out.printf( "%s\n%s: $%,.2f\n\n",
23: salariedEmployee, "earned", salariedEmployee.earnings() );
24: System.out.printf( "%s\n%s: $%,.2f\n\n",
25: hourlyEmployee, "earned", hourlyEmployee.earnings() );
26: System.out.printf( "%s\n%s: $%,.2f\n\n",
27: commissionEmployee, "earned", commissionEmployee.earnings() );
28: System.out.printf( "%s\n%s: $%,.2f\n\n",
29: basePlusCommissionEmployee,
30: "earned", basePlusCommissionEmployee.earnings() );
31:
32: // create four-element Employee array
33: Employee employees[] = new Employee[ 4 ];
34:
35: // initialize array with Employees
36: employees[ 0 ] = salariedEmployee;
37: employees[ 1 ] = hourlyEmployee;
38: employees[ 2 ] = commissionEmployee;
39: employees[ 3 ] = basePlusCommissionEmployee;
40:
41: System.out.println( "Employees processed polymorphically:\n" );
42:
43: // generically process each element in array employees
44: for ( Employee currentEmployee : employees )
45: {
46: System.out.println( currentEmployee ); // invokes toString
47:
48: // determine whether element is a BasePlusCommissionEmployee
49: if ( currentEmployee instanceof BasePlusCommissionEmployee )
50: {
51: // downcast Employee reference to
52: // BasePlusCommissionEmployee reference
53: BasePlusCommissionEmployee employee =
54: ( BasePlusCommissionEmployee ) currentEmployee;
55:
56: double oldBaseSalary = employee.getBaseSalary();
57: employee.setBaseSalary( 1.10 * oldBaseSalary );
58: System.out.printf(
59: "new base salary with 10%% increase is: $%,.2f\n",
60: employee.getBaseSalary() );
61: } // end if
62:
63: System.out.printf(
64: "earned $%,.2f\n\n", currentEmployee.earnings() );
65: } // end for
66:
67: // get type name of each object in employees array
68: for ( int j = 0; j < employees.length; j++ )
69: System.out.printf( "Employee %d is a %s\n", j,
70: employees[ j ].getClass().getName() );
71: } // end main
72: } // end class PayrollSystemTest
73:
74: /**************************************************************************
75: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
76: * Pearson Education, Inc. All Rights Reserved. *
77: * *
78: * DISCLAIMER: The authors and publisher of this book have used their *
79: * best efforts in preparing the book. These efforts include the *
80: * development, research, and testing of the theories and programs *
81: * to determine their effectiveness. The authors and publisher make *
82: * no warranty of any kind, expressed or implied, with regard to these *
83: * programs or to the documentation contained in these books. The authors *
84: * and publisher shall not be liable in any event for incidental or *
85: * consequential damages in connection with, or arising out of, the *
86: * furnishing, performance, or use of these programs. *
87: *************************************************************************/