-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompanyComposition.java
78 lines (63 loc) · 1.87 KB
/
CompanyComposition.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package kb.design_patterns.composite;
import java.util.ArrayList;
import java.util.List;
@FunctionalInterface
interface Employee {
void showDetails();
}
class Developer implements Employee {
private String name;
private String position;
private double salary;
public Developer(String name, String position, double salary) {
this.name = name;
this.position = position;
this.salary = salary;
}
@Override
public void showDetails() {
System.out.println("Name: " + name + ", position: " + position + ", salary: " + salary);
}
}
class Manager implements Employee {
private String name;
private String position;
private double salary;
public Manager(String name, String position, double salary) {
this.name = name;
this.position = position;
this.salary = salary;
}
@Override
public void showDetails() {
System.out.println("Name: " + name + ", position: " + position + ", salary: " + salary);
}
}
class Company implements Employee {
private List<Employee> employees = new ArrayList<>();
private String companyName;
public Company(String companyName) {
this.companyName = companyName;
}
public void add(Employee employee) {
employees.add(employee);
}
public void remove(Employee employee) {
employees.remove(employee);
}
@Override
public void showDetails() {
System.out.println("Company name: " + companyName);
employees.forEach(Employee::showDetails);
}
}
public class CompanyComposition {
public static void main(String[] args) {
Employee e1 = new Developer("A.S.", "developer", 7500.0);
Employee e2 = new Developer("D.N.", "manager", 12500.0);
Company c = new Company("The best company");
c.add(e1);
c.add(e2);
c.showDetails();
}
}