要用到一个Employee雇员类,这个Employee类中有两个属性,分别是雇员的姓名name和当前工资salary,在这个雇员类上加了一些限制: 固定雇员的工资不得低于工资的最低标准3000元 普通雇员每次工资的变化幅度不得高于原工资的20% 经理也是员工,但是每次工资的变化幅度不能高于原工资的30% 为上面的限制定义了两个用户异常IllegalSalaryException和 IllegalSalaryChangeException 并分别定义Employee、Manager类,其中Manager类是Employee类的子类,并编写一个简单的测试类Test,测试员工的最低工资的异常处理和员工工资变化幅度异常的处理。
时间: 2024-03-05 22:48:48 浏览: 31
新建一个雇员类,它的数据成员有雇员代号,年龄,工资,性别, 姓名,输入雇员资料方法,打印雇员资料方法。
以下是一个可能的实现,包括Employee、Manager类以及相应的异常类和测试类:
```java
class IllegalSalaryException extends Exception {
public IllegalSalaryException(String message) {
super(message);
}
}
class IllegalSalaryChangeException extends Exception {
public IllegalSalaryChangeException(String message) {
super(message);
}
}
class Employee {
private String name;
private double salary;
private static final double MIN_SALARY = 3000;
public Employee(String name, double salary) throws IllegalSalaryException {
if (salary < MIN_SALARY) {
throw new IllegalSalaryException("Salary is below the minimum standard.");
}
this.name = name;
this.salary = salary;
}
public void setSalary(double newSalary) throws IllegalSalaryChangeException {
double maxChange = salary * 0.2;
if (Math.abs(newSalary - salary) > maxChange) {
throw new IllegalSalaryChangeException("Salary change is too large.");
}
salary = newSalary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}
class Manager extends Employee {
private static final double MAX_CHANGE = 0.3;
public Manager(String name, double salary) throws IllegalSalaryException {
super(name, salary);
}
@Override
public void setSalary(double newSalary) throws IllegalSalaryChangeException {
double maxChange = getSalary() * MAX_CHANGE;
if (Math.abs(newSalary - getSalary()) > maxChange) {
throw new IllegalSalaryChangeException("Salary change is too large.");
}
super.setSalary(newSalary);
}
}
public class Test {
public static void main(String[] args) {
try {
// Test minimum salary
Employee e1 = new Employee("John", 2500);
System.out.println("Employee created: " + e1.getName() + ", salary: " + e1.getSalary());
} catch (IllegalSalaryException e) {
System.out.println("Illegal salary exception: " + e.getMessage());
}
try {
// Test salary change for employee
Employee e2 = new Employee("Mary", 5000);
System.out.println("Employee created: " + e2.getName() + ", salary: " + e2.getSalary());
e2.setSalary(5500);
System.out.println("Employee salary changed to: " + e2.getSalary());
e2.setSalary(6000); // This should throw an exception
} catch (IllegalSalaryException | IllegalSalaryChangeException e) {
System.out.println("Exception: " + e.getMessage());
}
try {
// Test salary change for manager
Manager m1 = new Manager("Bob", 8000);
System.out.println("Manager created: " + m1.getName() + ", salary: " + m1.getSalary());
m1.setSalary(8400);
System.out.println("Manager salary changed to: " + m1.getSalary());
m1.setSalary(9000); // This should throw an exception
} catch (IllegalSalaryException | IllegalSalaryChangeException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
```
该程序输出:
```
Illegal salary exception: Salary is below the minimum standard.
Employee created: Mary, salary: 5000.0
Employee salary changed to: 5500.0
Exception: Salary change is too large.
Manager created: Bob, salary: 8000.0
Manager salary changed to: 8400.0
Exception: Salary change is too large.
```
这个程序创建了两个类:Employee和Manager。其中Employee类有一个name属性和一个salary属性,Manager类是Employee类的子类,因此继承了这两个属性。Employee类和Manager类都有一个构造函数,可以初始化这两个属性。Employee类和Manager类也都有一个setSalary函数,可以设置salary属性,但是在设置salary属性时会检查是否满足限制。如果不满足限制,则会抛出相应的异常。Test类中创建了三个对象,分别测试了最低工资异常和工资变化幅度异常。
阅读全文