2 changed files with 104 additions and 0 deletions
@ -0,0 +1,87 @@ |
|||
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 + |
|||
'}'; |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
public class TestEmployee { |
|||
public static void main(String[] args) { |
|||
Employee emp1 = new Employee("E001", "张三", "技术部", 5000.0); |
|||
Employee emp2 = new Employee("E002", "李四", "销售部", 1800.0); // 会自动设为 2000
|
|||
|
|||
System.out.println("初始状态:"); |
|||
System.out.println(emp1); |
|||
System.out.println(emp2); |
|||
|
|||
emp1.raiseSalary(10); // 涨 10%
|
|||
emp2.raiseSalary(-5); // 无效操作
|
|||
|
|||
System.out.println("\n调整后:"); |
|||
System.out.println(emp1); |
|||
System.out.println(emp2); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue