File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33public 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}
Original file line number Diff line number Diff line change 11public 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11public class Person {
22
3- private String name ;
3+ private final String name ;
44
55 public Person (String name ) {
66 this .name = name ;
You can’t perform that action at this time.
0 commit comments