import java.time.LocalDate; /** * 证书类 - 负责证书信息管理 * 遵循单一职责原则:只负责证书数据的存储和状态管理 */ public class Certificate { private String certificateId; // 证书ID private String employeeId; // 员工ID private String courseId; // 课程ID private String courseName; // 课程名称 private LocalDate issueDate; // 颁发日期 private LocalDate expiryDate; // 过期日期 private String issuer; // 颁发机构 private String trainer; // 培训师 private double score; // 成绩 private boolean active; // 是否有效 private String certificateNumber; // 证书编号 private String description; // 证书描述 /** * 构造函数 - 创建证书 */ public Certificate(String certificateId, String employeeId, String courseId, String courseName, String issuer, String trainer, double score) { this.certificateId = certificateId; this.employeeId = employeeId; this.courseId = courseId; this.courseName = courseName; this.issuer = issuer; this.trainer = trainer; this.score = score; this.issueDate = LocalDate.now(); this.active = true; this.certificateNumber = generateCertificateNumber(certificateId, employeeId); this.description = ""; } /** * 生成证书编号 */ private String generateCertificateNumber(String certificateId, String employeeId) { LocalDate now = LocalDate.now(); return String.format("CERT-%s-%s-%d", employeeId, certificateId, now.getYear() * 10000 + now.getMonthValue() * 100 + now.getDayOfMonth()); } /** * 设置证书有效期 * @param years 有效期年数 */ public void setValidYears(int years) { if (years <= 0) { throw new IllegalArgumentException("有效期必须大于0"); } this.expiryDate = issueDate.plusYears(years); } /** * 检查证书是否过期 * @return true如果证书已过期 */ public boolean isExpired() { if (expiryDate == null) { return false; // 没有设置过期日期的证书永不过期 } return LocalDate.now().isAfter(expiryDate); } /** * 吊销证书 * @param reason 吊销原因 */ public void revokeCertificate(String reason) { this.active = false; this.description = reason; } /** * 恢复证书 */ public void reinstateCertificate() { if (!isExpired()) { this.active = true; } else { throw new IllegalStateException("已过期的证书无法恢复"); } } /** * 获取证书状态描述 * @return 证书状态描述 */ public String getStatusDescription() { if (!active) { return "已吊销"; } else if (isExpired()) { return "已过期"; } else { return "有效"; } } /** * 获取剩余有效天数 * @return 剩余有效天数,如果不过期则返回null */ public Long getRemainingValidDays() { if (expiryDate == null || isExpired()) { return null; } return (long) LocalDate.now().until(expiryDate).getDays(); } /** * 获取成绩等级 * @return 成绩等级 */ public String getScoreGrade() { if (score >= 90) return "优秀"; else if (score >= 80) return "良好"; else if (score >= 70) return "中等"; else if (score >= 60) return "及格"; else return "不及格"; } // Getter和Setter方法 public String getCertificateId() { return certificateId; } public String getEmployeeId() { return employeeId; } public String getCourseId() { return courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public LocalDate getIssueDate() { return issueDate; } public LocalDate getExpiryDate() { return expiryDate; } public String getIssuer() { return issuer; } public void setIssuer(String issuer) { this.issuer = issuer; } public String getTrainer() { return trainer; } public void setTrainer(String trainer) { this.trainer = trainer; } public double getScore() { return score; } public boolean isActive() { return active; } public String getCertificateNumber() { return certificateNumber; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Certificate{" + "certificateId='" + certificateId + "'" + ", certificateNumber='" + certificateNumber + "'" + ", courseName='" + courseName + "'" + ", employeeId='" + employeeId + "'" + ", score=" + score + "(" + getScoreGrade() + ")" + ", issueDate=" + issueDate + "'" + ", expiryDate=" + (expiryDate != null ? expiryDate : "永不过期") + "'" + ", status=" + getStatusDescription() + "'" + "}"; } }