import java.util.Scanner;
// PayrollSystemTest.java
// Employee hierarchy test program.
public class PayrollSystemTest {
private static final int BONUS_MONTH = 11;
public static void main(String args[]) {
// create scanner object to get values from user
Scanner input = new Scanner(System.in);
System.out.println("Employees process polymorphically:\n");
SalariedEmployee salariedEmployee = getSalariedEmployeeObject(input);
HourlyEmployee hourlyEmployee = getHourlyEmployeeObject(input);
CommissionEmployee commissionEmployee = getCommissionEmployeeObject(input);
BasePlusCommissionEmployee basePlusCommissionEmployee = getBasePlusCommissionEmployeeObject(input);
System.out.println("Employees processed individually:\n");
System.out.printf("%s\n%s: $%,.2f\n\n", salariedEmployee, "earned", salariedEmployee.earnings());
System.out.printf("%s\n%s: $%,.2f\n\n", hourlyEmployee, "earned", hourlyEmployee.earnings());
System.out.printf("%s\n%s: $%,.2f\n\n", commissionEmployee, "earned", commissionEmployee.earnings());
System.out.printf("%s\n%s: $%,.2f\n\n", basePlusCommissionEmployee, "earned",
basePlusCommissionEmployee.earnings());
// 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:\n");
// generically process each element in array employees
double earnings = 0;
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\n", employee.getBaseSalary());
} // end if
// Add $100 bonus if birth month is November while calculating
// monthly salary
if (currentEmployee.getBirthDate().getMonth() == BONUS_MONTH) {
earnings = (currentEmployee.earnings() * 4) + 100;
} else {
earnings = currentEmployee.earnings() * 4;
}
// Print monthly salary
System.out.printf("earned $%,.2f\n\n", 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\n", j, employees[j].getClass().getName());
} // end main
private static SalariedEmployee getSalariedEmployeeObject(Scanner input) {
System.out.println("Please enter first name.");
String firstName = input.next();
System.out.println("Please enter last name.");
String lastName = input.next();
System.out.println("Please enter SSN.");
String ssn = input.next();
System.out.println("Please enter Month of Birth.");
int month = input.nextInt();
System.out.println("Please enter Day of Birth.");
int day = input.nextInt();
System.out.println("Please enter Year of Birth.");
int year = input.nextInt();
System.out.println("Please enter Salary.");
double salary = input.nextDouble();
// create subclass objects
SalariedEmployee salariedEmployee = new SalariedEmployee(firstName, lastName, ssn, month, day, year, salary);
return salariedEmployee;
}
private static HourlyEmployee getHourlyEmployeeObject(Scanner input) {
System.out.println("\nHourly Employee Details");
System.out.println("Please enter first name.");
String firstName = input.next();
System.out.println("Please enter last name.");
String lastName = input.next();
System.out.println("Please enter SSN.");
String ssn = input.next();
System.out.println("Please enter Month of Birth.");
int month = input.nextInt();
System.out.println("Please enter Day of Birth.");
int day = input.nextInt();
System.out.println("Please enter Year of Birth.");
int year = input.nextInt();
System.out.println("Please enter hourly wages.");
double hourlyWages = input.nextDouble();
System.out.println("Please enter hours worked.");
double hoursWorked = input.nextDouble();
HourlyEmployee hourlyEmployee = new HourlyEmployee(firstName, lastName, ssn, month, day, year, hourlyWages,
hoursWorked);
return hourlyEmployee;
}
private static CommissionEmployee getCommissionEmployeeObject(Scanner input) {
System.out.println("\nCommission Employee Details");
System.out.println("Please enter first name.");
String firstName = input.next();
System.out.println("Please enter last name.");
String lastName = input.next();
System.out.println("Please enter SSN.");
String ssn = input.next();
System.out.println("Please enter Month of Birth.");
int month = input.nextInt();
System.out.println("Please enter Day of Birth.");
int day = input.nextInt();
System.out.println("Please enter Year of Birth.");
int year = input.nextInt();
System.out.println("Please enter gross sales.");
double grossSales = input.nextDouble();
System.out.println("Please enter commission rate.");
double commissionRate = input.nextDouble();
CommissionEmployee commissionEmployee = new CommissionEmployee(firstName, lastName, ssn, month, day, year,
grossSales, commissionRate);
return commissionEmployee;
}
private static BasePlusCommissionEmployee getBasePlusCommissionEmployeeObject(Scanner input) {
System.out.println("\nBase Plus Commission Employee Details");
System.out.println("Please enter first name.");
String firstName = input.next();
System.out.println("Please enter last name.");
String lastName = input.next();
System.out.println("Please enter SSN.");
String ssn = input.next();
System.out.println("Please enter Month of Birth.");
int month = input.nextInt();
System.out.println("Please enter Day of Birth.");
int day = input.nextInt();
System.out.println("Please enter Year of Birth.");
int year = input.nextInt();
System.out.println("Please enter gross sales.");
double grossSales = input.nextDouble();
System.out.println("Please enter commission rate.");
double commissionRate = input.nextDouble();
System.out.println("Please enter base salary.");
double baseSalary = input.nextDouble();
BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(firstName, lastName,
ssn, month, day, year, grossSales, commissionRate, baseSalary);
return basePlusCommissionEmployee;
}
} // end class PayrollSystemTest
*Edited: please