-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
110 lines (87 loc) · 2.78 KB
/
Main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.ArrayList;
import java.util.List;
abstract class Employee {
private String name;
private int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
// Abstract method to be implemented by subclasses
public abstract double calculateSalary();
@Override
public String toString() {
return "Employee [name=" + name + ", id=" + id + ", salary=" + calculateSalary() + "]";
}
}
class FullTimeEmployee extends Employee {
private double monthlySalary;
public FullTimeEmployee(String name, int id, double monthlySalary) {
super(name, id);
this.monthlySalary = monthlySalary;
}
@Override
public double calculateSalary() {
return monthlySalary;
}
}
class PartTimeEmployee extends Employee {
private int hoursWorked;
private double hourlyRate;
public PartTimeEmployee(String name, int id, int hoursWorked, double hourlyRate) {
super(name, id);
this.hoursWorked = hoursWorked;
this.hourlyRate = hourlyRate;
}
@Override
public double calculateSalary() {
return hoursWorked * hourlyRate;
}
}
class PayrollSystem {
private List<Employee> employeeList;
public PayrollSystem() {
employeeList = new ArrayList<>();
}
public void addEmployee(Employee employee) {
employeeList.add(employee);
}
public void removeEmployee(int id) {
Employee employeeToRemove = null;
for (Employee employee : employeeList) {
if (employee.getId() == id) {
employeeToRemove = employee;
break;
}
}
if (employeeToRemove != null) {
employeeList.remove(employeeToRemove);
}
}
public void displayEmployees() {
for (Employee employee : employeeList) {
System.out.println(employee);
}
}
}
public class Main {
public static void main(String[] args) {
PayrollSystem payrollSystem = new PayrollSystem();
FullTimeEmployee emp1 = new FullTimeEmployee("John Doe", 101, 5000.0);
PartTimeEmployee emp2 = new PartTimeEmployee("Jane Smith", 102, 30, 15.0);
payrollSystem.addEmployee(emp1);
payrollSystem.addEmployee(emp2);
System.out.println("Initial Employee Details:");
payrollSystem.displayEmployees();
System.out.println("\nRemoving Employee...");
payrollSystem.removeEmployee(101);
System.out.println("\nRemaining Employee Details:");
payrollSystem.displayEmployees();
}
}