Browse Source

feat(w4):W4-孟鑫垚-202506010204

main
Mengxinyao 2 weeks ago
parent
commit
0e088f925b
  1. 87
      w3/public class Employee {.groovy
  2. 17
      w3/public class TestEmployee {.java

87
w3/public class Employee {.groovy

@ -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 +
'}';
}
}

17
w3/public class TestEmployee {.java

@ -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…
Cancel
Save