Modify code to include private instance variable birthDate in class Employee. Us
ID: 3640563 • Letter: M
Question
Modify code to include private instance variable birthDate in class Employee. Use class Date 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.Here is the code to modify including the test code to modify as well.
I put a large space between classes
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
public Employee( String first, String last, String ssn )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
}
public void setFirstName( String first )
{
firstName = first; // should validate
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last; // should validate
}
public String getLastName()
{
return lastName;
}
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn;
}
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
}
@Override
public String toString()
{
return String.format( "%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber() );
}
public abstract double earnings();
}
public class Date
{
private int month;
private int day;
private int year;
private static final int[] daysPerMonth = // days in each month
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
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 );
}
private int checkMonth( int testMonth )
{
if ( testMonth > 0 && testMonth <= 12 )
return testMonth;
else
throw new IllegalArgumentException( "month must be 1-12" );
}
private int checkDay( int testDay )
{
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
throw new IllegalArgumentException(
"day out-of-range for the specified month and year" );
}
public String toString()
{
return String.format( "%d/%d/%d", month, day, year );
}
}
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,
double hourlyWage, double hoursWorked )
{
super( first, last, ssn );
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 )
{
if ( hourlyWage >= 0.0 )
wage = hourlyWage;
else
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0" );
} // end method setWage
// return wage
public double getWage()
{
return wage;
} // end method getWage
// set hours worked
public void setHours( double hoursWorked )
{
if ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) )
hours = hoursWorked;
else
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0" );
} // end method setHours
// return hours worked
public double getHours()
{
return hours;
} // end method getHours
// calculate earnings; override abstract method earnings in Employee
@Override
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
@Override
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
public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee( String first, String last, String ssn,
double salary )
{
super( first, last, ssn ); // pass to Employee constructor
setWeeklySalary( salary ); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary( double salary )
{
if ( salary >= 0.0 )
weeklySalary = salary;
else
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0" );
} // end method setWeeklySalary
// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
return getWeeklySalary();
} // end method earnings
// return String representation of SalariedEmployee object
@Override
public String toString()
{
return String.format( "salaried employee: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
} // end method toString
} // end class SalariedEmployee
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,
double sales, double rate )
{
super( first, last, ssn );
setGrossSales( sales );
setCommissionRate( rate );
} // end five-argument CommissionEmployee constructor
// set commission rate
public void setCommissionRate( double rate )
{
if ( rate > 0.0 && rate < 1.0 )
commissionRate = rate;
else
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0" );
} // end method setCommissionRate
// return commission rate
public double getCommissionRate()
{
return commissionRate;
} // end method getCommissionRate
// set gross sales amount
public void setGrossSales( double sales )
{
if ( sales >= 0.0 )
grossSales = sales;
else
throw new IllegalArgumentException(
"Gross sales must be >= 0.0" );
} // end method setGrossSales
// return gross sales amount
public double getGrossSales()
{
return grossSales;
} // end method getGrossSales
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings()
{
return getCommissionRate() * getGrossSales();
} // end method earnings
// return String representation of CommissionEmployee object
@Override
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
public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary; // base salary per week
// six-argument constructor
public BasePlusCommissionEmployee( String first, String last,
String ssn, double sales, double rate, double salary )
{
super( first, last, ssn, sales, rate );
setBaseSalary( salary ); // validate and store base salary
} // end six-argument BasePlusCommissionEmployee constructor
// set base salary
public void setBaseSalary( double salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw new IllegalArgumentException(
"Base salary must be >= 0.0" );
} // end method setBaseSalary
// return base salary
public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary
// calculate earnings; override method earnings in CommissionEmployee
@Override
public double earnings()
{
return getBaseSalary() + super.earnings();
} // end method earnings
// return String representation of BasePlusCommissionEmployee object
@Override
public String toString()
{
return String.format( "%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary() );
} // end method toString
} // end class BasePlusCommissionEmployee
Explanation / Answer
import java.util.Calendar;
public class EmployeesBirthdayReport {
public static void main(String[] args) {
Date birthDate =null;
Employee[] emp = new Employee[4];
birthDate = new Date(12,12,1980);
emp[0] = new HourlyEmployee ("John", "Carter", "111-111-11", birthDate,20.0, 40);
birthDate = new Date(7,7,1984);
emp[1] = new SalariedEmployee ("Kobe", "Bryan", "111-111-11",birthDate,40000);
birthDate = new Date(3,3,1982);
emp[2] = new CommissionEmployee("Paris", "Hilton", "111-111-11", birthDate, 50, 0.5);
birthDate = new Date(1,3,1970);
emp[3] = new BasePlusCommissionEmployee("Dr.", "Evil", "111-111-11", birthDate,40, 0.9,40000);
java.util.Calendar cal = java.util.Calendar.getInstance();
int currentMonth = cal.get(Calendar.MONTH)+1;
Employee employee =null;
System.out.println("Employees birthday this month : ");
System.out.println("================================");
for(int i=0; i
if(emp[i] instanceof HourlyEmployee) {
employee = emp[i];
} else if (emp[i] instanceof SalariedEmployee) {
employee = emp[i];
} else if (emp[i] instanceof CommissionEmployee) {
employee = emp[i];
} else if (emp[i] instanceof BasePlusCommissionEmployee) {
employee = emp[i];
}
Date birthDay = employee.getBirthDate();
if(birthDay.getMonth() == currentMonth) {
System.out.println(employee.getFirstName()+" "+employee.getLastName());
}
}
}
}
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 birthdate,
double hourlyWage, double hoursWorked) {
super(first, last, ssn, birthdate);
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) {
if (hourlyWage >= 0.0) {
wage = hourlyWage;
} else {
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0");
}
} // end method setWage
// return wage
public double getWage() {
return wage;
} // end method getWage
// set hours worked
public void setHours(double hoursWorked) {
if ((hoursWorked >= 0.0) && (hoursWorked <= 168.0)) {
hours = hoursWorked;
} else {
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");
}
} // end method setHours
// return hours worked
public double getHours() {
return hours;
} // end method getHours
// calculate earnings; override abstract method earnings in Employee
@Override
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
@Override
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
public class SalariedEmployee extends Employee {
private double weeklySalary;
// four-argument constructor
public SalariedEmployee(String first, String last, String ssn,
Date birthDate, double salary) {
super(first, last, ssn, birthDate); // pass to Employee constructor
setWeeklySalary(salary); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary(double salary) {
if (salary >= 0.0) {
weeklySalary = salary;
} else {
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
}
} // end method setWeeklySalary
// return salary
public double getWeeklySalary() {
return weeklySalary;
} // end method getWeeklySalary
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings() {
return getWeeklySalary();
} // end method earnings
// return String representation of SalariedEmployee object
@Override
public String toString() {
return String.format("salaried employee: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary());
} // end method toString
} // end class SalariedEmployee
public class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary; // base salary per week
// six-argument constructor
public BasePlusCommissionEmployee(String first, String last,
String ssn, Date birthDate, double sales, double rate, double salary) {
super(first, last, ssn, birthDate, sales, rate);
setBaseSalary(salary); // validate and store base salary
} // end six-argument BasePlusCommissionEmployee constructor
// set base salary
public void setBaseSalary(double salary) {
if (salary >= 0.0) {
baseSalary = salary;
} else {
throw new IllegalArgumentException(
"Base salary must be >= 0.0");
}
} // end method setBaseSalary
// return base salary
public double getBaseSalary() {
return baseSalary;
} // end method getBaseSalary
// calculate earnings; override method earnings in CommissionEmployee
@Override
public double earnings() {
return getBaseSalary() + super.earnings();
} // end method earnings
// return String representation of BasePlusCommissionEmployee object
@Override
public String toString() {
return String.format("%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary());
} // end method toString
} // end class BasePlusCommissionEmployee
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 birthDate,double sales, double rate) {
super(first, last, ssn, birthDate);
setGrossSales(sales);
setCommissionRate(rate);
} // end five-argument CommissionEmployee constructor
// set commission rate
public void setCommissionRate(double rate) {
if (rate > 0.0 && rate < 1.0) {
commissionRate = rate;
} else {
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
}
} // end method setCommissionRate
// return commission rate
public double getCommissionRate() {
return commissionRate;
} // end method getCommissionRate
// set gross sales amount
public void setGrossSales(double sales) {
if (sales >= 0.0) {
grossSales = sales;
} else {
throw new IllegalArgumentException(
"Gross sales must be >= 0.0");
}
} // end method setGrossSales
// return gross sales amount
public double getGrossSales() {
return grossSales;
} // end method getGrossSales
// calculate earnings; override abstract method earnings in Employee
@Override
public double earnings() {
return getCommissionRate() * getGrossSales();
} // end method earnings
// return String representation of CommissionEmployee object
@Override
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
public class Date {
private int month;
private int day;
private int year;
private static final int[] daysPerMonth = // days in each month
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
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);
}
private int checkMonth(int testMonth) {
if (testMonth > 0 && testMonth <= 12) {
return testMonth;
} else {
throw new IllegalArgumentException("month must be 1-12");
}
}
private int checkDay(int testDay) {
if (testDay > 0 && testDay <= daysPerMonth[ getMonth()]) {
return testDay;
}
if (getMonth() == 2 && testDay == 29 && (getYear() % 400 == 0
|| (getYear() % 4 == 0 && getYear() % 100 != 0))) {
return testDay;
}
throw new IllegalArgumentException(
"day out-of-range for the specified month and year");
}
public String toString() {
return String.format("%d/%d/%d", getMonth(), getDay(), getYear());
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
}
public abstract class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthDate;
public Employee(String first, String last, String ssn, Date birthdate) {
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = birthdate;
}
public void setFirstName(String first) {
firstName = first; // should validate
}
public String getFirstName() {
return firstName;
}
public void setLastName(String last) {
lastName = last; // should validate
}
public String getLastName() {
return lastName;
}
public void setSocialSecurityNumber(String ssn) {
socialSecurityNumber = ssn;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
@Override
public String toString() {
return String.format("%s %s social security number: %s birthday : %s",
getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthDate().toString());
}
public abstract double earnings();
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.