Domanda

We want the pay to show on screen when we execute the application, but all it shows is that even with the calculations we put into the code we only get:

EARNED : 0.0

As you can see, there are some elements of other classes, but no worries since they work like we expected except for the HourlyEmployee class.

The HourlyEmployee class:

 class HourlyEmployee extends Employee 
{
   private double wage; // wage per hour
   private double hours;// hours worked for week
   private double earnings= hours *wage;

   // five-argument constructor
   public HourlyEmployee( String first, String last,double payMe,
      double hourlyWage, double hoursWorked )
   {
      super( first, last );
      setWage( hourlyWage ); // validate and store hourly wage
      setHours( hoursWorked ); // validate and store hours worked
       payMe = earned();
   } // end five-argument HourlyEmployee constructor

   // set wage
   public void setWage( double hourlyWage )
   {
      wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
   } // end method setWage

   // return wage
   public double getWage()
   {
      return wage;
   } // end method getWage

   // set hours worked
   public void setHours( double hoursWorked )
   {
      hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 120.0 ) ) ?
         hoursWorked : 0.0;
   } // end method setHours

   // return hours worked
   public double getHours()
   {
      return hours;
   } // end method getHours

   // calculate earnings; override abstract method earnings in Employee

   public double earned()
   {
      if ( getHours() <= 40 ) // no overtime
         return getWage() * getHours();
      else
         return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
   } // end method earnings

   @Override
   public double getPaymentAmount()
   {
       return earnings;
    }

   // return String representation of HourlyEmployee object
   @Override
   public String toString()
   {
      return String.format( "hourly employee: %s\n%s: $%f\n %s: %f", 
         super.toString(), "hourly wage", getWage(), 
         "hours worked", getHours() );
   } // end method toString
} // end class HourlyEmployee

The Test class:

public class Test 
{
   public static void main( String args[] ) 
   {
      // create subclass objects                                          
      MonthlyEmployee salariedEmployee =                                 
         new MonthlyEmployee( "John", "Smith", 800.00 );  

      HourlyEmployee commissionEmployee = new HourlyEmployee("Sue", "Jones", 10000,        23.9, 5.7 ); 

      MonthlyEmployee worker3 = new MonthlyEmployee("Oliver" , "Queen" , 12500);


      System.out.println( "Employees processed individually:\n" );
      System.out.printf( "%s\n%s: %f\n\n",salariedEmployee, "earned",     salariedEmployee.getPaymentAmount() );

         System.out.printf("%s\n%s : $%f\n]n" , worker3 , "earned" ,   worker3.getPaymentAmount());


      System.out.printf( "%s\n%s: %f\n\n",commissionEmployee, "earned",     commissionEmployee.getPaymentAmount());


      // create four-element Employee array
      Employee employees[] = new Employee[ 3 ]; 

      // initialize array with Employees          
      employees[ 0 ] = salariedEmployee;          

      employees[ 1 ] = commissionEmployee; 

      employees [2] = worker3;


      System.out.println( "Employees processed polymorphically:\n" );

      // generically process each element in array employees
      // 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\n", j, 
            employees[ j ].getClass().getName() );      
   } // end main
} // end class PayrollSystemTest
È stato utile?

Soluzione

private double earnings= hours *wage;

This is not like excel, don't expect the value to be recalculated each time hours or wage change.

Because the default value for a double is 0.0, that's why earnings is equals to 0.0.

You have to initialize this value after you initialized hours and wage, most likely in your constructor:

public HourlyEmployee( String first, String last,double payMe,
      double hourlyWage, double hoursWorked ){
      super( first, last );
      setWage( hourlyWage ); // validate and store hourly wage
      setHours( hoursWorked ); // validate and store hours worked
      payMe = earned();
      earnings= hours *wage; 
}

But I'm trying exactly that , that is that the value would be recalculated every time hours or wage change

As suggested in the comments, you can get rid of the earnings variable and return the value in the getPaymentAmount method

@Override
public double getPaymentAmount() {
    return hours*wage;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top