-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
266 lines (232 loc) · 10.4 KB
/
Copy pathMain.java
File metadata and controls
266 lines (232 loc) · 10.4 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;
public class Main {
private static EmployeeManager manager = new EmployeeManager();
private static Scanner scanner = new Scanner(System.in);
// ----------------------------------------------------------------
// 1. Employee Model
// ----------------------------------------------------------------
private static class Employee {
private int id;
private String name;
private String department;
private double salary;
public Employee(int id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
public int getId() { return id; }
public String getName() { return name; }
public String getDepartment() { return department; }
public double getSalary() { return salary; }
public void setName(String name) { this.name = name; }
public void setDepartment(String department) { this.department = department; }
public void setSalary(double salary) { this.salary = salary; }
@Override
public String toString() {
return String.format("| ID: %-5d | Name: %-20s | Dept: %-15s | Salary: ₹%,.2f |",
id, name, department, salary);
}
}
// ----------------------------------------------------------------
// 2. Employee Manager (CRUD)
// ----------------------------------------------------------------
private static class EmployeeManager {
private List<Employee> employees;
public EmployeeManager() {
this.employees = new ArrayList<>();
// ✅ Removed the default employees (no pre-added data)
}
public void addEmployee(Employee employee) {
employees.add(employee);
System.out.println("-> Success: Employee " + employee.getName() + " added.");
}
public Optional<Employee> findEmployeeById(int id) {
return employees.stream()
.filter(e -> e.getId() == id)
.findFirst();
}
public void viewAllEmployees() {
if (employees.isEmpty()) {
System.out.println("--- The employee list is currently empty. ---");
return;
}
System.out.println("\n--- All Employees (Total: " + employees.size() + ") ---");
System.out.println("----------------------------------------------------------------------------------");
for (Employee e : employees) {
System.out.println(e);
}
System.out.println("----------------------------------------------------------------------------------");
}
public boolean updateEmployee(int id, String newName, String newDept, double newSalary) {
Optional<Employee> employeeOpt = findEmployeeById(id);
if (employeeOpt.isPresent()) {
Employee employee = employeeOpt.get();
employee.setName(newName);
employee.setDepartment(newDept);
employee.setSalary(newSalary);
System.out.println("-> Success: Employee ID " + id + " updated successfully.");
return true;
} else {
System.out.println("-> Error: Employee ID " + id + " not found for update.");
return false;
}
}
public boolean deleteEmployee(int id) {
Optional<Employee> employeeOpt = findEmployeeById(id);
if (employeeOpt.isPresent()) {
employees.remove(employeeOpt.get());
System.out.println("-> Success: Employee ID " + id + " deleted.");
return true;
} else {
System.out.println("-> Error: Employee ID " + id + " not found for deletion.");
return false;
}
}
}
// ----------------------------------------------------------------
// 3. Main Application Runner
// ----------------------------------------------------------------
public static void main(String[] args) {
System.out.println("==================================================");
System.out.println(" Welcome to the Employee Management System (EMS)");
System.out.println("==================================================");
boolean running = true;
while (running) {
displayMenu();
try {
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addEmployee();
break;
case 2:
manager.viewAllEmployees();
break;
case 3:
viewEmployeeById();
break;
case 4:
updateEmployee();
break;
case 5:
deleteEmployee();
break;
case 6:
running = false;
System.out.println("Thank you for using the EMS. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please enter a number between 1 and 6.");
}
} catch (InputMismatchException e) {
System.out.println("\n!!! Invalid input. Please enter a number for your choice. !!!\n");
scanner.nextLine();
} catch (Exception e) {
System.out.println("\n!!! An unexpected error occurred: " + e.getMessage() + " !!!\n");
}
}
scanner.close();
}
private static void displayMenu() {
System.out.println("\n--------------------------------------------------");
System.out.println("Please select an option:");
System.out.println("1. Add New Employee");
System.out.println("2. View All Employees");
System.out.println("3. View Employee by ID");
System.out.println("4. Update Employee Details");
System.out.println("5. Delete Employee by ID");
System.out.println("6. Exit Application");
System.out.print("Enter choice (1-6): ");
}
private static void addEmployee() {
try {
System.out.print("Enter Employee ID: ");
int id = scanner.nextInt();
scanner.nextLine();
if (manager.findEmployeeById(id).isPresent()) {
System.out.println("-> Error: Employee ID " + id + " already exists. Please use a unique ID.");
return;
}
System.out.print("Enter Employee Name: ");
String name = scanner.nextLine();
System.out.print("Enter Employee Department: ");
String department = scanner.nextLine();
System.out.print("Enter Employee Salary (in ₹): ");
double salary = scanner.nextDouble();
scanner.nextLine();
Employee newEmployee = new Employee(id, name, department, salary);
manager.addEmployee(newEmployee);
} catch (InputMismatchException e) {
System.out.println("-> Input Error: ID and Salary must be numbers.");
scanner.nextLine();
}
}
private static void viewEmployeeById() {
try {
System.out.print("Enter Employee ID to view: ");
int id = scanner.nextInt();
scanner.nextLine();
Optional<Employee> employeeOpt = manager.findEmployeeById(id);
if (employeeOpt.isPresent()) {
System.out.println("\n--- Employee Found ---");
System.out.println(employeeOpt.get());
System.out.println("----------------------");
} else {
System.out.println("-> Error: Employee ID " + id + " not found.");
}
} catch (InputMismatchException e) {
System.out.println("-> Input Error: ID must be a number.");
scanner.nextLine();
}
}
private static void updateEmployee() {
try {
System.out.print("Enter Employee ID to update: ");
int id = scanner.nextInt();
scanner.nextLine();
Optional<Employee> employeeOpt = manager.findEmployeeById(id);
if (employeeOpt.isPresent()) {
Employee employee = employeeOpt.get();
System.out.println("\n--- Current Details ---");
System.out.println(employee);
System.out.println("-----------------------");
System.out.print("Enter new Name (leave blank to keep '" + employee.getName() + "'): ");
String newName = scanner.nextLine();
if (newName.isEmpty()) newName = employee.getName();
System.out.print("Enter new Department (leave blank to keep '" + employee.getDepartment() + "'): ");
String newDept = scanner.nextLine();
if (newDept.isEmpty()) newDept = employee.getDepartment();
System.out.print("Enter new Salary (0 to skip): ");
String salaryInput = scanner.nextLine();
double newSalary = employee.getSalary();
if (!salaryInput.trim().isEmpty() && Double.parseDouble(salaryInput) != 0) {
newSalary = Double.parseDouble(salaryInput);
}
manager.updateEmployee(id, newName, newDept, newSalary);
} else {
System.out.println("-> Error: Employee ID " + id + " not found.");
}
} catch (InputMismatchException | NumberFormatException e) {
System.out.println("-> Input Error: Invalid number format for ID or Salary.");
scanner.nextLine();
}
}
private static void deleteEmployee() {
try {
System.out.print("Enter Employee ID to delete: ");
int id = scanner.nextInt();
scanner.nextLine();
manager.deleteEmployee(id);
} catch (InputMismatchException e) {
System.out.println("-> Input Error: ID must be a number.");
scanner.nextLine();
}
}
}