Skip to content

Commit 70b9ff1

Browse files
committed
implement solution
1 parent ba68c7a commit 70b9ff1

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

Employee.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ public int getSalary() {
2222
return salary;
2323
}
2424

25+
public void setSalary(int salary) throws SalaryDecreaseException, SalaryIncreaseTooHighException {
26+
if (this.salary > salary) {
27+
throw new SalaryDecreaseException();
28+
}
29+
30+
double change = (double) (salary - this.salary) / this.salary;
31+
if (change > 0.1) {
32+
throw new SalaryIncreaseTooHighException();
33+
}
34+
35+
this.salary = salary;
36+
}
37+
2538
public void print() {
2639
System.out.println(employeeId + " - " + getName() + " - " + salary + "€");
2740
}

Exercise.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public static void main(String[] args) {
1717
company.addEmployee(new Employee(4, new Person("Peter Schneider"), 55000));
1818
company.addEmployee(new Employee(5, new Person("Miriam Albers"), 90000));
1919

20+
try {
21+
e1.setSalary(55000);
22+
e2.setSalary(77000);
23+
e3.setSalary(45000);
24+
} catch (SalaryDecreaseException | SalaryIncreaseTooHighException e) {
25+
System.err.println(e.getMessage());
26+
}
27+
2028
company.print();
2129
}
2230
}

SalaryDecreaseException.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class SalaryDecreaseException extends Exception {
2+
3+
private static final long serialVersionUID = 1L;
4+
5+
public SalaryDecreaseException() {
6+
super("Das neue Gehalt muss hoeher sein als das bisherige");
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class SalaryIncreaseTooHighException extends Exception {
2+
3+
private static final long serialVersionUID = 1L;
4+
5+
public SalaryIncreaseTooHighException() {
6+
super("Das neue Gehalt darf maximal 10% ueber dem bisherigen Gehalt liegen");
7+
}
8+
}

0 commit comments

Comments
 (0)