import java.time.LocalDate; /** * 员工类 - 负责员工基本信息管理 * 遵循单一职责原则:只负责员工数据的存储和基本验证 */ public class Employee { private String id; // 员工ID private String name; // 员工姓名 private double salary; // 薪资 private String department; // 部门 private String position; // 职位 private LocalDate hireDate; // 入职日期 private LocalDate birthDate; // 出生日期 private String email; // 邮箱(新增属性) private boolean active; // 是否在职 /** * 构造函数 - 创建员工对象 */ public Employee(String id, String name, double salary, String department, String position, LocalDate hireDate, LocalDate birthDate, String email) { this.id = id; this.name = name; this.salary = salary; this.department = department; this.position = position; this.hireDate = hireDate; this.birthDate = birthDate; this.active = true; setEmail(email); // 使用setter进行邮箱验证 } /** * 设置邮箱 - 包含验证逻辑 * @param email 邮箱地址 * @throws IllegalArgumentException 如果邮箱格式无效 */ public void setEmail(String email) { if (isValidEmail(email)) { this.email = email; } else { throw new IllegalArgumentException("无效的邮箱格式: " + email); } } /** * 邮箱格式验证 * @param email 邮箱地址 * @return true如果邮箱格式有效 */ private boolean isValidEmail(String email) { if (email == null || email.trim().isEmpty()) { return false; } // 基本的邮箱格式验证正则表达式 String emailRegex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"; return email.matches(emailRegex); } /** * 获取员工年龄 * @return 员工年龄 */ public int getAge() { LocalDate now = LocalDate.now(); int age = now.getYear() - birthDate.getYear(); if (now.getMonthValue() < birthDate.getMonthValue() || (now.getMonthValue() == birthDate.getMonthValue() && now.getDayOfMonth() < birthDate.getDayOfMonth())) { age--; } return age; } /** * 获取员工司龄 * @return 员工司龄(年) */ public int getTenure() { LocalDate now = LocalDate.now(); int tenure = now.getYear() - hireDate.getYear(); if (now.getMonthValue() < hireDate.getMonthValue() || (now.getMonthValue() == hireDate.getMonthValue() && now.getDayOfMonth() < hireDate.getDayOfMonth())) { tenure--; } return tenure; } // Getter和Setter方法 - 遵循单一职责原则,只负责属性的访问和修改 public String getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { if (salary >= 0) { this.salary = salary; } else { throw new IllegalArgumentException("薪资不能为负数"); } } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public LocalDate getHireDate() { return hireDate; } public LocalDate getBirthDate() { return birthDate; } public String getEmail() { return email; } public boolean isActive() { return active; } public void setActive(boolean active) { this.active = active; } @Override public String toString() { return "Employee{" + "id='" + id + "'" + ", name='" + name + "'" + ", salary=" + salary + ", department='" + department + "'" + ", position='" + position + "'" + ", hireDate=" + hireDate + ", age=" + getAge() + ", tenure=" + getTenure() + "年" + ", email='" + email + "'" + ", active=" + (active ? "在职" : "离职") + "}"; } }