Java简易考勤系统

### 1. 定义必要的类和枚举

```java
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// 考勤状态枚举类
enum AttendanceStatus {
    PRESENT,    // 出勤
    ABSENT,     // 请假
    LATE,       // 迟到
    OVERTIME,   // 加班
    EARLY_EXIT  // 早退
}

// 角色枚举类
enum Role {
    ADMIN,
    MANAGER,
    EMPLOYEE
}

// 员工类
class Employee {
    private int id;
    private String name;
    private String contact;
    private String department;
    private String position;

    public Employee(int id, String name, String contact, String department, String position) {
        this.id = id;
        this.name = name;
        this.contact = contact;
        this.department = department;
        this.position = position;
    }

    // Getter and Setter methods...
}

// 考勤记录类
class AttendanceRecord {
    private int id;
    private int employeeId;
    private Date date;
    private AttendanceStatus status;
    private Date startTime; // 添加开始工作时间
    private Date endTime;   // 添加结束工作时间

    public AttendanceRecord(int id, int employeeId, Date date, AttendanceStatus status, Date startTime, Date endTime) {
        this.id = id;
        this.employeeId = employeeId;
        this.date = date;
        this.status = status;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    // Getter and Setter methods...
}

// 数据库连接类
class DBConnector {
    private static final String URL = "jdbc:mysql://localhost:3306/your_database_name";
    private static final String USERNAME = "your_username";
    private static final String PASSWORD = "your_password";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }
}
```

### 2. 完整的考勤系统类

```java
// 考勤系统管理类
class AttendanceSystem {
    private Connection connection;

    public AttendanceSystem() {
        try {
            this.connection = DBConnector.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 添加员工信息到数据库
    public void addEmployee(Employee employee, Role role) {
        String sql = "INSERT INTO employees (id, name, contact, department, position) VALUES (?, ?, ?, ?, ?)";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, employee.getId());
            stmt.setString(2, employee.getName());
            stmt.setString(3, employee.getContact());
            stmt.setString(4, employee.getDepartment());
            stmt.setString(5, employee.getPosition());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 记录考勤到数据库
    public void recordAttendance(int employeeId, Date date, AttendanceStatus status, Date startTime, Date endTime) {
        String sql = "INSERT INTO attendance_records (employee_id, date, status, start_time, end_time) VALUES (?, ?, ?, ?, ?)";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, employeeId);
            stmt.setDate(2, new java.sql.Date(date.getTime()));
            stmt.setString(3, status.toString());
            stmt.setTimestamp(4, new Timestamp(startTime.getTime()));
            stmt.setTimestamp(5, new Timestamp(endTime.getTime()));
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 获取员工考勤记录
    public List<AttendanceRecord> getAttendanceRecords(int employeeId) {
        List<AttendanceRecord> records = new ArrayList<>();
        String sql = "SELECT * FROM attendance_records WHERE employee_id = ?";
        try (PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, employeeId);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                Date date = rs.getDate("date");
                AttendanceStatus status = AttendanceStatus.valueOf(rs.getString("status"));
                Timestamp startTime = rs.getTimestamp("start_time");
                Timestamp endTime = rs.getTimestamp("end_time");
                records.add(new AttendanceRecord(id, employeeId, date, status, startTime, endTime));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return records;
    }

    // 其他方法(编辑员工信息、编辑考勤记录、权限控制等)需要相应地修改以支持数据库操作
}
```

### 3. 测试类

```java
public class Main {
    public static void main(String[] args) {
        AttendanceSystem system = new AttendanceSystem();

        // 添加员工和角色信息到数据库
        Employee emp1 = new Employee(1, "Employee1", "emp1@example.com", "Sales", "Sales Executive");
        system.addEmployee(emp1, Role.EMPLOYEE);

        // 记录考勤到数据库
        Date today = new Date();
        Date startTime = new Date(today.getTime() + 8 * 60 * 60 * 1000); // 八小时后开始工作
        Date endTime = new Date(today.getTime() + 10 * 60 * 60 * 1000); // 十小时后结束工作

        system.recordAttendance(1, today, AttendanceStatus.PRESENT, startTime, endTime);

        // 从数据库中获取员工考勤记录并打印
        List<AttendanceRecord> records = system.getAttendanceRecords(1);
        for (AttendanceRecord record : records) {
            System.out.println("Attendance Record - Date: " + record.getDate() +
                    ", Status: " + record.getStatus() +
                    ", Start Time: " + record.getStartTime() +
                    ", End Time: " + record.getEndTime());
        }
    }
}
```

一个简易考勤系统,包括员工信息管理、考勤记录的记录和查询,所有数据存储在MySQL数据库中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值