You need to add a new private instance field \"birthDate\" to the Employee class
ID: 3665639 • Letter: Y
Question
You need to add a new private instance field "birthDate" to the Employee class. The “birthDate” field must be a Date data type, not a String! Add get and set methods to Employee class for this new birthDate field.
The birthDate field must be determined from separate integer values, supplied by the user, for the birth month, day, and year. As with all other inputs, these three birth date components must be prompted from and input to class PayrollSystemTest. Once input, these three parameter values must then be supplied to the appropriate modified employee subclass constructor, and then the subclass constructors supply these three values to their parent class, a modified Employee class constructor.
The single birthDate field of a Date data type must be declared and initialized in the Employee class.
In class PayrollSystemTest create an array of Employee variables to store references to the various employee objects. In a loop, calculate the monthly paycheck amount for each Employee (polymorphically), and add a $100.00 bonus to the person's monthly payroll amount if the current month (i.e. November) is the month in which the Employee's birthday occurs.
Your program should input data and create the five employees below on the version of the program you submit for grading. Your program must calculate their monthly salaries for November (assumed to be the "current" month). Assume that the monthly salary is simply four times the weekly salary.
Explanation / Answer
PayrollSystemTest.java
package com.chegg.test;
//PayrollSystemTest.java
//Employee hierarchy test program.
import java.util.Date;
import javax.swing.JOptionPane;
public class PayrollSystemTest {
public static void main(String args[]) {
String first = "";
String last = "";
String ssn = "";
Date dateOfBirth = null;
String monthString;
String dayString;
String yearString;
int month = 0;
int day = 0;
int year = 0;
String salaryString;
double salary = 0;
String wageString;
double wage = 0;
String hoursString;
int hours = 0;
String salesString;
double sales = 0;
String rateString;
String dateOfBirthString;
double rate = 0;
String empTypeString = "";
int empType = 0;
int count;
for (count = 0; count < 5; count++) {
empTypeString = JOptionPane
.showInputDialog(
null,
"What type of employee are you? Enter the number that corresponds to your answer. 1. Salaried 2. Hourly 3. Commissioned 4. Base Plus Commissioned",
"Employee Type", JOptionPane.QUESTION_MESSAGE);
empType = Integer.parseInt(empTypeString);
switch (empType) {
case 1:
first = JOptionPane.showInputDialog(null,
"What is your first name?", "First Name",
JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog(null,
"What is your last name?", "Last Name",
JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog(null,
"What is your social security number?", "SSN",
JOptionPane.QUESTION_MESSAGE);
monthString = JOptionPane.showInputDialog(null,
"What is the month of your birth date?", "Month",
JOptionPane.QUESTION_MESSAGE);
month = Integer.parseInt(monthString);
dayString = JOptionPane.showInputDialog(null,
"What is the day of your birth date?", "Day",
JOptionPane.QUESTION_MESSAGE);
day = Integer.parseInt(dayString);
yearString = JOptionPane.showInputDialog(null,
"What is the year of your birth date?", "Year",
JOptionPane.QUESTION_MESSAGE);
year = Integer.parseInt(yearString);
dateOfBirth = new Date(month, day, year);
salaryString = JOptionPane.showInputDialog(null,
"What is your weekly salary?", "Salary",
JOptionPane.QUESTION_MESSAGE);
salary = Double.parseDouble(salaryString);
break;
case 2:
first = JOptionPane.showInputDialog(null,
"What is your first name?", "First Name",
JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog(null,
"What is your last name?", "Last Name",
JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog(null,
"What is your social security number?", "SSN",
JOptionPane.QUESTION_MESSAGE);
monthString = JOptionPane.showInputDialog(null,
"What is the month of your birth date?", "Month",
JOptionPane.QUESTION_MESSAGE);
month = Integer.parseInt(monthString);
dayString = JOptionPane.showInputDialog(null,
"What is the day of your birth date?", "Day",
JOptionPane.QUESTION_MESSAGE);
day = Integer.parseInt(dayString);
yearString = JOptionPane.showInputDialog(null,
"What is the year of your birth date?", "Year",
JOptionPane.QUESTION_MESSAGE);
year = Integer.parseInt(yearString);
dateOfBirth = new Date(month, day, year);
wageString = JOptionPane.showInputDialog(null,
"What is your hourly wage?", "Hourly Wage",
JOptionPane.QUESTION_MESSAGE);
wage = Double.parseDouble(wageString);
hoursString = JOptionPane.showInputDialog(null,
"How many hours per week do you work?", "Hours Worked",
JOptionPane.QUESTION_MESSAGE);
hours = Integer.parseInt(hoursString);
break;
case 3:
first = JOptionPane.showInputDialog(null,
"What is your first name?", "First Name",
JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog(null,
"What is your last name?", "Last Name",
JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog(null,
"What is your social security number?", "SSN",
JOptionPane.QUESTION_MESSAGE);
monthString = JOptionPane.showInputDialog(null,
"What is the month of your birth date?", "Month",
JOptionPane.QUESTION_MESSAGE);
month = Integer.parseInt(monthString);
dayString = JOptionPane.showInputDialog(null,
"What is the day of your birth date?", "Day",
JOptionPane.QUESTION_MESSAGE);
day = Integer.parseInt(dayString);
yearString = JOptionPane.showInputDialog(null,
"What is the year of your birth date?", "Year",
JOptionPane.QUESTION_MESSAGE);
year = Integer.parseInt(yearString);
dateOfBirth = new Date(month, day, year);
salesString = JOptionPane.showInputDialog(null,
"What are your weekly gross sales?", "Gross Sales",
JOptionPane.QUESTION_MESSAGE);
sales = Double.parseDouble(salesString);
rateString = JOptionPane.showInputDialog(null,
"What percent of commission do you receive?",
"Commsission Rate", JOptionPane.QUESTION_MESSAGE);
rate = Double.parseDouble(rateString);
break;
case 4:
first = JOptionPane.showInputDialog(null,
"What is your first name?", "First Name",
JOptionPane.QUESTION_MESSAGE);
last = JOptionPane.showInputDialog(null,
"What is your last name?", "Last Name",
JOptionPane.QUESTION_MESSAGE);
ssn = JOptionPane.showInputDialog(null,
"What is your social security number?", "SSN",
JOptionPane.QUESTION_MESSAGE);
monthString = JOptionPane.showInputDialog(null,
"What is the month of your birth date?", "Month",
JOptionPane.QUESTION_MESSAGE);
month = Integer.parseInt(monthString);
dayString = JOptionPane.showInputDialog(null,
"What is the day of your birth date?", "Day",
JOptionPane.QUESTION_MESSAGE);
day = Integer.parseInt(dayString);
yearString = JOptionPane.showInputDialog(null,
"What is the year of your birth date?", "Year",
JOptionPane.QUESTION_MESSAGE);
year = Integer.parseInt(yearString);
dateOfBirth = new Date(month, day, year);
salesString = JOptionPane.showInputDialog(null,
"What are your weekly gross sales?", "Gross Sales",
JOptionPane.QUESTION_MESSAGE);
sales = Double.parseDouble(salesString);
rateString = JOptionPane.showInputDialog(null,
"What percent of commission do you receive?",
"Commsission Rate", JOptionPane.QUESTION_MESSAGE);
rate = Double.parseDouble(rateString);
salaryString = JOptionPane.showInputDialog(null,
"What is your base pay?", "Base Pay",
JOptionPane.QUESTION_MESSAGE);
salary = Double.parseDouble(salaryString);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry!", "Error",
JOptionPane.ERROR_MESSAGE);
break;
}
}
// create subclass objects
SalariedEmployee salariedEmployee = new SalariedEmployee(first, last,
ssn, dateOfBirth, salary);
HourlyEmployee hourlyEmployee = new HourlyEmployee(first, last, ssn,
dateOfBirth, wage, hours);
CommissionEmployee commissionEmployee = new CommissionEmployee(first,
last, ssn, dateOfBirth, sales, rate);
BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(
first, last, ssn, dateOfBirth, sales, rate, salary);
// 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.println("Employees processed polymorphically: ");
// generically process each element in array employees
for (Employee currentEmployee : employees) {
System.out.println(currentEmployee); // invokes toString
int currentMonth = currentEmployee.getBirthDate().getMonth();
if (currentMonth == 11) {
if (currentEmployee instanceof SalariedEmployee) {
SalariedEmployee employee = (SalariedEmployee) currentEmployee;
double oldSalary = employee.getWeeklySalary();
employee.setWeeklySalary(4 * oldSalary + 100);
System.out
.printf("new weekly salary with adding 100 bonus if born on month november is: $%,.2f ",
employee.getWeeklySalary());
}
}
// 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());
} // 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
CommissionEmployee.Java
package com.chegg.test;
import java.util.Date;
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) {
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() * 4;
} // 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
BasePlusCommissionEmployee.java
package com.chegg.test;
import java.util.Date;
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) {
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() * 4 + 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
Employee.java
package com.chegg.test;
import java.util.Date;
//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 bDate) {
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = bDate;
} // 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
/**
* @return the birthDate
*/
public Date getBirthDate() {
return birthDate;
}
/**
* @param birthDate
* the birthDate to set
*/
public void setBirthDate(Date birthDate) {
this.birthDate = 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
SalariedEmployee.java
package com.chegg.test;
import java.util.Date;
//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 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) {
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() * 4;
} // end method earnings
// 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
HourlyEmployee.java
package com.chegg.test;
import java.util.Date;
//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 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) {
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() * 4;
else
return (40 * getWage() + (getHours() - 40) * getWage() * 1.5) * 4;
} // 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
Output:
Employees processed polymorphically:
salaried employee: wer we
social security number: 234
weekly salary: $2,800.00
earned $11,200.00
hourly employee: wer we
social security number: 234
hourly wage: $200.00; hours worked: 34.00
earned $27,200.00
commission employee: wer we
social security number: 234
gross sales: $2,345.00; commission rate: 0.00
earned $0.00
base-salaried commission employee: wer we
social security number: 234
gross sales: $2,345.00; commission rate: 0.00; base salary: $2,800.00
new base salary with 10% increase is: $3,080.00
earned $12,320.00
Employee 0 is a com.chegg.test.SalariedEmployee
Employee 1 is a com.chegg.test.HourlyEmployee
Employee 2 is a com.chegg.test.CommissionEmployee
Employee 3 is a com.chegg.test.BasePlusCommissionEmployee
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.