Skip to content

Commit 5a598f5

Browse files
committed
Update Exercise
1 parent ba68c7a commit 5a598f5

4 files changed

Lines changed: 36 additions & 17 deletions

File tree

Company.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,39 @@
22

33
public class Company {
44

5-
private String name;
6-
private ArrayList<Employee> employees;
5+
private final String name;
6+
private final ArrayList<Employee> employees;
77
private int numberOfEmployees;
88

99
public Company(String name) {
1010
this.name = name;
1111
employees = new ArrayList<>();
12+
numberOfEmployees = 0;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public ArrayList<Employee> getEmployees() {
20+
return employees;
21+
}
22+
23+
public int getNumberOfEmployees() {
24+
return numberOfEmployees;
1225
}
1326

1427
public void addEmployee(Employee employee) {
1528
employees.add(employee);
1629
numberOfEmployees++;
1730
}
1831

19-
public void print() {
20-
System.out.println(name + " (" + numberOfEmployees + " Mitarbeiter)");
21-
for (Employee e : employees) {
22-
e.print();
32+
public String toString() {
33+
String result = "";
34+
result += name + " (" + numberOfEmployees + " Mitarbeiter)" + "\n";
35+
for (Employee employee : employees) {
36+
result += employee.toString() + "\n";
2337
}
38+
return result;
2439
}
2540
}

Employee.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
public class Employee {
22

3-
private int employeeId;
4-
private Person person;
5-
private int salary;
3+
private final int employeeId;
4+
private final Person person;
5+
private int salaryInEuro;
66

7-
public Employee(int employeeId, Person person, int salary) {
7+
public Employee(int employeeId, Person person, int salaryInEuro) {
88
this.employeeId = employeeId;
99
this.person = person;
10-
this.salary = salary;
10+
this.salaryInEuro = salaryInEuro;
1111
}
1212

1313
public int getEmployeeId() {
@@ -18,11 +18,15 @@ public String getName() {
1818
return person.getName();
1919
}
2020

21-
public int getSalary() {
22-
return salary;
21+
public int getSalaryInEuro() {
22+
return salaryInEuro;
2323
}
2424

25-
public void print() {
26-
System.out.println(employeeId + " - " + getName() + " - " + salary + "€");
25+
public void setSalaryInEuro(int salaryInEuro) {
26+
this.salaryInEuro = salaryInEuro;
27+
}
28+
29+
public String toString() {
30+
return employeeId + " - " + getName() + " - " + salaryInEuro + "€";
2731
}
2832
}

Exercise.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ 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-
company.print();
20+
System.out.println(company.toString());
2121
}
2222
}

Person.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
public class Person {
22

3-
private String name;
3+
private final String name;
44

55
public Person(String name) {
66
this.name = name;

0 commit comments

Comments
 (0)