Skip to content

Commit 2f38803

Browse files
committed
Added Encapsulation - Exercise
1 parent 549a008 commit 2f38803

37 files changed

+452
-0
lines changed
Binary file not shown.

OOP/4.Encapsulation/Encapsulation - Exercise/01. Wild Cat Zoo/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Animal:
2+
3+
def __init__(self, name, gender, age, money_for_care):
4+
self.name = name
5+
self.gender = gender
6+
self.age = age
7+
self.money_for_care = money_for_care
8+
9+
def __repr__(self):
10+
return f"Name: {self.name}, Age: {self.age}, Gender: {self.gender}"
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from project.worker import Worker
2+
3+
4+
class Caretaker(Worker):
5+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from project.animal import Animal
2+
3+
4+
class Cheetah(Animal):
5+
MONEY_FOR_CARE = 60
6+
7+
def __init__(self, name: str, gender: str, age: int):
8+
super().__init__(name, gender, age, Cheetah.MONEY_FOR_CARE)
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from project.worker import Worker
2+
3+
4+
class Keeper(Worker):
5+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from project.animal import Animal
2+
3+
4+
class Lion(Animal):
5+
MONEY_FOR_CARE = 50
6+
7+
def __init__(self, name: str, gender: str, age: int):
8+
super().__init__(name, gender, age, Lion.MONEY_FOR_CARE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from project.animal import Animal
2+
3+
4+
class Tiger(Animal):
5+
MONEY_FOR_CARE = 45
6+
7+
def __init__(self, name: str, gender: str, age: int):
8+
super().__init__(name, gender, age, Tiger.MONEY_FOR_CARE)
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from project.worker import Worker
2+
3+
4+
class Vet(Worker):
5+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Worker:
2+
3+
def __init__(self, name, age, salary):
4+
self.name = name
5+
self.age = age
6+
self.salary = salary
7+
8+
def __repr__(self):
9+
return f"Name: {self.name}, Age: {self.age}, Salary: {self.salary}"
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from project.animal import Animal
2+
from project.worker import Worker
3+
4+
5+
class Zoo:
6+
7+
def __init__(self, name, budget, animal_capacity, workers_capacity):
8+
self.name = name
9+
self.__budget = budget
10+
self.__animal_capacity = animal_capacity
11+
self.__workers_capacity = workers_capacity
12+
self.animals = []
13+
self.workers = []
14+
15+
def add_animal(self, animal: Animal, price):
16+
if self.__budget < price:
17+
return "Not enough budget"
18+
if self.__animal_capacity == len(self.animals):
19+
return "Not enough space for animal"
20+
21+
self.animals.append(animal)
22+
self.__budget -= price
23+
24+
return f"{animal.name} the {animal.__class__.__name__} added to the zoo"
25+
26+
def hire_worker(self, worker: Worker):
27+
if self.__workers_capacity == len(self.workers):
28+
return "Not enough space for worker"
29+
30+
self.workers.append(worker)
31+
32+
return f"{worker.name} the {worker.__class__.__name__} hired successfully"
33+
34+
def fire_worker(self, worker_name):
35+
try:
36+
worker = next(filter(lambda w: w.name == worker_name, self.workers))
37+
except StopIteration:
38+
return f"There is no {worker_name} in the zoo"
39+
40+
self.workers.remove(worker)
41+
42+
return f"{worker_name} fired successfully"
43+
44+
def pay_workers(self):
45+
salaries = sum(w.salary for w in self.workers)
46+
47+
if self.__budget < salaries:
48+
return "You have no budget to pay your workers. They are unhappy"
49+
50+
self.__budget -= salaries
51+
52+
return f"You payed your workers. They are happy. Budget left: {self.__budget}"
53+
54+
def tend_animals(self):
55+
animals_cost = sum(a.money_for_care for a in self.animals)
56+
57+
if self.__budget < animals_cost:
58+
return "You have no budget to tend the animals. They are unhappy."
59+
60+
self.__budget -= animals_cost
61+
62+
return f"You tended all the animals. They are happy. Budget left: {self.__budget}"
63+
64+
def profit(self, amount):
65+
self.__budget += amount
66+
67+
def animals_status(self):
68+
lions = list(filter(lambda a: a.__class__.__name__ == "Lion", self.animals))
69+
tigers = list(filter(lambda a: a.__class__.__name__ == "Tiger", self.animals))
70+
cheetahs = list(filter(lambda a: a.__class__.__name__ == "Cheetah", self.animals))
71+
72+
result = [
73+
f"You have {len(self.animals)} animals",
74+
f"----- {len(lions)} Lions:",
75+
]
76+
result.extend(lions)
77+
78+
result.append(f"----- {len(tigers)} Tigers:")
79+
result.extend(tigers)
80+
81+
result.append(f"----- {len(cheetahs)} Cheetahs:")
82+
result.extend(cheetahs)
83+
84+
return "\n".join(str(x) for x in result)
85+
86+
def workers_status(self):
87+
info = {"Keeper": [], "Caretaker": [], "Vet": []}
88+
[info[w.__class__.__name__].append(str(w)) for w in self.workers]
89+
90+
result = [
91+
f"You have {len(self.workers)} workers",
92+
f"----- {len(info['Keeper'])} Keepers:",
93+
*info['Keeper'],
94+
f"----- {len(info['Caretaker'])} Caretakers:",
95+
*info['Caretaker'],
96+
f"----- {len(info['Vet'])} Vets:",
97+
*info['Vet'],
98+
]
99+
100+
return "\n".join(result)
101+
Binary file not shown.

OOP/4.Encapsulation/Encapsulation - Exercise/02. Pizza Maker/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Dough:
2+
def __init__(self, flour_type, baking_technique, weight):
3+
self.flour_type = flour_type
4+
self.baking_technique = baking_technique
5+
self.weight = weight
6+
7+
@property
8+
def flour_type(self):
9+
return self.__flour_type
10+
11+
@flour_type.setter
12+
def flour_type(self, value):
13+
if value == '':
14+
raise ValueError("The flour type cannot be an empty string")
15+
16+
self.__flour_type = value
17+
18+
@property
19+
def baking_technique(self):
20+
return self.__baking_technique
21+
22+
@baking_technique.setter
23+
def baking_technique(self, value):
24+
if value == '':
25+
raise ValueError("The baking technique cannot be an empty string")
26+
27+
self.__baking_technique = value
28+
29+
@property
30+
def weight(self):
31+
return self.__weight
32+
33+
@weight.setter
34+
def weight(self, value):
35+
if value <= 0:
36+
raise ValueError("The weight cannot be less or equal to zero")
37+
38+
self.__weight = value
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from project.dough import Dough
2+
from project.topping import Topping
3+
4+
5+
class Pizza:
6+
def __init__(self, name, dough: Dough, max_number_of_toppings):
7+
self.name = name
8+
self.dough = dough
9+
self.max_number_of_toppings = max_number_of_toppings
10+
self.toppings = {}
11+
12+
@property
13+
def name(self):
14+
return self.__name
15+
16+
@name.setter
17+
def name(self, value):
18+
if value == '':
19+
raise ValueError("The name cannot be an empty string")
20+
21+
self.__name = value
22+
23+
@property
24+
def dough(self):
25+
return self.__dough
26+
27+
@dough.setter
28+
def dough(self, value):
29+
if value is None:
30+
raise ValueError("You should add dough to the pizza")
31+
32+
self.__dough = value
33+
34+
@property
35+
def max_number_of_toppings(self):
36+
return self.__max_number_of_toppings
37+
38+
@max_number_of_toppings.setter
39+
def max_number_of_toppings(self, value):
40+
if value <= 0:
41+
raise ValueError("The maximum number of toppings cannot be less or equal to zero")
42+
43+
self.__max_number_of_toppings = value
44+
45+
def add_topping(self, topping: Topping):
46+
if len(self.toppings) == self.max_number_of_toppings:
47+
raise ValueError("Not enough space for another topping")
48+
49+
if topping.topping_type not in self.toppings:
50+
self.toppings[topping.topping_type] = 0
51+
52+
self.toppings[topping.topping_type] += topping.weight
53+
54+
def calculate_total_weight(self):
55+
return sum(self.toppings.values()) + self.dough.weight
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Topping:
2+
3+
# def __init__(self, topping_type, weight):
4+
# if topping_type == '':
5+
# raise ValueError("The topping type cannot be an empty string")
6+
# if weight <= 0:
7+
# raise ValueError("The weight cannot be less or equal to zero")
8+
#
9+
# self.topping_type = topping_type
10+
# self.weight = weight
11+
12+
def __init__(self, topping_type, weight):
13+
self.topping_type = topping_type
14+
self.weight = weight
15+
16+
@property
17+
def topping_type(self):
18+
return self.__topping_type
19+
20+
@topping_type.setter
21+
def topping_type(self, value):
22+
if value == '':
23+
raise ValueError("The topping type cannot be an empty string")
24+
25+
self.__topping_type = value
26+
27+
@property
28+
def weight(self):
29+
return self.__weight
30+
31+
@weight.setter
32+
def weight(self, value):
33+
if value <= 0:
34+
raise ValueError("The weight cannot be less or equal to zero")
35+
36+
self.__weight = value
37+
Binary file not shown.

OOP/4.Encapsulation/Encapsulation - Exercise/03. Football Team Generator/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Player:
2+
def __init__(self, name, sprint, dribble, passing, shooting):
3+
self.__name = name
4+
self.__sprint = sprint
5+
self.__dribble = dribble
6+
self.__passing = passing
7+
self.__shooting = shooting
8+
9+
@property
10+
def name(self):
11+
return self.__name
12+
13+
def __str__(self):
14+
return f"Player: {self.__name}\n" \
15+
f"Sprint: {self.__sprint}\n" \
16+
f"Dribble: {self.__dribble}\n" \
17+
f"Passing: {self.__passing}\n" \
18+
f"Shooting: {self.__shooting}"
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from project.player import Player
2+
3+
4+
class Team:
5+
def __init__(self, name: str, rating: int):
6+
self.__name = name
7+
self.__rating = rating
8+
self.__players = []
9+
10+
def add_player(self, player: Player) -> str:
11+
if player in self.__players:
12+
return f"Player {player.name} has already joined"
13+
14+
self.__players.append(player)
15+
16+
return f"Player {player.name} joined team {self.__name}"
17+
18+
def remove_player(self, player_name: str) -> str or Player:
19+
try:
20+
player = next(filter(lambda p: p.name == player_name, self.__players))
21+
except StopIteration:
22+
return f"Player {player_name} not found"
23+
24+
self.__players.remove(player)
25+
26+
return player
27+
Binary file not shown.

OOP/4.Encapsulation/Encapsulation - Exercise/04. Restaurant/project/__init__.py

Whitespace-only changes.

OOP/4.Encapsulation/Encapsulation - Exercise/04. Restaurant/project/beverage/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from project.product import Product
2+
3+
4+
class Beverage(Product):
5+
def __init__(self, name, price, milliliters):
6+
super().__init__(name, price)
7+
self.__milliliters = milliliters
8+
9+
@property
10+
def milliliters(self):
11+
return self.__milliliters
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from project.beverage.hot_beverage import HotBeverage
2+
3+
4+
class Coffee(HotBeverage):
5+
MILLILITERS = 50
6+
PRICE = 3.50
7+
8+
def __init__(self, name, caffeine):
9+
super().__init__(name, Coffee.PRICE, Coffee.MILLILITERS)
10+
self.__caffeine = caffeine
11+
12+
@property
13+
def caffeine(self):
14+
return self.__caffeine
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from project.beverage.beverage import Beverage
2+
3+
4+
class ColdBeverage(Beverage):
5+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from project.beverage.beverage import Beverage
2+
3+
4+
class HotBeverage(Beverage):
5+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from project.beverage.hot_beverage import HotBeverage
2+
3+
4+
class Tea(HotBeverage):
5+
pass

OOP/4.Encapsulation/Encapsulation - Exercise/04. Restaurant/project/food/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)