The document defines an Employee class and subclasses for Hourly, Salary, and Commission employees. It includes methods to calculate pay, apply raises, and reset tracking periods. The main method creates sample employees, alters values, calculates pay and bonuses, applies raises, and outputs the results.
6. public class EmployeeTest {
public static void main(String args[]) {
// Create references for our objects
HourlyEmployee hourly;
SalaryEmployee salary;
CommissionEmployee commission;
// The sum of all bonuses will be placed here
double bonusPayout = 0;
// Create and print a new HourlyEmployee
hourly = HourlyFactory();
System.out.printf(" %s ", hourly);
// Create and print a new SalaryEmployee
salary = SalaryFactory();
System.out.printf(" %s ", salary);
// Create and print a new CommissionEmployee
commission = CommissionFactory();
System.out.printf(" %s ", commission);
// Alter some values of Hourly and Commission
System.out.println("Increasing Hourly's hours worked by 50.");
hourly.increaseHours(50);
System.out.println("Increasing Commissions's sales by 150,000. ");
commission.increaseSales(150000);
// Output weekly pay for each Employee
System.out.printf("Hourly Payout: %.2f ", hourly.calculateWeeklyPay());
System.out.printf("Salary Payout: %.2f ", salary.calculateWeeklyPay());
System.out.printf("Commission Payout: %.2f ",
commission.calculateWeeklyPay());
// Find total bonus payout
System.out.println("Finding total bonus payout...");
bonusPayout += hourly.holidayBonus();
bonusPayout += salary.holidayBonus();
bonusPayout += commission.holidayBonus();
System.out.printf("Bonus Payout is %.2f ", bonusPayout);
// Reset the week and apply raises to all Employees
System.out.println("Applying annual raises and resetting week...");
hourly.resetWeek();
7. hourly.annualRaise();
salary.resetWeek();
salary.annualRaise();
commission.resetWeek();
commission.annualRaise();
// Output Employees after changes
System.out.printf("%s %s %s ", hourly, salary, commission);
}
// Retrieve input and create HourlyEmployee
public static HourlyEmployee HourlyFactory() {
System.out.println("Creating HourlyEmployee...");
return new HourlyEmployee("Steve", "Rogers", 'A', 'M', 12345, true,
15.34);
}
// Retrieve input and create SalaryEmployee
public static SalaryEmployee SalaryFactory() {
System.out.println("Creating SalaryEmployee...");
return new SalaryEmployee("Kitty", "Pryde", 'X', 'F', 54321, true,
75000);
}
// Retrieve input and create CommissionEmployee
public static CommissionEmployee CommissionFactory() {
System.out.println("Creating CommissionEmployee...");
return new CommissionEmployee("Johnny", "Storm", 'F', 'L', 976499,
false, 2.5);
}
}
OUTPUT:
Creating HourlyEmployee...
12345
Steve Rogers
Gender:M
Status:Full Time
Wage: 15.34
Hours Worked: 0.0
Creating SalaryEmployee...
8. 54321
Kitty Pryde
Gender:F
Status:Full Time
Salary:75000.0
Creating CommissionEmployee...
976499
Johnny Storm
Gender:L
Status:Part Time
Rate: 2.5
Sales: 0.0
Increasing Hourly's hours worked by 50.
Increasing Commissions's sales by 150,000.
Hourly Payout: 920.40
Salary Payout: 1442.31
Commission Payout: 3750.00
Finding total bonus payout...
Bonus Payout is 2863.60
Applying annual raises and resetting week...
12345
Steve Rogers
Gender:M
Status:Full Time
Wage: 0.77
Hours Worked: 0.0
54321
Kitty Pryde
Gender:F
Status:Full Time
Salary:4500.0
976499
Johnny Storm
Gender:L
Status:Part Time
Rate: 2.52
12. public double holidayBonus() {
return 40 * wage;
}
public void resetWeek() {
this.hoursWorked = 0;
}
}
package employeeType.subTypes;
import java.text.DecimalFormat;
import employeeType.employee.Employee;
public class SalaryEmployee extends Employee {
double salary;
public SalaryEmployee(String firstName, String lastName,
char middleInitial, char gender, int employeeNum, boolean fulltime,
double s) {
super(firstName, lastName, middleInitial, gender, employeeNum, fulltime);
// TODO Auto-generated constructor stub
this.salary = s;
}
public String toString() {
// TODO Auto-generated method stub
return super.toString() + " Salary:" + salary;
}
public double calculateWeeklyPay() {
return salary / 52.00d;
}
public void annualRaise() {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
salary = Double.valueOf(decimalFormat.format(salary * 0.06));
}
public double holidayBonus() {
return salary * 0.03;
}
public void resetWeek() {
}
}
13. package employeeType.subTypes;
import employeeType.employee.Employee;
public class CommissionEmployee extends Employee {
double sales;
double rate;
public CommissionEmployee(String firstName, String lastName,
char middleInitial, char gender, int employeeNum, boolean fulltime,
double r) {
super(firstName, lastName, middleInitial, gender, employeeNum, fulltime);
// TODO Auto-generated constructor stub
this.rate = r;
this.sales = 0.0d;
}
public String toString() {
// TODO Auto-generated method stub
return super.toString() + " Rate: " + rate + " Sales: " + sales;
}
public void increaseSales(double sales) {
if (sales > 0)
this.sales += sales;
else
System.out.println("Error! Not valid sales");
}
public double calculateWeeklyPay() {
return sales * rate / 100;
}
public void annualRaise() {
this.rate += 0.02;
}
public double holidayBonus() {
return 0.00d;
}
public void resetWeek() {
this.sales = 0.0d;
}
}
14. import employeeType.subTypes.CommissionEmployee;
import employeeType.subTypes.HourlyEmployee;
import employeeType.subTypes.SalaryEmployee;
public class EmployeeTest {
public static void main(String args[]) {
// Create references for our objects
HourlyEmployee hourly;
SalaryEmployee salary;
CommissionEmployee commission;
// The sum of all bonuses will be placed here
double bonusPayout = 0;
// Create and print a new HourlyEmployee
hourly = HourlyFactory();
System.out.printf(" %s ", hourly);
// Create and print a new SalaryEmployee
salary = SalaryFactory();
System.out.printf(" %s ", salary);
// Create and print a new CommissionEmployee
commission = CommissionFactory();
System.out.printf(" %s ", commission);
// Alter some values of Hourly and Commission
System.out.println("Increasing Hourly's hours worked by 50.");
hourly.increaseHours(50);
System.out.println("Increasing Commissions's sales by 150,000. ");
commission.increaseSales(150000);
// Output weekly pay for each Employee
System.out.printf("Hourly Payout: %.2f ", hourly.calculateWeeklyPay());
System.out.printf("Salary Payout: %.2f ", salary.calculateWeeklyPay());
System.out.printf("Commission Payout: %.2f ",
commission.calculateWeeklyPay());
// Find total bonus payout
System.out.println("Finding total bonus payout...");
bonusPayout += hourly.holidayBonus();
bonusPayout += salary.holidayBonus();
bonusPayout += commission.holidayBonus();
System.out.printf("Bonus Payout is %.2f ", bonusPayout);
15. // Reset the week and apply raises to all Employees
System.out.println("Applying annual raises and resetting week...");
hourly.resetWeek();
hourly.annualRaise();
salary.resetWeek();
salary.annualRaise();
commission.resetWeek();
commission.annualRaise();
// Output Employees after changes
System.out.printf("%s %s %s ", hourly, salary, commission);
}
// Retrieve input and create HourlyEmployee
public static HourlyEmployee HourlyFactory() {
System.out.println("Creating HourlyEmployee...");
return new HourlyEmployee("Steve", "Rogers", 'A', 'M', 12345, true,
15.34);
}
// Retrieve input and create SalaryEmployee
public static SalaryEmployee SalaryFactory() {
System.out.println("Creating SalaryEmployee...");
return new SalaryEmployee("Kitty", "Pryde", 'X', 'F', 54321, true,
75000);
}
// Retrieve input and create CommissionEmployee
public static CommissionEmployee CommissionFactory() {
System.out.println("Creating CommissionEmployee...");
return new CommissionEmployee("Johnny", "Storm", 'F', 'L', 976499,
false, 2.5);
}
}
OUTPUT:
Creating HourlyEmployee...
12345
Steve Rogers
Gender:M
Status:Full Time
16. Wage: 15.34
Hours Worked: 0.0
Creating SalaryEmployee...
54321
Kitty Pryde
Gender:F
Status:Full Time
Salary:75000.0
Creating CommissionEmployee...
976499
Johnny Storm
Gender:L
Status:Part Time
Rate: 2.5
Sales: 0.0
Increasing Hourly's hours worked by 50.
Increasing Commissions's sales by 150,000.
Hourly Payout: 920.40
Salary Payout: 1442.31
Commission Payout: 3750.00
Finding total bonus payout...
Bonus Payout is 2863.60
Applying annual raises and resetting week...
12345
Steve Rogers
Gender:M
Status:Full Time
Wage: 0.77
Hours Worked: 0.0
54321
Kitty Pryde
Gender:F
Status:Full Time
Salary:4500.0
976499
Johnny Storm