/** * 员工历史记录类 * 负责记录员工的历史变更信息,如职位变更、薪资调整等 * 遵循单一职责原则,只负责历史记录的管理 */ package com.employeetraining.history; import com.employeetraining.model.Employee; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Optional; public class EmployeeHistory { private String employeeId; private List historyRecords; private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public EmployeeHistory(String employeeId) { this.employeeId = employeeId; this.historyRecords = new ArrayList<>(); } /** * 记录职位变更 */ public void recordPositionChange(String oldPosition, String newPosition) { String description = String.format("职位变更: 从 '%s' 变更为 '%s'", oldPosition, newPosition); addRecord(HistoryType.POSITION_CHANGE, description); } /** * 记录薪资调整 */ public void recordSalaryChange(double oldSalary, double newSalary) { String description = String.format("薪资调整: 从 %.2f 调整为 %.2f", oldSalary, newSalary); addRecord(HistoryType.SALARY_CHANGE, description); } /** * 记录部门变更 */ public void recordDepartmentChange(String oldDepartment, String newDepartment) { String description = String.format("部门变更: 从 '%s' 变更为 '%s'", oldDepartment, newDepartment); addRecord(HistoryType.DEPARTMENT_CHANGE, description); } /** * 添加自定义记录 */ public void addCustomRecord(String description) { addRecord(HistoryType.CUSTOM, description); } /** * 内部方法:添加记录 */ private void addRecord(HistoryType type, String description) { HistoryRecord record = new HistoryRecord(LocalDateTime.now(), type, description); historyRecords.add(record); } /** * 获取所有历史记录 */ public List getAllRecords() { return new ArrayList<>(historyRecords); // 返回副本,防止外部修改 } /** * 根据类型获取历史记录 */ public List getRecordsByType(HistoryType type) { return historyRecords.stream() .filter(record -> record.getType() == type) .toList(); } /** * 获取最新的历史记录 */ public Optional getLatestRecord() { if (historyRecords.isEmpty()) { return Optional.empty(); } return Optional.of(historyRecords.get(historyRecords.size() - 1)); } /** * 获取记录总数 */ public int getRecordCount() { return historyRecords.size(); } /** * 历史记录类型枚举 */ public enum HistoryType { POSITION_CHANGE, SALARY_CHANGE, DEPARTMENT_CHANGE, CUSTOM } /** * 历史记录内部类 */ public class HistoryRecord { private final LocalDateTime timestamp; private final HistoryType type; private final String description; public HistoryRecord(LocalDateTime timestamp, HistoryType type, String description) { this.timestamp = timestamp; this.type = type; this.description = description; } public LocalDateTime getTimestamp() { return timestamp; } public String getFormattedTimestamp() { return timestamp.format(formatter); } public HistoryType getType() { return type; } public String getDescription() { return description; } @Override public String toString() { return "[" + getFormattedTimestamp() + "] " + type + ": " + description; } } }