-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHRDepartment.java
More file actions
76 lines (67 loc) · 2.72 KB
/
HRDepartment.java
File metadata and controls
76 lines (67 loc) · 2.72 KB
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
package staff;
import workers.*;
import java.io.*;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
//import java.io.ObjectInputStream;
//import java.io.ObjectOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HRDepartment extends Exception {
public List<Employee> employees;
public HRDepartment() {
employees = new ArrayList<>();
}
public void addEmployee(Employee employee) throws EmployeeAlreadyExistsException {
if (employees.contains(employee)) {
throw new EmployeeAlreadyExistsException("Employee already exists in the HR department.");
}
employees.add(employee);
}
public Employee findEmployeeById(String id) throws EmployeeNotFoundException {
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
return employee;
}
}
throw new EmployeeNotFoundException("Employee not found in the HR department.");
}
public void deleteEmployee(String id) throws EmployeeNotFoundException {
Employee employeeToRemove = findEmployeeById(id);
if (employeeToRemove != null) {
employees.remove(employeeToRemove);
System.out.println("Employee deleted successfully.");
} else {
throw new EmployeeNotFoundException("Employee not found with ID: " + id);
}
}
public List<Employee> sortEmployees() {
List<Employee> sortedEmployees = new ArrayList<>(employees);
Collections.sort(sortedEmployees);
return sortedEmployees;
}
public void loadWorkerData(String filePath) throws IOException, ClassNotFoundException {
try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filePath))) {
employees = (List<Employee>) inputStream.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading worker data from file: " + e.getMessage());
}
}
public void saveWorkerData(String filePath) {
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(filePath))) {
outputStream.writeObject(employees);
} catch (IOException e) {
System.out.println("Error saving worker data to file: " + e.getMessage());
}
}
@Override
public String toString() {
sortEmployees();
String employeesString = ""; //empty string
for (Employee employee : employees) {
employeesString += employee.toString() + "\n"; } // adds employee details to the string directly
return employeesString;
}
}