-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHRProgram.java
More file actions
179 lines (164 loc) · 7.06 KB
/
HRProgram.java
File metadata and controls
179 lines (164 loc) · 7.06 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package workersprogram;
import workers.*;
import staff.*;
import workers.EmployeeType;
import java.util.List;
import java.util.Scanner;
import java.io.*;
public class HRProgram {
private static HRDepartment hrDepartment = new HRDepartment();
public static void main(String[] args) throws EmployeeNotFoundException {
String filePath = "workers.dat";
try {
hrDepartment.loadWorkerData(filePath);
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
hrDepartment.saveWorkerData("workers.dat");
boolean exitProgram = false;
Scanner scanner = new Scanner(System.in);
MenuOption option = null;
do {
displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
option = MenuOption.values()[choice - 1];
switch (option) {
case ADD_EMPLOYEE:
addEmployee();
break;
case VIEW_EMPLOYEE:
viewEmployee();
break;
case DELETE_EMPLOYEE:
deleteEmployee();
break;
case VIEW_REPORT:
viewReport();
break;
case EXIT:
System.out.println("Exiting the program...");
exitProgram = true;
break;
default:
System.out.println("Invalid option. Please try again.");
break;
}
} while (!exitProgram);
scanner.close();
}
private static void displayMenu() {
System.out.println("Menu:");
int counter = 1;
for (MenuOption option : MenuOption.values()) {
System.out.println(counter + ". " + option.getDisplayName());
counter++;
}
System.out.print("Enter your choice: ");
}
private static void addEmployee() {
Scanner scanner = new Scanner(System.in);
EmployeeType employeeType = getEmployeeType();
System.out.print("Enter employee ID: ");
String id = scanner.nextLine();
try {
hrDepartment.findEmployeeById(id);
System.out.println("Employee with the same ID already exists.");
return; // Exit the method if employee already exists
} catch (EmployeeNotFoundException e) { // Continue with adding the employee
}
System.out.print("Enter first name: ");
String firstName = scanner.nextLine();
System.out.print("Enter last name: ");
String lastName = scanner.nextLine();
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Enter email address: ");
String emailAddress = scanner.nextLine();
System.out.print("Enter gender (Male/Female): ");
Gender gender = Gender.valueOf(scanner.nextLine().toUpperCase());
System.out.print("Enter basic monthly salary: ");
double basicMonthlySalary = scanner.nextDouble();
scanner.nextLine(); // Consume newline character
switch (employeeType) {
case ADMINISTRATIVE:
System.out.print("Enter role: ");
String role = scanner.nextLine();
AdministrativeWorker administrativeWorker = new AdministrativeWorker(id, firstName, lastName, phoneNumber, emailAddress, gender, basicMonthlySalary, role);
try {
hrDepartment.addEmployee(administrativeWorker);
System.out.println("Administrative worker added successfully.");
} catch (EmployeeAlreadyExistsException e) {
System.out.println(e.getMessage());
}
break;
case SOFTWARE_DEVELOPER:
System.out.print("Enter monthly bonus: ");
double monthlyBonus = scanner.nextDouble();
scanner.nextLine(); // Consume newline character
SoftwareDeveloper softwareDeveloper = new SoftwareDeveloper(id, firstName, lastName, phoneNumber,
emailAddress, gender, basicMonthlySalary, monthlyBonus);
try {
hrDepartment.addEmployee(softwareDeveloper);
System.out.println("Software developer added successfully.");
} catch (EmployeeAlreadyExistsException e) {
System.out.println(e.getMessage());
}
break;
case ALGORITHMIC_SOFTWARE_DEVELOPER:
System.out.print("Enter monthly bonus: ");
monthlyBonus = scanner.nextDouble();
System.out.print("Enter additional bonus: ");
double additionalBonus = scanner.nextDouble();
scanner.nextLine(); // Consume newline character
AlgorithmicSoftwareDeveloper algorithmicSoftwareDeveloper = new AlgorithmicSoftwareDeveloper(id, firstName,
lastName, phoneNumber, emailAddress, gender, basicMonthlySalary, monthlyBonus, additionalBonus);
try {
hrDepartment.addEmployee(algorithmicSoftwareDeveloper);
System.out.println("Algorithmic software developer added successfully.");
} catch (EmployeeAlreadyExistsException e) {
System.out.println(e.getMessage());
}
break;
default:
System.out.println("Invalid employee type.");
break;
}
}
private static void viewEmployee() throws EmployeeNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee ID: ");
String id = scanner.nextLine();
Employee employee = hrDepartment.findEmployeeById(id);
System.out.println(employee.toString());
}
private static void deleteEmployee() throws EmployeeNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee ID: ");
String id = scanner.nextLine();
hrDepartment.deleteEmployee(id);
}
private static void viewReport() {
List<Employee> sortedEmployees = hrDepartment.sortEmployees();
if (sortedEmployees.isEmpty()) {
System.out.println("No employees found.");
} else {
System.out.println("Employee Report:");
for (Employee employee : sortedEmployees) {
System.out.println(employee.toString());
}
}
}
private static EmployeeType getEmployeeType() {
Scanner scanner = new Scanner(System.in);
System.out.println("Select employee type:");
int counter = 1;
for (EmployeeType type : EmployeeType.values()) {
System.out.println(counter + ". " + type.name());
counter++;
}
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
return EmployeeType.values()[choice - 1];
}
}