1 changed files with 78 additions and 0 deletions
@ -0,0 +1,78 @@ |
|||
public class Employee { |
|||
private String id; // 工号
|
|||
private String name; // 姓名
|
|||
private String department; // 部门
|
|||
private double salary; // 工资
|
|||
|
|||
public static String companyName; // 静态变量
|
|||
|
|||
private static final double MIN_SALARY = 2000.0; // 最低工资标准常量
|
|||
|
|||
// 全参构造方法
|
|||
public Employee(String id, String name, String department, double salary, String companyName) { |
|||
this.id = id; |
|||
this.name = name; |
|||
this.department = department; |
|||
this.setSalary(salary); |
|||
Employee.companyName = companyName; // 为静态变量赋值(所有实例共享)
|
|||
} |
|||
|
|||
// 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); |
|||
this.salary = MIN_SALARY; |
|||
} |
|||
} |
|||
|
|||
// 涨薪方法
|
|||
public void raiseSalary(double percent) { |
|||
if (percent < 0) { |
|||
System.out.println("涨薪百分比不能为负数,操作取消"); |
|||
return; |
|||
} |
|||
double newSalary = this.salary * (1 + percent / 100); |
|||
this.salary = Math.max(newSalary, MIN_SALARY); |
|||
System.out.println(name + " 涨薪后工资:" + this.salary); |
|||
} |
|||
|
|||
// 打印员工信息
|
|||
@Override |
|||
public String toString() { |
|||
return "公司:" + companyName + |
|||
" | 工号:" + id + |
|||
" | 姓名:" + name + |
|||
" | 部门:" + department + |
|||
" | 工资:" + salary; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue