请用java代码写出考勤夜班记录新增以及重复数据更新最新
这里是一份示例代码,用于实现考勤夜班记录的新增和重复数据更新:
import java.util.ArrayList;
import java.util.List;
public class AttendanceNightShift {
private String employeeId;
private String employeeName;
private String date;
private String startTime;
private String endTime;
// 构造函数
public AttendanceNightShift(String employeeId, String employeeName, String date, String startTime, String endTime) {
this.employeeId = employeeId;
this.employeeName = employeeName;
this.date = date;
this.startTime = startTime;
this.endTime = endTime;
}
// 获取员工ID
public String getEmployeeId() {
return employeeId;
}
// 设置员工ID
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
// 获取员工姓名
public String getEmployeeName() {
return employeeName;
}
// 设置员工姓名
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
// 获取日期
public String getDate() {
return date;
}
// 设置日期
public void setDate(String date) {
this.date = date;
}
// 获取开始时间
public String getStartTime() {
return startTime;
}
// 设置开始时间
public void setStartTime(String startTime) {
this.startTime = startTime;
}
// 获取结束时间
public String getEndTime() {
return endTime;
}
// 设置结束时间
public void setEndTime(String endTime) {
this.endTime = endTime;
}
// 新增考勤夜班记录
public static void addAttendanceNightShift(List<AttendanceNightShift> attendanceList, AttendanceNightShift attendance) {
attendanceList.add(attendance);
}
// 更新考勤夜班记录
public static void updateAttendanceNightShift(List<AttendanceNightShift> attendanceList, AttendanceNightShift attendance) {
for (AttendanceNightShift a : attendanceList) {
if (a.getEmployeeId().equals(attendance.getEmployeeId()) && a.getDate().equals(attendance.getDate())) {
a.setStartTime(attendance.getStartTime());
a.setEndTime(attendance.getEndTime());
return;
}
}
addAttendanceNightShift(attendanceList, attendance);
}
// 示例代码的主函数
public static void main(String[] args) {
// 创建一个考勤列表
List<AttendanceNightShift> attendanceList = new ArrayList<>();
// 新增一条考勤记录
AttendanceNightShift attendance1 = new AttendanceNightShift("001", "张三", "2022-01-01", "21:00", "06:00");
addAttendanceNightShift(attendanceList, attendance1);
// 再次新增相同的考勤记录,会被忽略
AttendanceNightShift attendance2 = new AttendanceNightShift("001", "张三", "2022-01-01", "21:00", "06:00");
addAttendanceNightShift(attendanceList, attendance2);
// 更新考勤记录
AttendanceNightShift attendance3 = new AttendanceNightShift("001", "张三", "2022-01-01", "22:00", "07:00");
updateAttendanceNightShift(attendanceList, attendance3);
// 再次更新考勤记录,因为已经存在相同的记录,所以只会更新最新的记录
AttendanceNightShift attendance4 = new AttendanceNightShift("001", "张三", "2022-01-01", "23:00", "08:00");
updateAttendanceNightShift(attendanceList, attendance4);
// 输出考勤列表
for (AttendanceNightShift a : attendanceList) {
System.out.println(a.getEmployeeId() + " " + a.getEmployeeName() + " " + a.getDate() + " " + a.getStartTime() + " " + a.getEndTime());
}
}
}
在这份示例代码中,我们创建了一个AttendanceNightShift类来表示考勤夜班记录,其中包含员工ID、员工姓名、日期、开始时间和结束时间等属性,还定义了新增和更新考勤记录的静态方法。在主函数中,我们创建了一个考勤列表,并演示了如何使用上述方法来新增和更新考勤记录。当我们尝试新增已经存在的记录时,会被忽略;而当我们尝试更新已经存在的记录时,只会更新最新的记录。最后,我们输出了考勤列表,以检查我们的操作是否成功。
原文地址: https://www.cveoy.top/t/topic/bHwv 著作权归作者所有。请勿转载和采集!