-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee.java
50 lines (47 loc) · 1.68 KB
/
Employee.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Employee extends Person { //subclass of Person
public Employee(String name, int salary) throws NegativeSalaryException {
super(name, -salary);
if (salary < 0) { //The salary given as argument is strictly less than 0
throw new NegativeSalaryException("An employee cannot have a negative salary!");
}
}
@Override
public void pay(int amount) throws NegativeSalaryException {
if (getDebt() + amount > 0) { //The argument is too positive and would change the employee's salary to become negative
throw new NegativeSalaryException("An employee cannot be overpaid by " + (amount + getDebt()) + " yuans!");
}
setDebt(getDebt() + amount);
}
public static void testEmployee() {
try {
Employee e = new Employee("Daniel", -10000);
System.out.println(false);
} catch (NegativeSalaryException e) {
System.out.println(e.getMessage() == "An employee cannot have a negative salary!");
}
try {
Employee e = new Employee("Daniel", 0);
} catch (NegativeSalaryException e) {
System.out.println(false);
}
try {
Employee e = new Employee("Daniel", 10000);
System.out.println(e.getName() == "Daniel");
System.out.println(e.getDebt() == -10000);
e.setDebt(20000);
System.out.println(e.getDebt() == 20000);
e.setDebt(-10000);
System.out.println(e.getDebt() == -10000);
e.pay(2000);
System.out.println(e.getDebt() == -8000);
e.pay(-2000);
System.out.println(e.getDebt() == -10000);
e.pay(10000);
System.out.println(e.getDebt() == 0);
e.pay(1);
System.out.println(false);
} catch (NegativeSalaryException e) {
System.out.println(e.getMessage().equals("An employee cannot be overpaid by 1 yuans!"));
}
}
}