//
Payroll System Test, Java how to program 6/e, Exercise 10.9
//Modified based on Fig. 10.9: PayrollSystemTest.java
// Employee hierarchy test program.
package PayrollSystem;
import java.util. * ;
public class PayrollSystemTest
{
public static void main( String args[] )
{
// create subclass objects
Date birthday[] = new Date[ 5 ];
birthday[ 0 ] = new Date( 4 , 20 , 1980 );
birthday[ 1 ] = new Date( 9 , 30 , 1980 );
birthday[ 2 ] = new Date( 10 , 2 , 1982 );
birthday[ 3 ] = new Date( 9 , 6 , 1979 );
birthday[ 4 ] = new Date( 4 , 6 , 1979 );
SalariedEmployee salariedEmployee =
new SalariedEmployee( " Jack " , " Michale " , " 111-11-1111 " ,birthday[ 0 ], 20 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( " Karen " , " Price " , " 222-22-2222 " , birthday[ 1 ], 16.75 , 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
" Sue " , " Jones " , " 333-33-3333 " , birthday[ 2 ], 10000 , . 06 );
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
" Bob " , " Lewis " , " 444-44-4444 " , birthday[ 3 ], 5000 , . 04 , 300 );
PieceWorker pieceWorker =
new PieceWorker(
" Eddy " , " Thomas " , " 555-55-5555 " , birthday[ 4 ], 10000 , . 5 );
System.out.println( " Employees processed individually: /n/n " );
System.out.printf( " %s %s: $%,.2f " ,
salariedEmployee, " earned " , salariedEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
hourlyEmployee, " earned " , hourlyEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
commissionEmployee, " earned " , commissionEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
basePlusCommissionEmployee,
" earned " , basePlusCommissionEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
pieceWorker,
" earned " , pieceWorker.earnings() );
// create four-element Employee array
Employee employees[ ] = new Employee[ 5 ];
// initialize array with Employees
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
employees[ 4 ] = pieceWorker;
System.out.println( " Employees processed polymorphically:/n/n " );
// generically process each element in array employees
for ( Employee currentEmployee : employees )
{
System.out.println( currentEmployee ); // invokes toString
// check birth month's consistency
Calendar cal = new GregorianCalendar();
int currentMonth = cal.get(Calendar.MONTH);
if (currentEmployee instanceof PieceWorker)
System.out.printf( " %s %s " , " You are great " , " I was a piece worker before. " );
if ( currentEmployee.getBirthMonth() == currentMonth + 1 )
{
double bonus = 100.00 ;
System.out.printf(
" Annual bonus is: $%,.2f " ,
bonus);
System.out.printf(
" Total earning is: $%,.2f " , currentEmployee.earnings() + bonus );
} // end if
else {
System.out.printf(
" earned $%,.2f " , currentEmployee.earnings() );
}
} // end for
// get type name of each object in employees array
for ( int j = 0 ; j < employees.length; j ++ )
System.out.printf( " Employee %d is a %s " , j,
employees[ j ].getClass().getName() );
} // end main
} // end class PayrollSystemTest
//Modified based on Fig. 10.9: PayrollSystemTest.java
// Employee hierarchy test program.
package PayrollSystem;
import java.util. * ;
public class PayrollSystemTest
{
public static void main( String args[] )
{
// create subclass objects
Date birthday[] = new Date[ 5 ];
birthday[ 0 ] = new Date( 4 , 20 , 1980 );
birthday[ 1 ] = new Date( 9 , 30 , 1980 );
birthday[ 2 ] = new Date( 10 , 2 , 1982 );
birthday[ 3 ] = new Date( 9 , 6 , 1979 );
birthday[ 4 ] = new Date( 4 , 6 , 1979 );
SalariedEmployee salariedEmployee =
new SalariedEmployee( " Jack " , " Michale " , " 111-11-1111 " ,birthday[ 0 ], 20 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( " Karen " , " Price " , " 222-22-2222 " , birthday[ 1 ], 16.75 , 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
" Sue " , " Jones " , " 333-33-3333 " , birthday[ 2 ], 10000 , . 06 );
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
" Bob " , " Lewis " , " 444-44-4444 " , birthday[ 3 ], 5000 , . 04 , 300 );
PieceWorker pieceWorker =
new PieceWorker(
" Eddy " , " Thomas " , " 555-55-5555 " , birthday[ 4 ], 10000 , . 5 );
System.out.println( " Employees processed individually: /n/n " );
System.out.printf( " %s %s: $%,.2f " ,
salariedEmployee, " earned " , salariedEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
hourlyEmployee, " earned " , hourlyEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
commissionEmployee, " earned " , commissionEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
basePlusCommissionEmployee,
" earned " , basePlusCommissionEmployee.earnings() );
System.out.printf( " %s %s: $%,.2f " ,
pieceWorker,
" earned " , pieceWorker.earnings() );
// create four-element Employee array
Employee employees[ ] = new Employee[ 5 ];
// initialize array with Employees
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
employees[ 4 ] = pieceWorker;
System.out.println( " Employees processed polymorphically:/n/n " );
// generically process each element in array employees
for ( Employee currentEmployee : employees )
{
System.out.println( currentEmployee ); // invokes toString
// check birth month's consistency
Calendar cal = new GregorianCalendar();
int currentMonth = cal.get(Calendar.MONTH);
if (currentEmployee instanceof PieceWorker)
System.out.printf( " %s %s " , " You are great " , " I was a piece worker before. " );
if ( currentEmployee.getBirthMonth() == currentMonth + 1 )
{
double bonus = 100.00 ;
System.out.printf(
" Annual bonus is: $%,.2f " ,
bonus);
System.out.printf(
" Total earning is: $%,.2f " , currentEmployee.earnings() + bonus );
} // end if
else {
System.out.printf(
" earned $%,.2f " , currentEmployee.earnings() );
}
} // end for
// get type name of each object in employees array
for ( int j = 0 ; j < employees.length; j ++ )
System.out.printf( " Employee %d is a %s " , j,
employees[ j ].getClass().getName() );
} // end main
} // end class PayrollSystemTest
A snapshot of program execution: