Modify the payroll system of figs. 10.4 - 10.9 to include an additional Employee
ID: 665963 • Letter: M
Question
Modify the payroll system of figs. 10.4 - 10.9 to include an additional Employee subclass PieceWorker that represents an employee whose pay is based on the number of pieces of merchandise produced. Class PieceWorker should contain private instance variables wage (to store the employee's wage per piece) and pieces (to store the number of pieces produced). Provide a concrete implementation of method earnings in class PieceWorker that calcuates the employee's earnings by multiplying the number of pieces produced by the wage per piece. Create an array of Employee variables to store references to objects of each concrete class in the new Employee hierarchy (SalariedEmployee, CommissionEmployee, HourlyEmployee, BasePlusCommissionEmployee, and now PieceWorker). For each Employee, display its String representation and earnings.
Explanation / Answer
// PieceWorker class extends Employee.
public class PieceWorker extends Employee
{ private double wage; //wage per piece
private int pieces; //number of pieces
// six-argument constructor
public PieceWorker( String first, String last, String ssn, Date dateOfBirth, double employeeWage, int numberOfPieces )
{
super( first, last, ssn, dateOfBirth );
setWage( employeeWage ); // validate and store employee wage
setPieces( numberOfPieces ); // validate and store number of pieces
} // end six-argument HourlyEmployee constructor
// set wage
public void setWage( double employeeWage )
{
wage = ( employeeWage < 0.0 ) ? 0.0 : employeeWage;
} // end method setWage
// return wage
public double getWage() { return wage; }
// end method getWage
// set number of pieces
public void setPieces( int numberOfPieces )
{
pieces = ( ( numberOfPieces >= 0 ) && ( numberOfPieces <= 168 ) ) ? numberOfPieces : 0; }
// end method setPieces
// return pieces
public double getPieces()
{
return pieces;
} // end method getPieces
// calculate earnings;
override abstract method earnings in Employee public double earnings()
{
wage = 40.00;
pieces = 40
;//Fig. 10.9: PayrollSystemTest.java
//Employee hierarchy test program.
import java.util.Calendar;
public class PayrollSystemTest
{
public static void main(String args[])
{
Calendar today = Calendar.getInstance();
// create subclass objects Employee[]
employees = new Employee[]
{
new PieceWorker("Yamen", "El-Alam", "555-55-5555"
, new Date(8, 7,1979), 40.00, 40),
new SalariedEmployee("John", "Smith", "111-11-1111",
new Date(8, 3, 1976), 800.00),
new HourlyEmployee("Karen", "Price", "222-22-2222",
new Date(7, 14, 1989), 16.75, 40.00),
new CommissionEmployee("Sue", "Jones", "333-33-3333",
new Date(2, 10, 1978), 10000, .06),
new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444",
new Date(11, 12, 1950), 5000, .04, 300) };
System.out.println("Employees processed individually: ");
for (Employee employee : employees)
{
System.out.println(employee.getClass().
getSimpleName());
System.out.println("Employee Birth Day:" + employee.getBirthDate());
System.out.printf("%s %s: $%,.2f ",
employee.toString(),
"earned", employee.earnings());
Date birthday = employee.getBirthDate();
if (today.get(Calendar.MONTH)+1 == birthday.getMonth())
{
//employee.getWage() += 100.00;
System.out.println("birthday bonus: $100.00");
}
System.out.println();
}
//end for
// creates an array of employees (4 total)
/* Employee[]
employees = new Employee[4];
// initialize array with Employees
employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
employees[2] = commissionEmployee; employees[3] = basePlusCommissionEmployee;
// employees[ 4 ] = Date;
*/ System.out.println("Employees processed polymorphically: ");
// generically process each element in array employees for (Employee currentEmployee : employees)
{
System.out.println(currentEmployee);
// invokes toString
// determine whether element is a BasePlusCommissionEmployee
if (currentEmployee instanceof BasePlusCommissionEmployee)
{
// downcast Employee reference to
// BasePlusCommissionEmployee reference BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee) currentEmployee;
double oldBaseSalary = employee.getBaseSalary(); employee.setBaseSalary(1.10 * oldBaseSalary);
System.out.printf( "new base salary with 10%% increase is: $%,.2f ",
employee.getBaseSalary()); }
// end if System.out.printf("earned $%,.2f ", currentEmployee.earnings());
Date birthdate = currentEmployee.getBirthDate();
if (today.get(Calendar.MONTH) + 1 == birthdate.getMonth())
System.out.println("birthday bonus: $100.00"); System.out.println(); }
// 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 ***********************************************
// Fig. 10.8: BasePlusCommissionEmployee.java
// BasePlusCommissionEmployee class extends CommissionEmployee. public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary; // base salary per week
// six-argument constructor public BasePlusCommissionEmployee( String first, String last, String ssn, Date dateOfBirth, double sales, double rate, double salary )
{
super( first, last, ssn,dateOfBirth, sales, rate ); setBaseSalary( salary );
// validate and store base salary
}
// end six-argument BasePlusCommissionEmployee constructor
// set base salary public void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
// non-negative
}
// end method setBaseSalary
// return base salary public double getBaseSalary()
{
return baseSalary;
}
// end method getBaseSalary
// calculate earnings; override method earnings in CommissionEmployee public double earnings()
{
return getBaseSalary() + super.earnings();
}
// end method earnings
// return String representation of BasePlusCommissionEmployee object public String toString()
{
return String.format( "%s %s; %s: $%,.2f", "base-salaried", super.toString(), "base salary", getBaseSalary() );
}
// end method toString
}
// end class BasePlusCommissionEmployee ***************************************************** // Fig. 10.7: CommissionEmployee.java
// CommissionEmployee class extends Employee.
public class CommissionEmployee extends Employee { private double grossSales;
// gross weekly sales private double commissionRate;
// commission percentage
// five-argument constructor public CommissionEmployee( String first, String last, String ssn, Date dateOfBirth, double sales, double rate )
{
super( first, last, ssn, dateOfBirth ); setGrossSales( sales );
setCommissionRate( rate ); }
// end five-argument CommissionEmployee constructor
// set commission rate public void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
}
// end method setCommissionRate
// return commission rate public double getCommissionRate()
{
return commissionRate;
}
// end method getCommissionRate
// set gross sales amount public void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales; }
// end method setGrossSales
// return gross sales amount public double getGrossSales()
{
return grossSales;
}
// end method getGrossSales
// calculate earnings; override abstract method earnings in Employee public double earnings()
{
return getCommissionRate() * getGrossSales();
}
// end method earnings // return String representation of CommissionEmployee object public String toString()
{
return String.format( "%s: %s %s: $%,.2f; %s: %.2f", "commission employee", super.toString(), "gross sales", getGrossSales(), "commission rate", getCommissionRate() ); }
// end method toString
} // end class CommissionEmployee ********************************************************
// Fig. 8.7: Date.java
// Date class declaration. public class Date { private int month;
// 1-12 private int day;
// 1-31 based on month private int year;
// any year
// constructor: call checkMonth to confirm proper value for month;
// call checkDay to confirm proper value for day public Date( int theMonth, int theDay, int theYear )
{
month = checkMonth( theMonth );
// validate month year = theYear;
// could validate year day = checkDay( theDay );
// validate day System.out.printf( "Date object constructor for date %s ", this );
}
// end Date constructor // utility method to confirm proper month value private int checkMonth( int testMonth )
{
if ( testMonth > 0 && testMonth <= 12 )
// validate month return testMonth;
else
// month is invalid { System.out.printf( "Invalid month (%d) set to 1.", testMonth ); return 1;
// maintain object in consistent state
}
// end else
}
// end method checkMonth
// utility method to confirm proper day value based on month and year private int checkDay( int testDay )
{
int daysPerMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// check if day in range for month if ( testDay > 0 && testDay <= daysPerMonth[ month ] ) return testDay;
// check for leap year if ( month == 2 && testDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
) return testDay; System.out.printf( "Invalid day (%d) set to 1.", testDay ); return 1;
// maintain object in consistent state
}
// end method checkDay public void setMonth(int theMonth) { month = theMonth; }
//end setMonth public int getMonth() { return month; }
//end getMonth public void setDay(int theDay) { day = theDay;
}//end setDay public int getDay() { return day; }
//end getDay public void setYear(int theYear) { year = theYear; }
//end setYear public int getYear() { return year; }//end getYear
// return a String of the form month/day/year public String toString()
{
return String.format( "%d/%d/%d",month, day, year ); }
// end method toString }
// end class Date *******************************************************
// Fig. 10.4: Employee.java
// Employee abstract superclass.
public abstract class Employee { private String firstName; private String lastName;
private String socialSecurityNumber; private Date birthDate;
// three-argument constructor public Employee( String first, String last, String ssn, Date dateOfBirth )
{
firstName = first; lastName = last; socialSecurityNumber = ssn; birthDate = dateOfBirth; }
// end three-argument Employee constructor
// set first name public void setFirstName( String first )
{
firstName = first;
}
// end method setFirstName
// return first name public String getFirstName()
{
return firstName; }
// end method getFirstName
// set last name public void setLastName( String last )
{
lastName = last; }
// end method setLastName
// return last name public String getLastName()
{
return lastName;
}
// end method getLastName
// set social security number public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn;
// should validate
}
// end method setSocialSecurityNumber
// return social security number public String getSocialSecurityNumber()
{
return socialSecurityNumber
}
// end method getSocialSecurityNumber public void setBirthDate(Date dateOfBirth)
{
birthDate = dateOfBirth;
}
public Date getBirthDate()
{
return birthDate;
}
// return String representation of Employee object public String toString()
{
return String.format( "%s %s social security number: %s",
getFirstName(),
getLastName(),
getSocialSecurityNumber(),
getBirthDate() );
}
// end method toString
// abstract method overridden by subclasses public abstract double earnings();
// no implementation here }
// end abstract class Employee *************************************************************** // Fig. 10.6: HourlyEmployee.java
// HourlyEmployee class extends Employee.
public class HourlyEmployee extends Employee
{
private double wage;
// wage per hour private double hours;
// hours worked for week
// five-argument constructor public HourlyEmployee( String first, String last, String ssn, Date dateOfBirth, double hourlyWage, double hoursWorked )
{
super( first, last, ssn, dateOfBirth );
setWage( hourlyWage );
// validate and store hourly wage setHours( hoursWorked );
// validate and store hours worked
}
// 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 <= 168.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 earnings()
{
if ( getHours() <= 40 ) // no overtime return getWage() * getHours(); else return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5; }
// end method earnings
// return String representation of HourlyEmployee object public String toString() {
return String.format( "hourly employee: %s %s: $%,.2f; %s: %,.2f", super.toString(),
"hourly wage", getWage(), "hours worked",
getHours() ); }
// end method toString }
// end class HourlyEmployee *****************************************************
// Fig. 10.5: SalariedEmployee.java // SalariedEmployee class extends Employee.
public class SalariedEmployee extends Employee { private double weeklySalary;
// four-argument constructor public SalariedEmployee( String first, String last, String ssn, Date dateOfBirth, double salary )
{
super( first, last, ssn, dateOfBirth );
// pass to Employee constructor setWeeklySalary( salary );
// validate and store salary }
// end four-argument SalariedEmployee constructor
// set salary public void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary; }
// end method setWeeklySalary
// return salary public double getWeeklySalary()
{
return weeklySalary;
}
// end method getWeeklySalary
// calculate earnings;
override abstract method earnings in Employee public double earnings()
{
return getWeeklySalary();
}
// end method earning
// return String representation of SalariedEmployee object public String toString()
{
return String.format( "salaried employee: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() ); }
// end method toString }
// end class SalariedEmployee
return getWage() * getPieces();
}
// end method earnings
// return String representation of HourlyEmployee object public String toString()
// return String representation of HourlyEmployee object public String toString()
{
return String.format( "PieceWorker: %s %s: $%,.2f; %s: %,.2f",
super.toString(), "Employee Wage", getWage(), "Number Of Pieces", getPieces() );
}
// end method toString
}
// end class PieceWorker
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.