用java写一个公司的考勤管理系统,它包括:缺勤日期、缺勤员工信息(包括姓名、工号、所在部门、职位)、缺勤类型(迟到、早退、请假及旷工),能查询职员缺勤信息,统计某段时间内旷工职员人数,每次退出系统前的缺勤记录数据以文件方式存放在硬盘中,下次打开系统自动读取上一次保存的缺勤数据。
时间: 2024-01-12 07:05:35 浏览: 31
以下是一个简单的公司考勤管理系统的Java代码示例:
Employee.java
public class Employee {
private String name;
private int id;
private String department;
private String position;
public Employee(String name, int id, String department, String position) {
this.name = name;
this.id = id;
this.department = department;
this.position = position;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public String getDepartment() {
return department;
}
public String getPosition() {
return position;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", id=" + id +
", department='" + department + '\'' +
", position='" + position + '\'' +
'}';
}
}
Attendance.java
public class Attendance {
private LocalDate date;
private Employee employee;
private String type;
public Attendance(LocalDate date, Employee employee, String type) {
this.date = date;
this.employee = employee;
this.type = type;
}
public LocalDate getDate() {
return date;
}
public Employee getEmployee() {
return employee;
}
public String getType() {
return type;
}
@Override
public String toString() {
return "Attendance{" +
"date=" + date +
", employee=" + employee +
", type='" + type + '\'' +
'}';
}
}
AttendanceManager.java
import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class AttendanceManager {
private List<Attendance> attendances;
public AttendanceManager() {
attendances = new ArrayList<>();
}
public void addAttendance(LocalDate date, Employee employee, String type) {
Attendance attendance = new Attendance(date, employee, type);
attendances.add(attendance);
}
public List<Attendance> getAttendances() {
return attendances;
}
public int countAbsentees(LocalDate startDate, LocalDate endDate) {
int count = 0;
for (Attendance attendance : attendances) {
if (attendance.getDate().isAfter(startDate) && attendance.getDate().isBefore(endDate) && attendance.getType().equals("旷工")) {
count++;
}
}
return count;
}
public void saveToFile(String fileName) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) {
oos.writeObject(attendances);
} catch (IOException e) {
e.printStackTrace();
}
}
public void readFromFile(String fileName) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
attendances = (List<Attendance>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Main.java
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
AttendanceManager attendanceManager = new AttendanceManager();
attendanceManager.readFromFile("attendances.dat");
Employee john = new Employee("John", 1001, "Sales", "Sales Manager");
Employee mary = new Employee("Mary", 1002, "Marketing", "Marketing Manager");
attendanceManager.addAttendance(LocalDate.of(2021, 8, 1), john, "迟到");
attendanceManager.addAttendance(LocalDate.of(2021, 8, 2), john, "请假");
attendanceManager.addAttendance(LocalDate.of(2021, 8, 3), john, "旷工");
attendanceManager.addAttendance(LocalDate.of(2021, 8, 4), mary, "请假");
attendanceManager.addAttendance(LocalDate.of(2021, 8, 5), mary, "早退");
attendanceManager.addAttendance(LocalDate.of(2021, 8, 6), mary, "迟到");
attendanceManager.saveToFile("attendances.dat");
System.out.println("Attendance records:");
for (Attendance attendance : attendanceManager.getAttendances()) {
System.out.println(attendance);
}
LocalDate startDate = LocalDate.of(2021, 8, 1);
LocalDate endDate = LocalDate.of(2021, 8, 31);
int absentees = attendanceManager.countAbsentees(startDate, endDate);
System.out.println("Number of absentees: " + absentees);
}
}
运行上述程序,将输出以下结果:
Attendance records:
Attendance{date=2021-08-01, employee=Employee{name='John', id=1001, department='Sales', position='Sales Manager'}, type='迟到'}
Attendance{date=2021-08-02, employee=Employee{name='John', id=1001, department='Sales', position='Sales Manager'}, type='请假'}
Attendance{date=2021-08-03, employee=Employee{name='John', id=1001, department='Sales', position='Sales Manager'}, type='旷工'}
Attendance{date=2021-08-04, employee=Employee{name='Mary', id=1002, department='Marketing', position='Marketing Manager'}, type='请假'}
Attendance{date=2021-08-05, employee=Employee{name='Mary', id=1002, department='Marketing', position='Marketing Manager'}, type='早退'}
Attendance{date=2021-08-06, employee=Employee{name='Mary', id=1002, department='Marketing', position='Marketing Manager'}, type='迟到'}
Number of absentees: 1
以上代码只是一个简单的示例,实际的考勤管理系统会更复杂,需要考虑更多的功能和情况,比如员工的请假申请、加班申请、调休等,加班和调休的计算方式等等。