公司员工考勤系统Java代码

package com.company;

import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
       Company company = new Company();
        //添加员工
        company.addEmp(11,"zhangsan",20,'男');
        company.addEmp(22,"lisi",22,'男');
        company.addEmp(33,"lili",24,'女');
        company.addEmp(44,"fsw",28,'男');
        //显示员工信息
        company.showEmp();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入删除员工信息!工号");
        int empNum = sc.nextInt();
        String s = sc.nextLine();
        //删除员工的方法
        company.deleteEmp(empNum);
        //显示删除后的员工信息
        company.showEmp();
        System.out.println("请输入查询员工信息的工号:");
        int id = sc.nextInt();
        String a = sc.nextLine();
        company.selectEmp(id);
        int input = 0;
        do{
            System.out.println("员工考勤系统");
            System.out.println("0-------------exit");
            System.out.println("1-------------签到");
            System.out.println("2-------------签退");
            System.out.println("3-------------显示考勤信息");
            input = sc.nextInt();
            String c = sc.nextLine();
            switch (input){
                case 1:
                    company.qiandao();
                    break;
                case 2:
                    company.qiantui();
                    break;
                case 3:
                    company.showAttendence();
                    break;
                default:
                    break;
            }
        }while(input!=0);
    }

}

package com.company;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 定义一个公司类,包括员工的集合,还有每天考勤的集合
 * Created by ttc on 2017/6/30.
 */
public class Company {
    private  Map<Integer,Emp> mapEmp = new HashMap<>();
    //Map中的KEY为签到的日期
    private Map<String,List<KaoqinRecord>> map = new HashMap<>();
    //定义一个添加员工的方法
    public Map<Integer,Emp> addEmp(int id,String name,int age,char sex){
        mapEmp.put(id,new Emp(id,name,age,sex));
        return mapEmp;
    }
    //显示现在公司员工的信息
    public void showEmp(){
        for(Map.Entry<Integer,Emp> m :mapEmp.entrySet()){
            System.out.println("工号:"+m.getValue().getId()+" 姓名:"+m.getValue().getName()+" 年龄"+m.getValue().getAge()+" 性别"+m.getValue().getSex());
        }
    }
    //定义删除员工的方法
    public void deleteEmp(int id){
        mapEmp.remove(id);
    }
    //定义查询员工的方法
    public void selectEmp(int id){
        System.out.println("工号:"+mapEmp.get(id).getId()+" 姓名:"+mapEmp.get(id).getName()+" 年龄"+mapEmp.get(id).getAge()+" 性别"+mapEmp.get(id).getSex());
    }
    //定义公司的签到方法
    public void qiandao(){
        Date date = new Date();
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
        String qiandaoDate = sdfDate.format(date);
        String kaoqinTime = sdfTime.format(date);
        System.out.println("请输入要签到的员工号码");
        Scanner sc = new Scanner(System.in);
        int inputId = sc.nextInt();
        if(!mapEmp.containsKey(inputId)){
            System.out.println("不存在该员工");
            return;
        }
        List<KaoqinRecord> listKaoQin = null;
        if(!map.containsKey(qiandaoDate)){
            //第一个签到的员工
            listKaoQin = new ArrayList<>();
            KaoqinRecord kq = new KaoqinRecord();
            kq.setName(mapEmp.get(inputId).getName());
            kq.setStratTime(kaoqinTime);
            kq.setEndTime("");
            listKaoQin.add(kq);
            map.put(qiandaoDate,listKaoQin);
            System.out.println("签到成功!");
        }else {//说明之前,今天已经有人签到过了
            listKaoQin = map.get(qiandaoDate);
            //判断是否已经签到过
            boolean bIsFind = false;
            for (KaoqinRecord kr : listKaoQin) {
                if (kr.getName() == mapEmp.get(inputId).getName()) {
                    System.out.println("已经签到过了");
                    bIsFind = true;
                    break;
                }
            }
            if (!bIsFind) {
                KaoqinRecord kr = new KaoqinRecord();
                kr.setName(mapEmp.get(inputId).getName());
                kr.setStratTime(kaoqinTime);
                kr.setEndTime("");
                listKaoQin.add(kr);
                System.out.println("签到成功!");
            }
        }
    }
    public void qiantui(){
        System.out.println("请输入要签退的员工ID");
        Scanner sc = new Scanner(System.in);
        int inputId = sc.nextInt();
        String s = sc.nextLine();
        if(!mapEmp.containsKey(inputId)){
            System.out.println("员工不存在");
            return;
        }
        Date date = new Date();
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");
        String qiandaoDate = sdfDate.format(date);
        String kaoqinTime = sdfTime.format(date);
        if(!map.containsKey(qiandaoDate)){
            System.out.println("您没签到,不能签退!");
        }else{
            List<KaoqinRecord> listKaoQin = map.get(qiandaoDate);
            boolean bIsFind = false;
            for(KaoqinRecord kr : listKaoQin){
                if(kr.getName().equals(mapEmp.get(inputId).getName())){
                    kr.setEndTime(kaoqinTime);
                    bIsFind = true;
                    break;
                }
            }
            if(!bIsFind){
                System.out.println("您没签到,不能签退");
            }else{
                System.out.println("签退成功!");
            }
        }
    }
    public void showAttendence(){
        System.out.println("请输入要查询的日期(yyyy-MM-dd)");
        Scanner sc = new Scanner(System.in);
        String qiandaoDate = sc.nextLine();
        List<KaoqinRecord> listKaoQin = map.get(qiandaoDate);
        for(KaoqinRecord kr : listKaoQin){
            System.out.println(kr.getName()+" 签到时间为:"+kr.getStratTime()+" 签退时间为:"+kr.getEndTime());
        }
    }
}
package com.company;

/**
 * 定义一个员工每天考勤的类,包括员工姓名,还有签到时间和签退时间
 * Created by ttc on 2017/6/30.
 */
public class KaoqinRecord {
    private String name;
    private String stratTime;
    private String endTime;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStratTime() {
        return stratTime;
    }

    public void setStratTime(String stratTime) {
        this.stratTime = stratTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
}
package com.company;

/**
 * 定义一个员工类,属性包括员工的id,员工姓名,年龄,性别
 * Created by ttc on 2017/6/30.
 */
public class Emp {
    private int id;
    private String name;
    private int age;
    private char sex;
    public  Emp(){

    }

    public Emp(int id, String name, int age, char sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }
}

公司员工考勤管理系统的Java实现包括以下步骤: 1. 定义员工类和考勤类,员工类包含员工的基本信息,考勤类包含员工的考勤记录。 ```java public class Employee { private int id; private String name; private String department; private String position; // 省略getter和setter方法 } public class Attendance { private int id; private Date date; private int employeeId; private String status; // 省略getter和setter方法 } ``` 2. 定义数据库连接类,实现与数据库的连接和操作。 ```java public class DBConnection { private static final String URL = "jdbc:mysql://localhost:3306/attendance"; private static final String USER = "root"; private static final String PASSWORD = "root"; public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(URL, USER, PASSWORD); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void close(Connection conn, Statement stmt, ResultSet rs) { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } ``` 3. 定义员工管理类和考勤管理类,实现员工和考勤的增删改查等操作。 ```java public class EmployeeManager { public void addEmployee(Employee employee) { Connection conn = null; PreparedStatement stmt = null; try { conn = DBConnection.getConnection(); String sql = "INSERT INTO employee (id, name, department, position) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); stmt.setInt(1, employee.getId()); stmt.setString(2, employee.getName()); stmt.setString(3, employee.getDepartment()); stmt.setString(4, employee.getPosition()); stmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, null); } } public void deleteEmployee(int id) { Connection conn = null; PreparedStatement stmt = null; try { conn = DBConnection.getConnection(); String sql = "DELETE FROM employee WHERE id=?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); stmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, null); } } public void updateEmployee(Employee employee) { Connection conn = null; PreparedStatement stmt = null; try { conn = DBConnection.getConnection(); String sql = "UPDATE employee SET name=?, department=?, position=? WHERE id=?"; stmt = conn.prepareStatement(sql); stmt.setString(1, employee.getName()); stmt.setString(2, employee.getDepartment()); stmt.setString(3, employee.getPosition()); stmt.setInt(4, employee.getId()); stmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, null); } } public List<Employee> getEmployees() { List<Employee> employees = new ArrayList<>(); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = DBConnection.getConnection(); String sql = "SELECT * FROM employee"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { Employee employee = new Employee(); employee.setId(rs.getInt("id")); employee.setName(rs.getString("name")); employee.setDepartment(rs.getString("department")); employee.setPosition(rs.getString("position")); employees.add(employee); } } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, rs); } return employees; } } public class AttendanceManager { public void addAttendance(Attendance attendance) { Connection conn = null; PreparedStatement stmt = null; try { conn = DBConnection.getConnection(); String sql = "INSERT INTO attendance (id, date, employee_id, status) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); stmt.setInt(1, attendance.getId()); stmt.setDate(2, new java.sql.Date(attendance.getDate().getTime())); stmt.setInt(3, attendance.getEmployeeId()); stmt.setString(4, attendance.getStatus()); stmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, null); } } public void deleteAttendance(int id) { Connection conn = null; PreparedStatement stmt = null; try { conn = DBConnection.getConnection(); String sql = "DELETE FROM attendance WHERE id=?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); stmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, null); } } public void updateAttendance(Attendance attendance) { Connection conn = null; PreparedStatement stmt = null; try { conn = DBConnection.getConnection(); String sql = "UPDATE attendance SET date=?, employee_id=?, status=? WHERE id=?"; stmt = conn.prepareStatement(sql); stmt.setDate(1, new java.sql.Date(attendance.getDate().getTime())); stmt.setInt(2, attendance.getEmployeeId()); stmt.setString(3, attendance.getStatus()); stmt.setInt(4, attendance.getId()); stmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, null); } } public List<Attendance> getAttendances() { List<Attendance> attendances = new ArrayList<>(); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = DBConnection.getConnection(); String sql = "SELECT * FROM attendance"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { Attendance attendance = new Attendance(); attendance.setId(rs.getInt("id")); attendance.setDate(rs.getDate("date")); attendance.setEmployeeId(rs.getInt("employee_id")); attendance.setStatus(rs.getString("status")); attendances.add(attendance); } } catch (Exception e) { e.printStackTrace(); } finally { DBConnection.close(conn, stmt, rs); } return attendances; } } ``` 4. 编写测试类,测试员工和考勤的增删改查等操作。 ```java public class Test { public static void main(String[] args) { EmployeeManager employeeManager = new EmployeeManager(); AttendanceManager attendanceManager = new AttendanceManager(); // 添加员工 Employee employee1 = new Employee(1, "张三", "研发部", "工程师"); employeeManager.addEmployee(employee1); Employee employee2 = new Employee(2, "李四", "市场部", "销售"); employeeManager.addEmployee(employee2); // 添加考勤记录 Attendance attendance1 = new Attendance(1, new Date(), 1, "正常"); attendanceManager.addAttendance(attendance1); Attendance attendance2 = new Attendance(2, new Date(), 2, "迟到"); attendanceManager.addAttendance(attendance2); // 查询员工列表 List<Employee> employees = employeeManager.getEmployees(); for (Employee employee : employees) { System.out.println(employee.getId() + " " + employee.getName() + " " + employee.getDepartment() + " " + employee.getPosition()); } // 查询考勤记录列表 List<Attendance> attendances = attendanceManager.getAttendances(); for (Attendance attendance : attendances) { System.out.println(attendance.getId() + " " + attendance.getDate() + " " + attendance.getEmployeeId() + " " + attendance.getStatus()); } // 更新员工信息 Employee employee3 = new Employee(2, "王五", "市场部", "销售经理"); employeeManager.updateEmployee(employee3); // 删除考勤记录 attendanceManager.deleteAttendance(2); // 删除员工 employeeManager.deleteEmployee(1); } } ``` 以上是一个简单的公司员工考勤管理系统的Java实现方案,其中还有很多细节需要处理,例如数据库连接池、异常处理、日期格式化等。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值