10.12 (Payroll System Modification) Modify the payroll system of Figs. 10.4–10.9
ID: 3861015 • Letter: 1
Question
10.12 (Payroll System Modification)
Modify the payroll system of Figs. 10.4–10.9 to include private instance variable birthDate in class Employee.
Use class Date of Fig. 8.7 to represent an employee’s birthday. Add get methods to class Date.
Assume that payroll is processed once per month. Create an array of Employee variables to store references to the various employee objects.
In a loop, calculate the payroll for each Employee (polymorphically), and add a $100.00 bonus to the person’s payroll amount if the current month is the one in which the Employee’s birthday occurs.
1) cube ram WinExplanation / Answer
Date.Java
package payrollsystem;
public class Date
{
private int month;
private int day;
private int year;
public Date( int theMonth, int theDay, int theYear )
{
month = theMonth;
year = theYear;
day = checkDay( theDay ); // validate day
}
public void setMonth(int theMonth)
{
month = theMonth;
}
public int getMonth()
{
if ( month > 0 && month <= 12 )
return month;
return 0;
}
private int checkDay( int testDay )
{
int daysPerMonth[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
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;
return 0;
}
public String toString()
{
return String.format( "%d/%d/%d", month, day, year );
}
}
Employee.Java
package payrollsystem;
public abstract class Employee
{
private String firstName;
private String lastName;
private String SSN;
private Date birthday;
public Employee( String first, String last, String ssn , Date DayOfBirth)
{
firstName = first;
lastName = last;
SSN = ssn;
birthday = DayOfBirth;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setSSN( String ssn )
{
SSN = ssn; // should validate
}
public String getSSN()
{
return SSN;
}
public void setBirthday(Date DayOfBirth)
{
birthday = DayOfBirth;
}
public Date getBirthday()
{
return birthday;
}
public String toString()
{
return String.format( "%s %s social security number: %s birthday: %s ",
getFirstName(), getLastName(), getSSN(), getBirthday());
}
public abstract double earnings();
}
HourlyEmployee.Java
package payrollsystem;
public class HourlyEmployee extends Employee
{
private double wage;
private double hours;
public HourlyEmployee( String first, String last, String ssn, Date DayOfBirth, double hourlyWage, double hoursWorked )
{
super( first, last, ssn, DayOfBirth);
setWage( hourlyWage );
setHours( hoursWorked );
}
public final void setWage( double hourlyWage )
{
wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
}
public double getWage()
{
return wage;
}
public final void setHours( double hoursWorked )
{
hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
hoursWorked : 0.0;
}
public double getHours()
{
return hours;
}
public double earnings()
{
if ( getHours() <= 40 )
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
}
@Override
public String toString()
{
return String.format( "hourly employee: %s %s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours() );
}
}
SalariedEmployee.Java
package payrollsystem;
public class SalariedEmployee extends Employee
{
private double weeklySalary;
public SalariedEmployee( String first, String last, String ssn, Date DayOfBirth, double salary )
{
super( first, last, ssn, DayOfBirth);
setWeeklySalary( salary );
}
public final void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
public double earnings()
{
return getWeeklySalary();
}
public String toString()
{
return String.format( "salaried employee: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
}
}
CommossionEmployee.java
package payrollsystem;
public class CommissionEmployee extends Employee
{
private double grossSales;
private double commissionRate;
public CommissionEmployee( String first, String last, String ssn, Date DayOfBirth, double sales, double rate )
{
super( first, last, ssn, DayOfBirth);
setGrossSales( sales );
setCommissionRate( rate );
}
public final void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
}
public double getCommissionRate()
{
return commissionRate;
}
public final void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
}
public double getGrossSales()
{
return grossSales;
}
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
public String toString()
{
return String.format( "%s: %s %s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
}
}
PayrollSystem.Java
package payrollsystem;
public class PayrollSystemTest
{
public static void main( String args[] )
{
Date currentDate = new Date(6,20,2010);
System.out.printf( "Current Date is: %s ", currentDate.toString() );
System.out.println("###################################");
SalariedEmployee salariedEmployee =
new SalariedEmployee( "John", "Smith", "111-11-1111", new Date(5,11,1984),800.00 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( "Karen", "Price", "222-22-2222", new Date(6,15,1988),16.75, 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", new Date(8,25,1974),10000, .06 );
System.out.println( "Employees processed individually: " );
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() );
Employee employees[] = new Employee[ 5 ];
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
System.out.println("###################################");
System.out.println( "Employees processed polymorphically: " );
for ( Employee currentEmployee : employees )
{
System.out.println( currentEmployee ); // invokes toString
if ( currentEmployee instanceof BasePlusCommissionEmployee )
{
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() );
}
if(currentDate.getMonth()==currentEmployee.getBirthday().getMonth())
{
System.out.printf("earned $%,.2f. %s ", currentEmployee.earnings() + 100.00,
"Note: added a $100 bonus to your payroll amount for birthday!!!" );
}else{
System.out.printf("earned $%,.2f ", currentEmployee.earnings() );
}
}
for ( int j = 0; j < employees.length; j++ )
System.out.printf( "Employee %d is a %s ", j,
employees[ j ].getClass().getSimpleName() );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.