Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python OOP Exam - 15 August 2021 - Bakery
- # https://judge.softuni.org/Contests/Practice/Index/3591#0
- =============================================================================================
- # file name: baked_food.py
- from abc import ABC, abstractmethod
- class BakedFood(ABC):
- def __init__(self, name: str, portion: float, price: float):
- self.name = name
- self.portion = portion
- self.price = price
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, value):
- if value.strip() == '':
- raise ValueError('Name cannot be empty string or white space!')
- self.__name = value
- @property
- def price(self):
- return self.__price
- @price.setter
- def price(self, value):
- if value <= 0:
- raise ValueError("Price cannot be less than or equal to zero!")
- self.__price = value
- @abstractmethod
- def __repr__(self):
- ...
- =============================================================================================
- # file name: bread.py
- from project.baked_food.baked_food import BakedFood
- class Bread(BakedFood):
- def __init__(self, name: str, price: float):
- super().__init__(name, 200, price)
- def __repr__(self):
- return f" - {self.name}: {self.portion:.2f}g - {self.price:.2f}lv"
- =============================================================================================
- # file name: cake.py
- from project.baked_food.baked_food import BakedFood
- class Cake(BakedFood):
- def __init__(self, name: str, price: float):
- super().__init__(name, 245, price)
- def __repr__(self):
- return f" - {self.name}: {self.portion:.2f}g - {self.price:.2f}lv"
- =============================================================================================
- # file name: drink.py
- from abc import ABC, abstractmethod
- class Drink(ABC):
- def __init__(self, name: str, portion: float, price: float, brand: str):
- self.name = name
- self.portion = portion
- self.price = price
- self.brand = brand
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, value):
- if value.strip() == '':
- raise ValueError("Name cannot be empty string or white space!")
- self.__name = value
- @property
- def portion(self):
- return self.__portion
- @portion.setter
- def portion(self, value):
- if value <= 0:
- raise ValueError("Portion cannot be less than or equal to zero!")
- self.__portion = value
- @property
- def brand(self):
- return self.__brand
- @brand.setter
- def brand(self, value):
- if value.strip() == "":
- raise ValueError("Brand cannot be empty string or white space!")
- self.__brand = value
- @abstractmethod
- def __str__(self):
- ...
- =============================================================================================
- # file name: tea.py
- from project.drink.drink import Drink
- class Tea(Drink):
- def __init__(self, name: str, portion: float, brand: str):
- super().__init__(name, portion, 2.50, brand)
- def __str__(self):
- return f" - {self.name} {self.brand} - {self.portion:.2f}ml - {self.price:.2f}lv"
- =============================================================================================
- # file name: water.py
- from project.drink.drink import Drink
- class Water(Drink):
- def __init__(self, name: str, portion: float, brand: str):
- super().__init__(name, portion, 1.50, brand)
- def __str__(self):
- return f" - {self.name} {self.brand} - {self.portion:.2f}ml - {self.price:.2f}lv"
- =============================================================================================
- # file name: table.py
- from abc import ABC, abstractmethod
- from project.baked_food.baked_food import BakedFood
- from project.drink.drink import Drink
- class Table(ABC):
- def __init__(self, table_number: int, capacity: int):
- self.table_number = table_number
- self.capacity = capacity
- self.food_orders = []
- self.drink_orders = []
- self.number_of_people = 0
- self.is_reserved = False
- @property
- def capacity(self):
- return self.__capacity
- @capacity.setter
- def capacity(self, value):
- if value <= 0:
- raise ValueError("Capacity has to be greater than 0!")
- self.__capacity = value
- @abstractmethod
- def reserve(self, number_of_people):
- ...
- @abstractmethod
- def order_food(self, baked_food: BakedFood):
- ...
- @abstractmethod
- def order_drink(self, drink: Drink):
- ...
- @abstractmethod
- def get_bill(self):
- ...
- @abstractmethod
- def clear(self):
- pass
- @abstractmethod
- def free_table_info(self):
- ...
- =============================================================================================
- # file name: inside_table.py
- from project.baked_food.baked_food import BakedFood
- from project.drink.drink import Drink
- from project.table.table import Table
- class InsideTable(Table):
- def __init__(self, table_number: int, capacity: int):
- super().__init__(table_number, capacity)
- @property
- def table_number(self):
- return self.__table_number
- @table_number.setter
- def table_number(self, value):
- if not 1 <= value <= 50:
- raise ValueError("Inside table's number must be between 1 and 50 inclusive!")
- self.__table_number = value
- def reserve(self, number_of_people):
- if self.capacity >= number_of_people and not self.is_reserved:
- self.number_of_people += number_of_people
- self.is_reserved = True
- return f'Table {self.table_number} has been reserved for {number_of_people} people'
- else:
- return f'No available table for {number_of_people} people'
- def order_food(self, baked_food: BakedFood):
- self.food_orders.append(baked_food)
- def order_drink(self, drink: Drink):
- self.drink_orders.append(drink)
- def get_bill(self):
- return sum([d.price for d in self.drink_orders]) + sum([f.price for f in self.food_orders])
- def clear(self):
- self.drink_orders = []
- self.food_orders = []
- self.number_of_people = 0
- self.is_reserved = False
- def free_table_info(self):
- if not self.is_reserved:
- return f"Table: {self.table_number}" + \
- f'\nType: {self.__class__.__name__}' \
- + f"\nCapacity: {self.capacity}"
- =============================================================================================
- # file name: outside_table.py
- from project.baked_food.baked_food import BakedFood
- from project.drink.drink import Drink
- from project.table.table import Table
- class OutsideTable(Table):
- def __init__(self, table_number: int, capacity: int):
- super().__init__(table_number, capacity)
- @property
- def table_number(self):
- return self.__table_number
- @table_number.setter
- def table_number(self, value):
- if not 51 <= value <= 100:
- raise ValueError("Outside table's number must be between 51 and 100 inclusive!")
- self.__table_number = value
- def reserve(self, number_of_people):
- if self.capacity >= number_of_people:
- self.number_of_people = number_of_people
- self.is_reserved = True
- def order_food(self, baked_food: BakedFood):
- self.food_orders.append(baked_food)
- def order_drink(self, drink: Drink):
- self.drink_orders.append(drink)
- def get_bill(self):
- return sum([d.price for d in self.drink_orders]) + sum([f.price for f in self.food_orders])
- def clear(self):
- self.drink_orders = []
- self.food_orders = []
- self.number_of_people = 0
- self.is_reserved = False
- def free_table_info(self):
- if not self.is_reserved:
- return f"Table: {self.table_number}" + \
- f'\nType: {self.__class__.__name__}' \
- + f"\nCapacity: {self.capacity}"
- =============================================================================================
- # file name: bakery.py
- from project.baked_food.bread import Bread
- from project.baked_food.cake import Cake
- from project.drink.tea import Tea
- from project.drink.water import Water
- from project.table.inside_table import InsideTable
- from project.table.outside_table import OutsideTable
- class Bakery:
- def __init__(self, name):
- self.name = name
- self.food_menu = []
- self.drinks_menu = []
- self.tables_repository = []
- self.total_income = 0
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, value):
- if value.strip() == "":
- raise ValueError("Name cannot be empty string or white space!")
- self.__name = value
- def add_food(self, food_type: str, name: str, price: float):
- food_mapper = {'Bread': Bread, 'Cake': Cake}
- if food_type in food_mapper.keys():
- if name in [b.name for b in self.food_menu]:
- raise Exception(f"{food_type} {name} is already in the menu!")
- else:
- new_food = food_mapper[food_type](name, price)
- self.food_menu.append(new_food)
- return f"Added {name} ({food_type}) to the food menu"
- def add_drink(self, drink_type: str, name: str, portion: float, brand: str):
- drink_mapper = {'Water': Water, "Tea": Tea}
- if drink_type in drink_mapper.keys():
- if name in [d.name for d in self.drinks_menu]:
- raise Exception(f"{drink_type} {name} is already in the menu!")
- else:
- new_drink = drink_mapper[drink_type](name, portion, brand)
- self.drinks_menu.append(new_drink)
- return f"Added {name} ({brand}) to the drink menu"
- def add_table(self, table_type: str, table_number: int, capacity: int):
- table_mapper = {"InsideTable": InsideTable, "OutsideTable": OutsideTable}
- if table_type in table_mapper.keys():
- if table_number in [t.table_number for t in self.tables_repository]:
- raise Exception(f"Table {table_number} is already in the bakery!")
- else:
- new_table = table_mapper[table_type](table_number, capacity)
- self.tables_repository.append(new_table)
- return f'Added table number {table_number} in the bakery'
- def reserve_table(self, number_of_people: int):
- all_tables = [t for t in self.tables_repository if t.capacity >= number_of_people and not t.is_reserved]
- if all_tables:
- free_table = all_tables[0]
- free_table.is_reserved = True
- free_table.number_of_people += number_of_people
- return f'Table {free_table.table_number} has been reserved for {number_of_people} people'
- else:
- return f'No available table for {number_of_people} people'
- def order_food(self, table_number: int, *foods_names):
- searched_table = None
- for table_obj in self.tables_repository:
- if table_obj.table_number == table_number:
- searched_table = table_obj
- if not searched_table:
- return f"Could not find table {table_number}"
- else:
- ordered = f'Table {table_number} ordered:\n'
- not_ordered = f'{self.name} does not have in the menu:\n'
- for food_name in foods_names:
- if food_name in [f.name for f in self.food_menu]:
- for food_obj in self.food_menu:
- if food_obj.name == food_name:
- ordered += f'{repr(food_obj)}\n'
- searched_table.food_orders.append(food_obj)
- self.total_income += food_obj.price
- else:
- not_ordered += f"{food_name}\n"
- return ordered + not_ordered.rstrip()
- def order_drink(self, table_number: int, *drinks_name):
- searched_table = None
- for table_obj in self.tables_repository:
- if table_obj.table_number == table_number:
- searched_table = table_obj
- if not searched_table:
- return f"Could not find table {table_number}"
- else:
- ordered = f'Table {table_number} ordered:\n'
- not_ordered = f'{self.name} does not have in the menu:\n'
- for drink_name in drinks_name:
- if drink_name in [f.name for f in self.drinks_menu]:
- for drink_obj in self.drinks_menu:
- if drink_obj.name == drink_name:
- ordered += f'{str(drink_obj)}\n'
- searched_table.drink_orders.append(drink_obj)
- self.total_income += drink_obj.price
- else:
- not_ordered += f"{drink_name}\n"
- return ordered + not_ordered.rstrip()
- def leave_table(self, table_number: int):
- table = [t for t in self.tables_repository if t.table_number == table_number][0]
- foods_bill = sum([f.price for f in table.food_orders])
- drinks_bill = sum([f.price for f in table.drink_orders])
- total_bill = foods_bill + drinks_bill
- table.clear()
- return f'Table: {table_number}\nBill: {total_bill:.2f}'
- def get_free_tables_info(self):
- result = ""
- free_tables = [t for t in self.tables_repository if not t.is_reserved]
- for table_obj in free_tables:
- result += f"{table_obj.free_table_info()}\n"
- return result.rstrip()
- def get_total_income(self):
- return f'Total income: {self.total_income:.2f}lv'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement