-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeRecordManager.java
More file actions
88 lines (72 loc) · 3.37 KB
/
Copy pathEmployeeRecordManager.java
File metadata and controls
88 lines (72 loc) · 3.37 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
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.HashMap;
import java.util.Map;
// 1. Define the Employee Record
// Records automatically provide a constructor, getters (id(), name(), department()),
// equals(), hashCode(), and toString() without any boilerplate code!
record Employee(int id, String name, String department) {
}
// 2. Main Driver Class (PascalCase)
public class EmployeeRecordManager {
// Using a Map to store and manage the records. The ID is the Key, the Record is the Value.
private Map<Integer, Employee> employeeDatabase;
public EmployeeRecordManager() {
employeeDatabase = new HashMap<>();
}
// Method to add a new employee to the Map
public void addEmployee(Employee emp) {
employeeDatabase.put(emp.id(), emp);
System.out.println("Added: " + emp);
}
// Method to find an employee by their ID
public Employee findEmployeeById(int id) {
return employeeDatabase.get(id); // Returns the record, or null if not found
}
// Method to update the department of an employee
public void updateDepartment(int id, String newDepartment) {
Employee existingEmp = findEmployeeById(id);
if (existingEmp != null) {
// CRITICAL CONCEPT: Records are immutable!
// We cannot change the existing object. We must create a new record
// instance with the updated data and replace the old one in the Map.
Employee updatedEmp = new Employee(existingEmp.id(), existingEmp.name(), newDepartment);
employeeDatabase.put(id, updatedEmp); // Replaces the old record at this ID key
System.out.println("Successfully updated department for ID " + id + " to '" + newDepartment + "'.");
} else {
System.out.println("Error: Employee with ID " + id + " not found in the database.");
}
}
// Helper method to display all current employees
public void displayAll() {
System.out.println("\n--- Current Employee Database ---");
if (employeeDatabase.isEmpty()) {
System.out.println("No records found.");
} else {
for (Employee emp : employeeDatabase.values()) {
System.out.println(emp);
}
}
System.out.println("---------------------------------\n");
}
public static void main(String[] args) {
System.out.println("--- Employee Record Management System ---");
EmployeeRecordManager manager = new EmployeeRecordManager();
// 1. Adding initial records
manager.addEmployee(new Employee(101, "Alice Smith", "Engineering"));
manager.addEmployee(new Employee(102, "Bob Jones", "Marketing"));
manager.addEmployee(new Employee(103, "Charlie Brown", "Human Resources"));
manager.displayAll();
// 2. Finding an employee by ID
System.out.println("Searching for Employee ID 102...");
Employee found = manager.findEmployeeById(102);
if (found != null) {
System.out.println("Found: " + found);
} else {
System.out.println("Employee not found.");
}
// 3. Updating an employee's department
System.out.println("\nUpdating Department for Employee ID 101...");
manager.updateDepartment(101, "Research and Development");
// 4. Display all again to verify the update worked
manager.displayAll();
}
}