ADD file via upload

main
pm4c6ia2v 6 months ago
parent e213220747
commit 2a85c1dfb5

@ -0,0 +1,143 @@
/**
*
*
*
*/
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<HistoryRecord> 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<HistoryRecord> getAllRecords() {
return new ArrayList<>(historyRecords); // 返回副本,防止外部修改
}
/**
*
*/
public List<HistoryRecord> getRecordsByType(HistoryType type) {
return historyRecords.stream()
.filter(record -> record.getType() == type)
.toList();
}
/**
*
*/
public Optional<HistoryRecord> 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;
}
}
}
Loading…
Cancel
Save