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.
 
 

87 lines
2.0 KiB

public class Employee {
// 属性:工号、姓名、部门、工资
private String id;
private String name;
private String department;
private double salary;
// 最低工资标准(假设为2000)
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;
this.salary = salary;
}
// 无参构造方法(可选)
public Employee() {
}
// 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;
}
public void setSalary(double salary) {
if (salary >= MIN_SALARY) {
this.salary = salary;
} else {
System.out.println("工资不能低于最低工资标准 " + MIN_SALARY);
}
}
// 按百分比增加工资的方法
public void raiseSalary(double percent) {
if (percent < 0) {
System.out.println("百分比不能为负数");
return;
}
double newSalary = salary * (1 + percent / 100);
// 确保增加后仍不低于最低工资
if (newSalary < MIN_SALARY) {
this.salary = MIN_SALARY;
} else {
this.salary = newSalary;
}
}
// toString 便
@Override
public String toString() {
return "Employee{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", department='" + department + '\'' +
", salary=" + salary +
'}';
}
}