Repository navigation
Expand file tree
/
Copy pathDeleteEmployeeConsole.java
More file actions
64 lines (57 loc) · 2.66 KB
/
Copy pathDeleteEmployeeConsole.java
File metadata and controls
64 lines (57 loc) · 2.66 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
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;
import java.util.Scanner;
/** PT4 console-menu interaction. Pass a role from a real authenticated session. */
public final class DeleteEmployeeConsole {
private DeleteEmployeeConsole() { }
public static void run(Scanner scanner, Connection connection, UserRole authenticatedRole) {
if (authenticatedRole != UserRole.HR_ADMIN) {
System.out.println("Access denied. HR Administrator privileges are required.");
return;
}
System.out.print("Enter Employee ID to delete: ");
String rawId = scanner.nextLine().trim();
final int empID;
try {
empID = Integer.parseInt(rawId);
if (empID <= 0) throw new NumberFormatException();
} catch (NumberFormatException ex) {
System.out.println("Invalid employee ID. Enter a positive whole number.");
return;
}
EmployeeDeletionService service = new EmployeeDeletionService(connection);
try {
Optional<EmployeeDeletionService.EmployeeSummary> result =
service.findById(empID, authenticatedRole);
if (!result.isPresent()) {
System.out.println("No matching employee found.");
return;
}
EmployeeDeletionService.EmployeeSummary employee = result.get();
System.out.println("Employee found:");
System.out.println("ID: " + employee.empID);
System.out.println("Name: " + employee.firstName + " " + employee.lastName);
System.out.println("WARNING: Deletion also removes this employee's payroll history, "
+ "address, demographics, division, and job assignments.");
System.out.print("Are you sure you want to delete this employee? (Y/N): ");
String confirm = scanner.nextLine().trim();
if (confirm.equalsIgnoreCase("N")) {
System.out.println("Deletion canceled.");
return;
}
if (!confirm.equalsIgnoreCase("Y")) {
System.out.println("Invaild input. Please enter Y or N.");
return;
}
if (service.deleteEmployee(empID, authenticatedRole)) {
System.out.println("Employee successfully deleted.");
} else {
System.out.println("Employee no longer exists; nothing was deleted.");
}
} catch (SQLException ex) {
System.out.println("Deletion failed. Database changes were rolled back.");
System.err.println("Database error: " + ex.getMessage());
}
}
}