Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Encapsulation - Exercise
- # https://judge.softuni.org/Contests/Compete/Index/1939#0
- # 01. Wild Cat Zoo
- # 02. Pizza Maker
- # 03. Football Team Generator
- # 04. Restaurant
- =====================================================================================================================
- _______________________
- | |
- | 01. Wild Cat Zoo |
- |_______________________|
- # file name: animal.py
- class Animal:
- def __init__(self, name, gender, age, money_for_care):
- self.name = name
- self.gender = gender
- self.age = age
- self.money_for_care = money_for_care
- def __repr__(self):
- return f'Name: {self.name}, Age: {self.age}, Gender: {self.gender}'
- ----------------------------------------------------------------------------------------------------------------------
- # file name: caretaker.py
- from project.worker import Worker
- class Caretaker(Worker):
- def __init__(self, name, age, salary):
- super().__init__(name, age, salary)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: cheetah.py
- from project.animal import Animal
- class Cheetah(Animal):
- MONEY_FOR_CARE = 60
- def __init__(self, name, gender, age):
- super().__init__(name, gender, age, self.MONEY_FOR_CARE)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: keeper.py
- from project.worker import Worker
- class Keeper(Worker):
- def __init__(self, name, age, salary):
- super().__init__(name, age, salary)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: lion.py
- from project.animal import Animal
- class Lion(Animal):
- MONEY_FOR_CARE = 50
- def __init__(self, name, gender, age):
- super().__init__(name, gender, age, self.MONEY_FOR_CARE)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: tiger.py
- from project.animal import Animal
- class Tiger(Animal):
- MONEY_FOR_CARE = 45
- def __init__(self, name, gender, age):
- super().__init__(name, gender, age, self.MONEY_FOR_CARE)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: vet.py
- from project.worker import Worker
- class Vet(Worker):
- def __init__(self, name, age, salary):
- super().__init__(name, age, salary)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: worker.py
- class Worker:
- def __init__(self, name, age, salary):
- self.name = name
- self.age = age
- self.salary = salary
- def __repr__(self):
- return f'Name: {self.name}, Age: {self.age}, Salary: {self.salary}'
- ----------------------------------------------------------------------------------------------------------------------
- # file name: zoo.py
- from project.animal import Animal
- from project.caretaker import Caretaker
- from project.cheetah import Cheetah
- from project.keeper import Keeper
- from project.lion import Lion
- from project.tiger import Tiger
- from project.vet import Vet
- from project.worker import Worker
- class Zoo:
- def __init__(self, name, budget, animal_capacity, workers_capacity):
- self.name = name
- self.__budget = budget
- self.__animal_capacity = animal_capacity
- self.__workers_capacity = workers_capacity
- self.animals = []
- self.workers = []
- def add_animal(self, animal: Animal, price):
- if len(self.animals) == self.__animal_capacity:
- return f'Not enough space for animal'
- if price > self.__budget:
- return f'Not enough budget'
- self.animals.append(animal)
- self.__budget -= price
- return f"{animal.name} the {animal.__class__.__name__} added to the zoo"
- def hire_worker(self, worker: Worker):
- if len(self.workers) == self.__workers_capacity:
- return f'Not enough space for worker'
- self.workers.append(worker)
- return f'{worker.name} the {worker.__class__.__name__} hired successfully'
- def fire_worker(self, worker_name):
- for worker in self.workers:
- if worker.name == worker_name:
- self.workers.remove(worker)
- return f'{worker_name} fired successfully'
- return f'There is no {worker_name} in the zoo'
- def pay_workers(self):
- total_salaries = 0
- for worker in self.workers:
- total_salaries += worker.salary
- if self.__budget >= total_salaries:
- self.__budget -= total_salaries
- return f"You payed your workers. They are happy. Budget left: {self.__budget}"
- return f"You have no budget to pay your workers. They are unhappy"
- def tend_animals(self):
- total_care_money = 0
- for animal in self.animals:
- total_care_money += animal.money_for_care
- if self.__budget >= total_care_money:
- self.__budget -= total_care_money
- return f"You tended all the animals. They are happy. Budget left: {self.__budget}"
- return f"You have no budget to tend the animals. They are unhappy."
- def profit(self, amount):
- self.__budget += amount
- def animals_status(self):
- result = f'You have {len(self.animals)} animals\n'
- lions = [repr(animal) for animal in self.animals if isinstance(animal, Lion)]
- result += f'----- {len(lions)} Lions:\n' + '\n'.join(lions) + '\n'
- tigers = [repr(animal) for animal in self.animals if isinstance(animal, Tiger)]
- result += f'----- {len(tigers)} Tigers:\n' + '\n'.join(tigers) + '\n'
- cheetah = [repr(animal) for animal in self.animals if isinstance(animal, Cheetah)]
- result += f'----- {len(cheetah)} Cheetahs:\n' + '\n'.join(cheetah)
- return result
- def workers_status(self):
- result = f"You have {len(self.workers)} workers\n"
- keepers = [repr(worker) for worker in self.workers if isinstance(worker, Keeper)]
- result += f'----- {len(keepers)} Keepers:\n' + '\n'.join(keepers) + '\n'
- caretakers = [repr(worker) for worker in self.workers if isinstance(worker, Caretaker)]
- result += f'----- {len(caretakers)} Caretakers:\n' + '\n'.join(caretakers) + '\n'
- vets = [repr(worker) for worker in self.workers if isinstance(worker, Vet)]
- result += f'----- {len(vets)} Vets:\n' + '\n'.join(vets)
- return result
- ======================================================================================================================
- _______________________
- | |
- | 02. Pizza Maker |
- |_______________________|
- # file name: dough.py
- class Dough:
- def __init__(self, flour_type: str, baking_technique: str, weight: float):
- self.flour_type = flour_type
- self.baking_technique = baking_technique
- self.weight = weight
- @property
- def flour_type(self):
- return self.__flour_type
- @flour_type.setter
- def flour_type(self, value):
- if not value:
- raise ValueError('The flour type cannot be an empty string')
- self.__flour_type = value
- @property
- def baking_technique(self):
- return self.__baking_technique
- @baking_technique.setter
- def baking_technique(self, value):
- if not value:
- raise ValueError('The baking technique cannot be an empty string')
- self.__baking_technique = value
- @property
- def weight(self):
- return self.__weight
- @weight.setter
- def weight(self, value):
- if value <= 0:
- raise ValueError('The weight cannot be less or equal to zero')
- self.__weight = value
- ----------------------------------------------------------------------------------------------------------------------
- # file name: pizza.py
- from project.dough import Dough
- from project.topping import Topping
- class Pizza:
- def __init__(self, name: str, dough: Dough, toppings_capacity: int):
- self.name = name
- self.dough = dough
- self.toppings_capacity = toppings_capacity
- self.toppings = {}
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, value):
- if not value:
- raise ValueError('The name cannot be an empty string')
- self.__name = value
- @property
- def dough(self):
- return self.__dough
- @dough.setter
- def dough(self, value):
- if value is None:
- raise ValueError('You should add dough to the pizza')
- self.__dough = value
- @property
- def toppings_capacity(self):
- return self.__toppings_capacity
- @toppings_capacity.setter
- def toppings_capacity(self, value):
- if value <= 0:
- raise ValueError("The topping's capacity cannot be less or equal to zero")
- self.__toppings_capacity = value
- def add_topping(self, toppings: Topping):
- if len(self.toppings) == self.toppings_capacity:
- raise ValueError("Not enough space for another topping")
- if toppings.topping_type not in self.toppings:
- self.toppings[toppings.topping_type] = toppings.weight
- else:
- self.toppings[toppings.topping_type] += toppings.weight
- def calculate_total_weight(self):
- result = self.dough.weight + sum(self.toppings.values())
- return result
- ----------------------------------------------------------------------------------------------------------------------
- # file name: topping.py
- class Topping:
- def __init__(self, topping_type: str, weight: float):
- self.topping_type = topping_type
- self.weight = weight
- @property
- def topping_type(self):
- return self.__topping_type
- @topping_type.setter
- def topping_type(self, value):
- if not value:
- raise ValueError('The topping type cannot be an empty string')
- self.__topping_type = value
- @property
- def weight(self):
- return self.__weight
- @weight.setter
- def weight(self, value):
- if value <= 0:
- raise ValueError('The weight cannot be less or equal to zero')
- self.__weight = value
- ======================================================================================================================
- ___________________________________
- | |
- | 03. Football Team Generator |
- |___________________________________|
- # file name : player.py
- class Player:
- def __init__(self, name: str, sprint: int, dribble: int, passing: int, shooting: int):
- self.__name = name
- self.__sprint = sprint
- self.__dribble = dribble
- self.__passing = passing
- self.__shooting = shooting
- @property
- def name(self):
- return self.__name
- def __str__(self):
- result = ''
- result += f'Player: {self.name}\n'
- result += f'Sprint: {self.__sprint}\n'
- result += f'Dribble: {self.__dribble}\n'
- result += f'Passing: {self.__passing}\n'
- result += f'Shooting: {self.__shooting}'
- return result
- ----------------------------------------------------------------------------------------------------------------------
- # file name: team.py
- from project.player import Player
- class Team:
- def __init__(self, name: str, rating: int):
- self.__name = name
- self.__rating = rating
- self.__players = []
- def add_player(self, player: Player):
- if player in self.__players:
- return f"Player {player.name} has already joined"
- self.__players.append(player)
- return f'Player {player.name} joined team {self.__name}'
- def remove_player(self, player_name: str):
- for player in self.__players:
- if player_name == player.name:
- self.__players.remove(player)
- return player
- return f'Player {player_name} not found'
- ======================================================================================================================
- ______________________
- | |
- | 04. Restaurant |
- |______________________|
- ----------------------------------------------------------------------------------------------------------------------
- # file name: product.py
- class Product:
- def __init__(self, name: str, price: float):
- self.__name = name
- self.__price = price
- @property
- def name(self):
- return self.__name
- @property
- def price(self):
- return self.__price
- ----------------------------------------------------------------------------------------------------------------------
- # file name: salmon.py
- from project.food.main_dish import MainDish
- class Salmon(MainDish):
- GRAMS = 22
- def __init__(self, name: str, price: float):
- super().__init__(name, price, self.GRAMS)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: food.py
- from project.product import Product
- class Food(Product):
- def __init__(self, name: str, price: float, grams: float):
- super().__init__(name, price)
- self.__grams = grams
- @property
- def grams(self):
- return self.__grams
- ----------------------------------------------------------------------------------------------------------------------
- # file name: dessert.py
- from project.food.food import Food
- class Dessert(Food):
- def __init__(self, name: str, price: float, grams: float, calories: float):
- super().__init__(name, price, grams)
- self.__calories = calories
- @property
- def calories(self):
- return self.__calories
- ----------------------------------------------------------------------------------------------------------------------
- # file name: cake.py
- from project.food.dessert import Dessert
- class Cake(Dessert):
- GRAMS = 250
- CALORIES = 1000
- PRICE = 5
- def __init__(self, name: str):
- super().__init__(name, self.PRICE,self.GRAMS,self.CALORIES)
- ----------------------------------------------------------------------------------------------------------------------
- # file name: coffee.py
- from project.beverage.hot_beverage import HotBeverage
- class Coffee(HotBeverage):
- MILLILITERS = 50
- PRICE = 3.50
- def __init__(self, name: str, caffeine: float):
- super().__init__(name, self.PRICE, self.MILLILITERS)
- self.__caffeine = caffeine
- @property
- def caffeine(self):
- return self.__caffeine
- ----------------------------------------------------------------------------------------------------------------------
- # file name: beverage.py
- from project.product import Product
- class Beverage(Product):
- def __init__(self, name: str, price: float, milliliters: float):
- super().__init__(name, price)
- self.__milliliters = milliliters
- @property
- def milliliters(self):
- return self.__milliliters
- ======================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement