forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee Attendance System.java
106 lines (95 loc) · 3.77 KB
/
Employee Attendance System.java
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
import java.util.*;
import java.text.SimpleDateFormat;
class Employee {
private String name;
private int id;
private List<String> attendance;
public Employee(int id, String name) {
this.id = id;
this.name = name;
this.attendance = new ArrayList<>();
}
public void markAttendance() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
attendance.add(dateFormat.format(date));
}
public List<String> getAttendance() {
return attendance;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
}
public class AttendanceSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Employee> employees = new ArrayList<>();
int employeeIdCounter = 1;
while (true) {
System.out.println("Employee Attendance System Menu:");
System.out.println("1. Add Employee");
System.out.println("2. Mark Attendance");
System.out.println("3. View Attendance");
System.out.println("4. Exit");
System.out.print("Select an option (1/2/3/4): ");
int option = scanner.nextInt();
switch (option) {
case 1:
scanner.nextLine();
System.out.print("Enter the employee's name: ");
String name = scanner.nextLine();
employees.add(new Employee(employeeIdCounter, name));
employeeIdCounter++;
System.out.println("Employee added successfully.");
break;
case 2:
System.out.print("Enter employee ID to mark attendance: ");
int employeeId = scanner.nextInt();
Employee employee = findEmployeeById(employees, employeeId);
if (employee != null) {
employee.markAttendance();
System.out.println("Attendance marked for " + employee.getName());
} else {
System.out.println("Employee not found.");
}
break;
case 3:
System.out.print("Enter employee ID to view attendance: ");
int idToView = scanner.nextInt();
Employee empToView = findEmployeeById(employees, idToView);
if (empToView != null) {
List<String> attendance = empToView.getAttendance();
if (attendance.isEmpty()) {
System.out.println(empToView.getName() + " has no attendance records.");
} else {
System.out.println("Attendance records for " + empToView.getName() + ":");
for (String record : attendance) {
System.out.println(record);
}
}
} else {
System.out.println("Employee not found.");
}
break;
case 4:
System.out.println("Exiting the Attendance System.");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid option. Please select 1, 2, 3, or 4.");
}
}
}
private static Employee findEmployeeById(List<Employee> employees, int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee;
}
}
return null;
}
}