10.13Payroll System Modigication) Modity the payroll system of Figs. 10.4-10.9 t
ID: 3729989 • Letter: 1
Question
10.13Payroll System Modigication) Modity the payroll system of Figs. 10.4-10.9 to indude orivate instance variable birthDate in class Employee. Use class D Dlovec'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' payroll amount if the current month is the one in which the Employee's birthday occurs ate of Fig. 8.7 to represent an em s birt once per moExplanation / Answer
Solution:
code:
/**Employee.java**/
public abstract class Employee
{
private final String firstName;
private final String lastName;
private final String SocialSecurityNumber;
private final Date birthDate;
//constructor
public Employee(String firstName,String lastName, String socialSecurityNumber,Date birthDate)
{
this.firstName=firstName;
this.lastName=lastName;
this.SocialSecurityNumber=socialSecurityNumber;
this.birthDate=birthDate;
}
//return first name
public String getFirstName()
{
return firstName;
}
//return last name
public String getLastName()
{
return lastName;
}
//return social security number
public String getSocialSecurityNumber()
{
return SocialSecurityNumber;
}
//return date of birth
public Date getBirthDate()
{
return birthDate;
}
//return String representation of Employee object
public String toString()
{
return String.format("%s %s%nsocial security number: %s%nDOB: %s",getFirstName(), getLastName(), getSocialSecurityNumber(),getBirthDate());
}
//abstract method
public abstract double earnings();
}//end class Employee
/**SalariedEmployee.java**/
import java.util.IllegalFormatConversionException;
public class SalariedEmployee extends Employee
{
private double weeklySalary;
//constructor
public SalariedEmployee(String firstName,String lastName,String socialSecurityNumber, double weeklySalary,Date birthDate)
{
super(firstName,lastName,socialSecurityNumber,birthDate);
if(weeklySalary <0.0)
throw new IllegalArgumentException("Weekly salary must be >= 0.0");
this.weeklySalary=weeklySalary;
}
public void setWeeklySalary(double weeklySalary)
{
if(weeklySalary <0.0)
throw new IllegalArgumentException("Weekly salary must be >= 0.0");
this.weeklySalary=weeklySalary;
}
public double getWeeklySalary()
{
return weeklySalary;
}
public String toString()
{
return String.format("salaried employee: %s%n%s: $%,.2f",super.toString(),"weekly salary",getWeeklySalary());
}
@Override
public double earnings()
{
return weeklySalary;
}
}
/**HourlyEmployee.java**/
public class HourlyEmployee extends Employee
{
private double wage;
private double hours;
//constructor
public HourlyEmployee(String firstName, String lastName,String socialSecurityNumber, double wage, double hours,Date birthDate)
{
super(firstName,lastName,socialSecurityNumber,birthDate);
if(wage <0.0)//validate wage
throw new IllegalArgumentException("Hourly wage must be >= 0.0");
if((hours < 0.0) || (hours >168.0))// validate hours
throw new IllegalArgumentException("Hours worked must be <= 168.0");
this.wage=wage;
this.hours=hours;
}
public void setWage(double wage)
{
if(wage <0.0)
throw new IllegalArgumentException("Hourly wage must be >= 0.0");
this.wage=wage;
}
public double getWage()
{
return wage;
}
public void setHours(double hours)
{
if((hours < 0.0) || (hours >168.0))
throw new IllegalArgumentException("Hours worked must be <= 168.0");
this.hours=hours;
}
public double getHours()
{
return hours;
}
@Override
public double earnings()
{
if(getHours()<=40)
return getWage()*getHours();
else
return 40*getWage()+(getHours()-40)*getWage()*1.5;
}
public String toString()
{
return String.format("hourly employee: %s%n%s: $%,.2f; %s: %,.2f",super.toString(),"hourly wage",getWage(),"Hours worked",getHours());
}
}
/**CommissionEmployee.java**/
public class CommissionEmployee extends Employee
{
private double grossSales;
private double commissionRate;
//constructor
public CommissionEmployee(String firstName,String lastName,String socialSecurityNumber, double grossSales,double commissionRate,Date birthDate)
{
super(firstName,lastName,socialSecurityNumber,birthDate);
if(commissionRate <0.0 || commissionRate >=1.0)//validate commission rate
throw new IllegalArgumentException("Commission rate must be > 0.0 and <1.0");
if(grossSales < 0.0)//validate gross sales
throw new IllegalArgumentException("Gross sales must be >= 0.0");
this.grossSales=grossSales;
this.commissionRate=commissionRate;
}
public void setGrossSales(double grossSales)
{
if(grossSales < 0.0)
throw new IllegalArgumentException("Gross sales must be >= 0.0");
this.grossSales=grossSales;
}
public double getGrossSales()
{
return grossSales;
}
public void setCommissionRate(double commissionRate)
{
if(commissionRate <0.0 || commissionRate >=1.0)
throw new IllegalArgumentException("Commission rate must be > 0.0 and <1.0");
this.commissionRate=commissionRate;
}
public double getCommissionRate()
{
return commissionRate;
}
@Override
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
public String toString()
{
return String.format("%s: %s%n%s: $%,.2f; %s: %.2f","commission employee", super.toString(),"gross sales", getGrossSales(),"commission rate", getCommissionRate());
}
}
/**BasePlusCommissionEmployee.java**/
public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary;
//constructor
public BasePlusCommissionEmployee(String firstName,String lastName,String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary,Date birthDate)
{
super(firstName,lastName,socialSecurityNumber,grossSales,commissionRate,birthDate);
if(baseSalary < 0.0 )//validate base salary per week
throw new IllegalArgumentException("Base salary must be >= 0.0");
this.baseSalary=baseSalary;
}
public void setBaseSalary(double baseSalary)
{
if(baseSalary < 0.0 )
throw new IllegalArgumentException("Base salary must be >= 0.0");
this.baseSalary=baseSalary;
}
public double getBaseSalary()
{
return baseSalary;
}
public double earnings()
{
return getBaseSalary()+super.earnings();
}
public String toString()
{
return String.format("%s %s: %s: $%,.2f","base-salaried",super.toString(),"base salary",getBaseSalary());
}
}
/**PayrollSystemTest.java**/
import java.util.Calendar;
public class PayrollSystemTest
{
public static void main(String[] args)
{
//Create subclass objects
SalariedEmployee salariedEmployee=new SalariedEmployee("John","Smith","111-11-1111",800.00,new Date(07,23,2016));
HourlyEmployee hourlyEmployee=new HourlyEmployee("Karen","Price","222-22-2222",16.75,40,new Date(4,2,1981));
CommissionEmployee commissionEmployee= new CommissionEmployee("Sue","Jones","333-33-3333",10000,.06,new Date(5,8,1993));
BasePlusCommissionEmployee basePlusCommissionEmployee=new BasePlusCommissionEmployee("Bob","Lewis","444-44-4444",5000,.04,300,new Date(2,29,2000));
//create four-element Employee array
Employee [] employees=new Employee[4];
//Initialize array with Employees
employees[0]=salariedEmployee;
employees[1]=hourlyEmployee;
employees[2]=commissionEmployee;
employees[3]=basePlusCommissionEmployee;
System.out.printf("Employees processed polymorphically: %n%n");
//calculate the payroll for each Employee (polymorphically)
for(Employee currentEmployee: employees)
{
//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.
if (currentEmployee.getBirthDate().getMonth()==Calendar.getInstance().get(Calendar.MONTH)+1)
{
System.out.printf("%s%n%s: $%,.2f%n",currentEmployee,"earned",currentEmployee.earnings());
System.out.printf("Birthday bonus : $%,.2f%n",100.00);
System.out.printf("earned $%,.2f%n%n", currentEmployee.earnings()+100);
}
//Otherwise
else
System.out.printf("%s%n%s: $%,.2f%n%n",currentEmployee,"earned",currentEmployee.earnings());
}
for(int j=0;j<employees.length;j++)
{
System.out.printf("Employee %d is a %s%n",j,employees[j].getClass().getName());
}
}
}
/**Date.java**/
public class Date
{
private int month;
private int day;
private int year;
private static final int[] daysPerMonth={0,31,28,31,30,31,30,31,31,30,31,30,31};
//construcotr: confirm proper value for month and day given the year
public Date(int month,int day,int year)
{
//check if month in range
if(month <=0 || month >12)
throw new IllegalArgumentException("month ("+month+") must be 1-12");
//Check if day in range for month
if(day <=0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))
throw new IllegalArgumentException("day ("+day+") out-of-range for the specified month and year");
//Check for leap year if month is 2 and day is 29
if(month ==2 && day==29 && !(year % 400 ==0 || (year % 4 ==0 && year % 100 !=0 )))
throw new IllegalArgumentException("day ("+day+") out-of-range for the specified month and year");
this.month=month;
this.day=day;
this.year=year;
}
//return Month
public int getMonth()
{
return month;
}
//return day
public int getDay()
{
return day;
}
//return year
public int getYear()
{
return year;
}
//return a String of the form month/day/year
public String toString()
{
return String.format("%d/%d/%d",getMonth(),getDay(),getYear());
}
}//end class Date
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.