-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeManagementSystem.java
More file actions
131 lines (115 loc) · 4.17 KB
/
EmployeeManagementSystem.java
File metadata and controls
131 lines (115 loc) · 4.17 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
import java.util.ArrayList;
import java.util.Scanner;
class Employee {
private int id;
private String name;
private double salary;
private String department;
public Employee(int id, String name, double salary, String department) {
this.id = id;
this.name = name;
this.salary = salary;
this.department = department;
}
public int getId() { return id; }
public String getName() { return name; }
public double getSalary() { return salary; }
public String getDepartment() { return department; }
public void setName(String name) { this.name = name; }
public void setSalary(double salary) { this.salary = salary; }
public void setDepartment(String department) { this.department = department; }
@Override
public String toString() {
return "Employee [ID=" + id + ", Name=" + name + ", Salary=" + salary + ", Department=" + department + "]";
}
}
// Employee Management System
public class EmployeeManagementSystem {
private static ArrayList<Employee> employees = new ArrayList<>();
private static Scanner sc = new Scanner(System.in);
// Create
public static void addEmployee() {
System.out.print("Enter ID: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Enter Salary: ");
double salary = sc.nextDouble();
sc.nextLine();
System.out.print("Enter Department: ");
String dept = sc.nextLine();
employees.add(new Employee(id, name, salary, dept));
System.out.println(" Employee added successfully!\n");
}
// Read
public static void viewEmployees() {
if (employees.isEmpty()) {
System.out.println(" No employees found.\n");
return;
}
System.out.println(" Employee Records:");
for (Employee emp : employees) {
System.out.println(emp);
}
System.out.println();
}
// Update
public static void updateEmployee() {
System.out.print("Enter Employee ID to update: ");
int id = sc.nextInt();
sc.nextLine();
for (Employee emp : employees) {
if (emp.getId() == id) {
System.out.print("Enter new Name: ");
emp.setName(sc.nextLine());
System.out.print("Enter new Salary: ");
emp.setSalary(sc.nextDouble());
sc.nextLine();
System.out.print("Enter new Department: ");
emp.setDepartment(sc.nextLine());
System.out.println(" Employee updated successfully!\n");
return;
}
}
System.out.println(" Employee not found!\n");
}
// Delete
public static void deleteEmployee() {
System.out.print("Enter Employee ID to delete: ");
int id = sc.nextInt();
sc.nextLine();
for (Employee emp : employees) {
if (emp.getId() == id) {
employees.remove(emp);
System.out.println(" Employee deleted successfully!\n");
return;
}
}
System.out.println(" Employee not found!\n");
}
// Main Menu
public static void main(String[] args) {
while (true) {
System.out.println("=== Employee Management System ===");
System.out.println("1. Add Employee");
System.out.println("2. View Employees");
System.out.println("3. Update Employee");
System.out.println("4. Delete Employee");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = sc.nextInt();
switch (choice) {
case 1 -> addEmployee();
case 2 -> viewEmployees();
case 3 -> updateEmployee();
case 4 -> deleteEmployee();
case 5 -> {
System.out.println(" Exiting... Goodbye!");
System.exit(0);
}
default -> System.out.println(" Invalid choice. Try again.\n");
}
}
}
}