|
| 1 | +import java.util.*; |
| 2 | +import java.text.SimpleDateFormat; |
| 3 | + |
| 4 | +class Employee { |
| 5 | + private String name; |
| 6 | + private int id; |
| 7 | + private List<String> attendance; |
| 8 | + |
| 9 | + public Employee(int id, String name) { |
| 10 | + this.id = id; |
| 11 | + this.name = name; |
| 12 | + this.attendance = new ArrayList<>(); |
| 13 | + } |
| 14 | + |
| 15 | + public void markAttendance() { |
| 16 | + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 17 | + Date date = new Date(); |
| 18 | + attendance.add(dateFormat.format(date)); |
| 19 | + } |
| 20 | + |
| 21 | + public List<String> getAttendance() { |
| 22 | + return attendance; |
| 23 | + } |
| 24 | + |
| 25 | + public String getName() { |
| 26 | + return name; |
| 27 | + } |
| 28 | + |
| 29 | + public int getId() { |
| 30 | + return id; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +public class AttendanceSystem { |
| 35 | + public static void main(String[] args) { |
| 36 | + Scanner scanner = new Scanner(System.in); |
| 37 | + List<Employee> employees = new ArrayList<>(); |
| 38 | + int employeeIdCounter = 1; |
| 39 | + |
| 40 | + while (true) { |
| 41 | + System.out.println("Employee Attendance System Menu:"); |
| 42 | + System.out.println("1. Add Employee"); |
| 43 | + System.out.println("2. Mark Attendance"); |
| 44 | + System.out.println("3. View Attendance"); |
| 45 | + System.out.println("4. Exit"); |
| 46 | + System.out.print("Select an option (1/2/3/4): "); |
| 47 | + |
| 48 | + int option = scanner.nextInt(); |
| 49 | + |
| 50 | + switch (option) { |
| 51 | + case 1: |
| 52 | + scanner.nextLine(); |
| 53 | + System.out.print("Enter the employee's name: "); |
| 54 | + String name = scanner.nextLine(); |
| 55 | + employees.add(new Employee(employeeIdCounter, name)); |
| 56 | + employeeIdCounter++; |
| 57 | + System.out.println("Employee added successfully."); |
| 58 | + break; |
| 59 | + case 2: |
| 60 | + System.out.print("Enter employee ID to mark attendance: "); |
| 61 | + int employeeId = scanner.nextInt(); |
| 62 | + Employee employee = findEmployeeById(employees, employeeId); |
| 63 | + if (employee != null) { |
| 64 | + employee.markAttendance(); |
| 65 | + System.out.println("Attendance marked for " + employee.getName()); |
| 66 | + } else { |
| 67 | + System.out.println("Employee not found."); |
| 68 | + } |
| 69 | + break; |
| 70 | + case 3: |
| 71 | + System.out.print("Enter employee ID to view attendance: "); |
| 72 | + int idToView = scanner.nextInt(); |
| 73 | + Employee empToView = findEmployeeById(employees, idToView); |
| 74 | + if (empToView != null) { |
| 75 | + List<String> attendance = empToView.getAttendance(); |
| 76 | + if (attendance.isEmpty()) { |
| 77 | + System.out.println(empToView.getName() + " has no attendance records."); |
| 78 | + } else { |
| 79 | + System.out.println("Attendance records for " + empToView.getName() + ":"); |
| 80 | + for (String record : attendance) { |
| 81 | + System.out.println(record); |
| 82 | + } |
| 83 | + } |
| 84 | + } else { |
| 85 | + System.out.println("Employee not found."); |
| 86 | + } |
| 87 | + break; |
| 88 | + case 4: |
| 89 | + System.out.println("Exiting the Attendance System."); |
| 90 | + scanner.close(); |
| 91 | + System.exit(0); |
| 92 | + default: |
| 93 | + System.out.println("Invalid option. Please select 1, 2, 3, or 4."); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static Employee findEmployeeById(List<Employee> employees, int id) { |
| 99 | + for (Employee employee : employees) { |
| 100 | + if (employee.getId() == id) { |
| 101 | + return employee; |
| 102 | + } |
| 103 | + } |
| 104 | + return null; |
| 105 | + } |
| 106 | +} |
0 commit comments