You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.0 KiB

public class Employee {
// 私有属性:封装
private String id;
private String name;
private String department;
private double salary;
// 最低工资标准常量
private static final double MIN_SALARY = 2000.0;
// 全参构造方法
public Employee(String id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
// 使用 setSalary 来确保工资符合最低标准
setSalary(salary);
}
// Getter 和 Setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
// 在 setSalary 中检查工资是否低于最低标准
public void setSalary(double salary) {
if (salary < MIN_SALARY) {
System.out.println("警告:工资不能低于最低工资 " + MIN_SALARY + " 元,已自动设置为最低工资。");
this.salary = MIN_SALARY;
} else {
this.salary = salary;
}
}
// 涨薪方法:percent 为 5 表示增加 5%
public void raiseSalary(double percent) {
if (percent <= 0) {
System.out.println("涨薪百分比必须大于 0");
return;
}
double increase = this.salary * (percent / 100);
double newSalary = this.salary + increase;
setSalary(newSalary); // 复用 setSalary 的校验逻辑
}
// 方便展示员工信息的方法
public void displayInfo() {
System.out.println("工号:" + id + ",姓名:" + name + ",部门:" + department + ",工资:" + salary);
}
}