Skip to content

Solution for Exceptions 03 #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: exercises/exceptions/03
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ public int getSalaryInEuro() {
return salaryInEuro;
}

public void setSalaryInEuro(int salaryInEuro) {
public void setSalaryInEuro(int salaryInEuro)
throws SalaryDecreaseException, SalaryIncreaseTooHighException {
if (this.salaryInEuro > salaryInEuro) {
throw new SalaryDecreaseException();
}

double change = (double) (salaryInEuro - this.salaryInEuro) / this.salaryInEuro;
if (change > 0.1) {
throw new SalaryIncreaseTooHighException();
}

this.salaryInEuro = salaryInEuro;
}

Expand Down
33 changes: 33 additions & 0 deletions Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@ public static void main(String[] args) {
company.addEmployee(new Employee(4, new Person("Peter Schneider"), 55000));
company.addEmployee(new Employee(5, new Person("Miriam Albers"), 90000));

try {
e1.setSalaryInEuro(55000);
e2.setSalaryInEuro(77000);
e3.setSalaryInEuro(45000);
} catch (SalaryDecreaseException | SalaryIncreaseTooHighException e) {
System.err.println(e.getMessage());
}

// alternative multiple catch:
try {
e1.setSalaryInEuro(55000);
e2.setSalaryInEuro(77000);
e3.setSalaryInEuro(45000);
} catch (SalaryDecreaseException e) {
System.out.println(e.getMessage());
} catch (SalaryIncreaseTooHighException e) {
System.out.println(e.getMessage());
}

// alternative instance of
try {
e1.setSalaryInEuro(55000);
e2.setSalaryInEuro(77000);
e3.setSalaryInEuro(45000);
} catch (Exception e) {
if (e instanceof SalaryDecreaseException) {
System.out.println(e.getMessage());
}
if (e instanceof SalaryIncreaseTooHighException) {
System.out.println(e.getMessage());
}
}

System.out.println(company.toString());
}
}
7 changes: 7 additions & 0 deletions SalaryDecreaseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class SalaryDecreaseException extends Exception {

public SalaryDecreaseException() {
super("Das neue Gehalt muss hoeher sein als das bisherige");
// alternativ: super();
}
}
7 changes: 7 additions & 0 deletions SalaryIncreaseTooHighException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class SalaryIncreaseTooHighException extends Exception {

public SalaryIncreaseTooHighException() {
super("Das neue Gehalt darf maximal 10% ueber dem bisherigen Gehalt liegen");
// alternativ: super();
}
}