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.
101 lines
2.7 KiB
101 lines
2.7 KiB
public class Employee {
|
|
|
|
// 静态变量:公司名称
|
|
private static String companyName = "ABC科技有限公司";
|
|
|
|
// 最低工资标准
|
|
private static final double MIN_SALARY = 2000.0;
|
|
|
|
// 属性:工号、姓名、部门、工资
|
|
private String id;
|
|
private String name;
|
|
private String department;
|
|
private double salary;
|
|
|
|
// 全参构造方法(同时为 companyName 赋值)
|
|
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 static String getCompanyName() {
|
|
return companyName;
|
|
}
|
|
|
|
public static void setCompanyName(String companyName) {
|
|
Employee.companyName = companyName;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 设置工资,工资必须大于等于最低工资标准(2000)
|
|
*/
|
|
public void setSalary(double salary) {
|
|
if (salary >= MIN_SALARY) {
|
|
this.salary = salary;
|
|
} else {
|
|
System.out.println("警告:工资不能低于最低工资标准 " + MIN_SALARY
|
|
+ " 元,已自动设置为最低工资标准。");
|
|
this.salary = MIN_SALARY;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 按百分比涨工资
|
|
* percent 为 5 表示增加 5%
|
|
* 增加后仍不低于最低工资
|
|
*/
|
|
public void raiseSalary(double percent) {
|
|
double newSalary = this.salary * (1 + percent / 100.0);
|
|
if (newSalary < MIN_SALARY) {
|
|
newSalary = MIN_SALARY;
|
|
}
|
|
this.salary = newSalary;
|
|
System.out.printf("涨薪 %.1f%%,%s 的新工资为:%.2f 元%n", percent, this.name, this.salary);
|
|
}
|
|
|
|
/**
|
|
* 输出员工信息
|
|
*/
|
|
public void printInfo() {
|
|
System.out.println("==============================");
|
|
System.out.println("公司:" + companyName);
|
|
System.out.println("工号:" + id);
|
|
System.out.println("姓名:" + name);
|
|
System.out.println("部门:" + department);
|
|
System.out.printf("工资:%.2f 元%n", salary);
|
|
System.out.println("==============================");
|
|
}
|
|
}
|
|
|