-
Notifications
You must be signed in to change notification settings - Fork 0
/
Composite.py
61 lines (49 loc) · 1.68 KB
/
Composite.py
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
class ComponentCompany():
def getTotalSalary(self):
throw NotImplementedError
class Department(ComponentCompany):
def __init__(self,name):
self.name = name
self.subdepartments = []
def add(self, worker):
self.subdepartments.append(worker)
def getTotalSalary(self):
res = 0
for worker in self.subdepartments:
res += worker.getTotalSalary()
return res
class Worker(Department):
def __init__(self,name ,salary ):
self.name = name
self.salary = salary
def getTotalSalary(self):
return self.salary
class CompositeCompany(ComponentCompany):
def __init__(self):
self.departments = []
def add(self, worker):
self.departments.append(worker)
def getTotalSalary(self):
res = 0
for worker in self.departments:
res += worker.getTotalSalary()
return res
company = CompositeCompany()
worker1 = Worker("Makr", 3500)
worker2 = Worker("Borya", 1500)
worker3 = Worker("Katay", 4000)
worker4 = Worker("Vova", 6000)
main_department = Department("Main Department")
minor_department = Department("Minor Department")
second_department = Department("Second Department")
sub_minor_department = Department("Subminor Department")
sub_minor_department = Department("Subminor Department")
sub_minor_department.add(worker2)
second_department.add(worker1)
minor_department.add(worker3)
main_department.add(worker4)
minor_department.add(sub_minor_department)
main_department.add(second_department)
main_department.add(minor_department)
company.add(main_department)
print(company.getTotalSalary())