diff --git a/Certificate.java b/Certificate.java new file mode 100644 index 0000000..d7d7c41 --- /dev/null +++ b/Certificate.java @@ -0,0 +1,173 @@ +import java.time.LocalDate; +import java.util.Objects; +import java.util.UUID; + +/** + * 证书类:管理员工获得的证书 + */ +public class Certificate { + private String certificateId; // 证书ID + private int employeeId; // 员工ID + private String title; // 证书名称 + private String issuingAuthority; // 发证机构 + private LocalDate issueDate; // 颁发日期 + private LocalDate expiryDate; // 到期日期(可选) + private String description; // 证书描述 + private String certificateUrl; // 证书电子版URL(可选) + private boolean isActive; // 是否有效 + + /** + * 构造函数 + */ + public Certificate(int employeeId, String title, String issuingAuthority, + LocalDate issueDate) { + validateEmployeeId(employeeId); + validateTitle(title); + validateIssuingAuthority(issuingAuthority); + validateIssueDate(issueDate); + + this.certificateId = generateCertificateId(); + this.employeeId = employeeId; + this.title = title; + this.issuingAuthority = issuingAuthority; + this.issueDate = issueDate; + this.isActive = true; // 默认为有效证书 + this.description = ""; + this.certificateUrl = null; + } + + /** + * 构造函数(包含到期日期) + */ + public Certificate(int employeeId, String title, String issuingAuthority, + LocalDate issueDate, LocalDate expiryDate) { + this(employeeId, title, issuingAuthority, issueDate); + + if (expiryDate != null && expiryDate.isBefore(issueDate)) { + throw new IllegalArgumentException("到期日期不能早于颁发日期"); + } + this.expiryDate = expiryDate; + + // 如果有到期日期且已过期,则标记为无效 + if (expiryDate != null && expiryDate.isBefore(LocalDate.now())) { + this.isActive = false; + } + } + + // 生成证书ID + private String generateCertificateId() { + return "CERT-" + UUID.randomUUID().toString().substring(0, 8).toUpperCase(); + } + + // 验证方法 + private void validateEmployeeId(int employeeId) { + if (employeeId <= 0) { + throw new IllegalArgumentException("员工ID必须为正整数"); + } + } + + private void validateTitle(String title) { + if (title == null || title.trim().isEmpty()) { + throw new IllegalArgumentException("证书名称不能为空"); + } + } + + private void validateIssuingAuthority(String issuingAuthority) { + if (issuingAuthority == null || issuingAuthority.trim().isEmpty()) { + throw new IllegalArgumentException("发证机构不能为空"); + } + } + + private void validateIssueDate(LocalDate issueDate) { + if (issueDate == null || issueDate.isAfter(LocalDate.now())) { + throw new IllegalArgumentException("颁发日期不能晚于当前日期"); + } + } + + /** + * 检查证书是否过期 + */ + public boolean isExpired() { + return expiryDate != null && expiryDate.isBefore(LocalDate.now()); + } + + /** + * 检查证书是否仍然有效 + */ + public boolean isValid() { + return isActive && !isExpired(); + } + + /** + * 更新证书到期日期 + */ + public void updateExpiryDate(LocalDate expiryDate) { + if (expiryDate != null && expiryDate.isBefore(issueDate)) { + throw new IllegalArgumentException("到期日期不能早于颁发日期"); + } + this.expiryDate = expiryDate; + + // 更新状态 + if (expiryDate != null && expiryDate.isBefore(LocalDate.now())) { + this.isActive = false; + } + } + + // Getter方法 + public String getCertificateId() { return certificateId; } + public int getEmployeeId() { return employeeId; } + public String getTitle() { return title; } + public String getIssuingAuthority() { return issuingAuthority; } + public LocalDate getIssueDate() { return issueDate; } + public LocalDate getExpiryDate() { return expiryDate; } + public String getDescription() { return description; } + public String getCertificateUrl() { return certificateUrl; } + public boolean isActive() { return isActive; } + + // Setter方法 + public void setTitle(String title) { + validateTitle(title); + this.title = title; + } + + public void setIssuingAuthority(String issuingAuthority) { + validateIssuingAuthority(issuingAuthority); + this.issuingAuthority = issuingAuthority; + } + + public void setDescription(String description) { + this.description = description != null ? description : ""; + } + + public void setCertificateUrl(String certificateUrl) { + this.certificateUrl = certificateUrl; + } + + public void activate() { + this.isActive = true; + } + + public void deactivate() { + this.isActive = false; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Certificate that = (Certificate) o; + return certificateId.equals(that.certificateId); + } + + @Override + public int hashCode() { + return Objects.hash(certificateId); + } + + @Override + public String toString() { + return String.format("证书[ID: %s, 名称: %s, 员工ID: %d, 发证机构: %s, 状态: %s]", + certificateId, title, employeeId, issuingAuthority, + isValid() ? "有效" : "无效"); + } +} \ No newline at end of file