Advertisement
GeorgiLukanov87

Encapsulation - Exercise

Oct 25th, 2022 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.44 KB | None | 0 0
  1. # Encapsulation - Exercise
  2.  
  3. # https://judge.softuni.org/Contests/Compete/Index/1939#0
  4.  
  5. # 01. Wild Cat Zoo
  6. # 02. Pizza Maker
  7. # 03. Football Team Generator
  8. # 04. Restaurant
  9. =====================================================================================================================
  10.  _______________________
  11. |                       |
  12. |   01. Wild Cat Zoo    |
  13. |_______________________|
  14.  
  15. # file name: animal.py
  16. class Animal:
  17.     def __init__(self, name, gender, age, money_for_care):
  18.         self.name = name
  19.         self.gender = gender
  20.         self.age = age
  21.         self.money_for_care = money_for_care
  22.  
  23.     def __repr__(self):
  24.         return f'Name: {self.name}, Age: {self.age}, Gender: {self.gender}'
  25.  
  26. ----------------------------------------------------------------------------------------------------------------------
  27. # file name: caretaker.py
  28. from project.worker import Worker
  29.  
  30.  
  31. class Caretaker(Worker):
  32.     def __init__(self, name, age, salary):
  33.         super().__init__(name, age, salary)
  34.  
  35. ----------------------------------------------------------------------------------------------------------------------
  36. # file name: cheetah.py
  37. from project.animal import Animal
  38.  
  39.  
  40. class Cheetah(Animal):
  41.     MONEY_FOR_CARE = 60
  42.  
  43.     def __init__(self, name, gender, age):
  44.         super().__init__(name, gender, age, self.MONEY_FOR_CARE)
  45.  
  46. ----------------------------------------------------------------------------------------------------------------------
  47. # file name: keeper.py
  48. from project.worker import Worker
  49.  
  50.  
  51. class Keeper(Worker):
  52.     def __init__(self, name, age, salary):
  53.         super().__init__(name, age, salary)
  54.  
  55. ----------------------------------------------------------------------------------------------------------------------
  56. # file name: lion.py
  57. from project.animal import Animal
  58.  
  59.  
  60. class Lion(Animal):
  61.     MONEY_FOR_CARE = 50
  62.  
  63.     def __init__(self, name, gender, age):
  64.         super().__init__(name, gender, age, self.MONEY_FOR_CARE)
  65.        
  66. ----------------------------------------------------------------------------------------------------------------------
  67. # file name: tiger.py
  68. from project.animal import Animal
  69.  
  70.  
  71. class Tiger(Animal):
  72.     MONEY_FOR_CARE = 45
  73.  
  74.     def __init__(self, name, gender, age):
  75.         super().__init__(name, gender, age, self.MONEY_FOR_CARE)
  76.  
  77. ----------------------------------------------------------------------------------------------------------------------
  78. # file name: vet.py
  79. from project.worker import Worker
  80.  
  81.  
  82. class Vet(Worker):
  83.     def __init__(self, name, age, salary):
  84.         super().__init__(name, age, salary)
  85.  
  86. ----------------------------------------------------------------------------------------------------------------------
  87. # file name: worker.py
  88. class Worker:
  89.     def __init__(self, name, age, salary):
  90.         self.name = name
  91.         self.age = age
  92.         self.salary = salary
  93.  
  94.     def __repr__(self):
  95.         return f'Name: {self.name}, Age: {self.age}, Salary: {self.salary}'
  96.  
  97. ----------------------------------------------------------------------------------------------------------------------
  98. # file name: zoo.py
  99. from project.animal import Animal
  100. from project.caretaker import Caretaker
  101. from project.cheetah import Cheetah
  102. from project.keeper import Keeper
  103. from project.lion import Lion
  104. from project.tiger import Tiger
  105. from project.vet import Vet
  106. from project.worker import Worker
  107.  
  108.  
  109. class Zoo:
  110.     def __init__(self, name, budget, animal_capacity, workers_capacity):
  111.         self.name = name
  112.         self.__budget = budget
  113.         self.__animal_capacity = animal_capacity
  114.         self.__workers_capacity = workers_capacity
  115.         self.animals = []
  116.         self.workers = []
  117.  
  118.     def add_animal(self, animal: Animal, price):
  119.         if len(self.animals) == self.__animal_capacity:
  120.             return f'Not enough space for animal'
  121.         if price > self.__budget:
  122.             return f'Not enough budget'
  123.         self.animals.append(animal)
  124.         self.__budget -= price
  125.         return f"{animal.name} the {animal.__class__.__name__} added to the zoo"
  126.  
  127.     def hire_worker(self, worker: Worker):
  128.         if len(self.workers) == self.__workers_capacity:
  129.             return f'Not enough space for worker'
  130.         self.workers.append(worker)
  131.         return f'{worker.name} the {worker.__class__.__name__} hired successfully'
  132.  
  133.     def fire_worker(self, worker_name):
  134.         for worker in self.workers:
  135.             if worker.name == worker_name:
  136.                 self.workers.remove(worker)
  137.                 return f'{worker_name} fired successfully'
  138.         return f'There is no {worker_name} in the zoo'
  139.  
  140.     def pay_workers(self):
  141.         total_salaries = 0
  142.         for worker in self.workers:
  143.             total_salaries += worker.salary
  144.         if self.__budget >= total_salaries:
  145.             self.__budget -= total_salaries
  146.             return f"You payed your workers. They are happy. Budget left: {self.__budget}"
  147.         return f"You have no budget to pay your workers. They are unhappy"
  148.  
  149.     def tend_animals(self):
  150.         total_care_money = 0
  151.         for animal in self.animals:
  152.             total_care_money += animal.money_for_care
  153.         if self.__budget >= total_care_money:
  154.             self.__budget -= total_care_money
  155.             return f"You tended all the animals. They are happy. Budget left: {self.__budget}"
  156.         return f"You have no budget to tend the animals. They are unhappy."
  157.  
  158.     def profit(self, amount):
  159.         self.__budget += amount
  160.  
  161.     def animals_status(self):
  162.         result = f'You have {len(self.animals)} animals\n'
  163.         lions = [repr(animal) for animal in self.animals if isinstance(animal, Lion)]
  164.         result += f'----- {len(lions)} Lions:\n' + '\n'.join(lions) + '\n'
  165.  
  166.         tigers = [repr(animal) for animal in self.animals if isinstance(animal, Tiger)]
  167.         result += f'----- {len(tigers)} Tigers:\n' + '\n'.join(tigers) + '\n'
  168.  
  169.         cheetah = [repr(animal) for animal in self.animals if isinstance(animal, Cheetah)]
  170.         result += f'----- {len(cheetah)} Cheetahs:\n' + '\n'.join(cheetah)
  171.         return result
  172.  
  173.     def workers_status(self):
  174.         result = f"You have {len(self.workers)} workers\n"
  175.         keepers = [repr(worker) for worker in self.workers if isinstance(worker, Keeper)]
  176.         result += f'----- {len(keepers)} Keepers:\n' + '\n'.join(keepers) + '\n'
  177.  
  178.         caretakers = [repr(worker) for worker in self.workers if isinstance(worker, Caretaker)]
  179.         result += f'----- {len(caretakers)} Caretakers:\n' + '\n'.join(caretakers) + '\n'
  180.  
  181.         vets = [repr(worker) for worker in self.workers if isinstance(worker, Vet)]
  182.         result += f'----- {len(vets)} Vets:\n' + '\n'.join(vets)
  183.         return result
  184.  
  185. ======================================================================================================================
  186.  _______________________
  187. |                       |
  188. |   02. Pizza Maker     |
  189. |_______________________|
  190.  
  191. # file name: dough.py
  192. class Dough:
  193.     def __init__(self, flour_type: str, baking_technique: str, weight: float):
  194.         self.flour_type = flour_type
  195.         self.baking_technique = baking_technique
  196.         self.weight = weight
  197.  
  198.     @property
  199.     def flour_type(self):
  200.         return self.__flour_type
  201.  
  202.     @flour_type.setter
  203.     def flour_type(self, value):
  204.         if not value:
  205.             raise ValueError('The flour type cannot be an empty string')
  206.         self.__flour_type = value
  207.  
  208.     @property
  209.     def baking_technique(self):
  210.         return self.__baking_technique
  211.  
  212.     @baking_technique.setter
  213.     def baking_technique(self, value):
  214.         if not value:
  215.             raise ValueError('The baking technique cannot be an empty string')
  216.         self.__baking_technique = value
  217.  
  218.     @property
  219.     def weight(self):
  220.         return self.__weight
  221.  
  222.     @weight.setter
  223.     def weight(self, value):
  224.         if value <= 0:
  225.             raise ValueError('The weight cannot be less or equal to zero')
  226.         self.__weight = value
  227.  
  228. ----------------------------------------------------------------------------------------------------------------------
  229. # file name: pizza.py
  230. from project.dough import Dough
  231. from project.topping import Topping
  232.  
  233.  
  234. class Pizza:
  235.     def __init__(self, name: str, dough: Dough, toppings_capacity: int):
  236.         self.name = name
  237.         self.dough = dough
  238.         self.toppings_capacity = toppings_capacity
  239.         self.toppings = {}
  240.  
  241.     @property
  242.     def name(self):
  243.         return self.__name
  244.  
  245.     @name.setter
  246.     def name(self, value):
  247.         if not value:
  248.             raise ValueError('The name cannot be an empty string')
  249.         self.__name = value
  250.  
  251.     @property
  252.     def dough(self):
  253.         return self.__dough
  254.  
  255.     @dough.setter
  256.     def dough(self, value):
  257.         if value is None:
  258.             raise ValueError('You should add dough to the pizza')
  259.         self.__dough = value
  260.  
  261.     @property
  262.     def toppings_capacity(self):
  263.         return self.__toppings_capacity
  264.  
  265.     @toppings_capacity.setter
  266.     def toppings_capacity(self, value):
  267.         if value <= 0:
  268.             raise ValueError("The topping's capacity cannot be less or equal to zero")
  269.         self.__toppings_capacity = value
  270.  
  271.     def add_topping(self, toppings: Topping):
  272.         if len(self.toppings) == self.toppings_capacity:
  273.             raise ValueError("Not enough space for another topping")
  274.         if toppings.topping_type not in self.toppings:
  275.             self.toppings[toppings.topping_type] = toppings.weight
  276.         else:
  277.             self.toppings[toppings.topping_type] += toppings.weight
  278.  
  279.     def calculate_total_weight(self):
  280.         result = self.dough.weight + sum(self.toppings.values())
  281.         return result
  282.  
  283. ----------------------------------------------------------------------------------------------------------------------
  284. # file name: topping.py
  285. class Topping:
  286.  
  287.     def __init__(self, topping_type: str, weight: float):
  288.         self.topping_type = topping_type
  289.         self.weight = weight
  290.  
  291.     @property
  292.     def topping_type(self):
  293.         return self.__topping_type
  294.  
  295.     @topping_type.setter
  296.     def topping_type(self, value):
  297.         if not value:
  298.             raise ValueError('The topping type cannot be an empty string')
  299.         self.__topping_type = value
  300.  
  301.     @property
  302.     def weight(self):
  303.         return self.__weight
  304.  
  305.     @weight.setter
  306.     def weight(self, value):
  307.         if value <= 0:
  308.             raise ValueError('The weight cannot be less or equal to zero')
  309.         self.__weight = value
  310.  
  311.  
  312. ======================================================================================================================
  313.  ___________________________________
  314. |                                   |
  315. |   03. Football Team Generator     |
  316. |___________________________________|
  317.  
  318. # file name : player.py
  319. class Player:
  320.     def __init__(self, name: str, sprint: int, dribble: int, passing: int, shooting: int):
  321.         self.__name = name
  322.         self.__sprint = sprint
  323.         self.__dribble = dribble
  324.         self.__passing = passing
  325.         self.__shooting = shooting
  326.  
  327.     @property
  328.     def name(self):
  329.         return self.__name
  330.  
  331.     def __str__(self):
  332.         result = ''
  333.         result += f'Player: {self.name}\n'
  334.         result += f'Sprint: {self.__sprint}\n'
  335.         result += f'Dribble: {self.__dribble}\n'
  336.         result += f'Passing: {self.__passing}\n'
  337.         result += f'Shooting: {self.__shooting}'
  338.  
  339.         return result
  340.  
  341. ----------------------------------------------------------------------------------------------------------------------
  342. # file name: team.py
  343. from project.player import Player
  344.  
  345.  
  346. class Team:
  347.     def __init__(self, name: str, rating: int):
  348.         self.__name = name
  349.         self.__rating = rating
  350.         self.__players = []
  351.  
  352.     def add_player(self, player: Player):
  353.         if player in self.__players:
  354.             return f"Player {player.name} has already joined"
  355.         self.__players.append(player)
  356.         return f'Player {player.name} joined team {self.__name}'
  357.  
  358.     def remove_player(self, player_name: str):
  359.         for player in self.__players:
  360.             if player_name == player.name:
  361.                 self.__players.remove(player)
  362.                 return player
  363.         return f'Player {player_name} not found'
  364.  
  365.  
  366. ======================================================================================================================
  367.  
  368.  ______________________
  369. |                      |
  370. |   04. Restaurant     |
  371. |______________________|
  372. ----------------------------------------------------------------------------------------------------------------------
  373. # file name: product.py
  374. class Product:
  375.     def __init__(self, name: str, price: float):
  376.         self.__name = name
  377.         self.__price = price
  378.  
  379.     @property
  380.     def name(self):
  381.         return self.__name
  382.  
  383.     @property
  384.     def price(self):
  385.         return self.__price
  386.  
  387. ----------------------------------------------------------------------------------------------------------------------
  388. # file name: salmon.py
  389.  
  390. from project.food.main_dish import MainDish
  391.  
  392.  
  393. class Salmon(MainDish):
  394.     GRAMS = 22
  395.  
  396.     def __init__(self, name: str, price: float):
  397.         super().__init__(name, price, self.GRAMS)
  398. ----------------------------------------------------------------------------------------------------------------------
  399. # file name: food.py
  400. from project.product import Product
  401.  
  402.  
  403. class Food(Product):
  404.     def __init__(self, name: str, price: float, grams: float):
  405.         super().__init__(name, price)
  406.         self.__grams = grams
  407.  
  408.     @property
  409.     def grams(self):
  410.         return self.__grams
  411.  
  412. ----------------------------------------------------------------------------------------------------------------------
  413. # file name: dessert.py
  414. from project.food.food import Food
  415.  
  416.  
  417. class Dessert(Food):
  418.     def __init__(self, name: str, price: float, grams: float, calories: float):
  419.         super().__init__(name, price, grams)
  420.         self.__calories = calories
  421.  
  422.     @property
  423.     def calories(self):
  424.         return self.__calories
  425.  
  426. ----------------------------------------------------------------------------------------------------------------------
  427. # file name: cake.py
  428. from project.food.dessert import Dessert
  429.  
  430.  
  431. class Cake(Dessert):
  432.     GRAMS = 250
  433.     CALORIES = 1000
  434.     PRICE = 5
  435.  
  436.     def __init__(self, name: str):
  437.         super().__init__(name, self.PRICE,self.GRAMS,self.CALORIES)
  438.  
  439. ----------------------------------------------------------------------------------------------------------------------
  440. # file name: coffee.py
  441. from project.beverage.hot_beverage import HotBeverage
  442.  
  443.  
  444. class Coffee(HotBeverage):
  445.     MILLILITERS = 50
  446.     PRICE = 3.50
  447.  
  448.     def __init__(self, name: str, caffeine: float):
  449.         super().__init__(name, self.PRICE, self.MILLILITERS)
  450.         self.__caffeine = caffeine
  451.  
  452.     @property
  453.     def caffeine(self):
  454.         return self.__caffeine
  455.  
  456. ----------------------------------------------------------------------------------------------------------------------
  457. # file name: beverage.py
  458. from project.product import Product
  459.  
  460.  
  461. class Beverage(Product):
  462.     def __init__(self, name: str, price: float, milliliters: float):
  463.         super().__init__(name, price)
  464.         self.__milliliters = milliliters
  465.  
  466.     @property
  467.     def milliliters(self):
  468.         return self.__milliliters
  469.  
  470. ======================================================================================================================
  471.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement