-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayrollTester.java
76 lines (62 loc) · 2.19 KB
/
PayrollTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
abstract class Employee {
private int employeeNumber;
public Employee(int employeeNumber) {
this.employeeNumber = employeeNumber;
}
public int getEmployeeNumber() {
return employeeNumber;
}
public abstract double calculatePay();
}
class SalariedEmployee extends Employee {
private double salary;
public SalariedEmployee(int employeeNumber, double salary) {
super(employeeNumber);
this.salary = salary;
}
@Override
public double calculatePay() {
// Assuming salary is paid bi-monthly
return salary / 2;
}
}
class HourlyEmployee extends Employee {
private double hourlyRate;
private int hoursWorked;
public HourlyEmployee(int employeeNumber, double hourlyRate, int hoursWorked) {
super(employeeNumber);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
@Override
public double calculatePay() {
if (hoursWorked <= 40) {
return hourlyRate * hoursWorked;
} else {
return (hourlyRate * 40) + (hourlyRate * 1.5 * (hoursWorked - 40));
}
}
}
class CommissionEmployee extends Employee {
private double grossSales;
public CommissionEmployee(int employeeNumber, double grossSales) {
super(employeeNumber);
this.grossSales = grossSales;
}
@Override
public double calculatePay() {
return 0.2 * grossSales; // 20% commission
}
}
public class PayrollTester {
public static void main(String[] args) {
HourlyEmployee hourlyEmployee1 = new HourlyEmployee(1, 16.50, 35);
HourlyEmployee hourlyEmployee2 = new HourlyEmployee(2, 16.50, 45);
SalariedEmployee salariedEmployee = new SalariedEmployee(3, 110000);
CommissionEmployee commissionEmployee = new CommissionEmployee(4, 75000);
System.out.println("Hourly employee 1 pay: $" + hourlyEmployee1.calculatePay());
System.out.println("Hourly employee 2 pay: $" + hourlyEmployee2.calculatePay());
System.out.println("Salaried employee pay: $" + salariedEmployee.calculatePay());
System.out.println("Commission employee pay: $" + commissionEmployee.calculatePay());
}
}