薪水册测试类及结果(Payroll System modification)

// 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

 

A snapshot of program execution:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值