Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python OOP Exam - 10 December 2022
- ==========================================================================================
- ==========================================================================================
- # booth.py
- from abc import ABC, abstractmethod
- class Booth(ABC):
- def __init__(self, booth_number: int, capacity: int):
- self.booth_number = booth_number
- self.capacity = capacity
- self.delicacy_orders = []
- self.price_for_reservation = 0
- self.is_reserved = False
- @property
- def capacity(self):
- return self.__capacity
- @capacity.setter
- def capacity(self, value):
- if value < 0:
- raise ValueError("Capacity cannot be a negative number!")
- self.__capacity = value
- @abstractmethod
- def reserve(self, number_of_people: int):
- ...
- ==========================================================================================
- # open_booth.py
- from project.booths.booth import Booth
- class OpenBooth(Booth):
- PRICE_PER_PERSON = 2.50
- def __init__(self, booth_number: int, capacity: int):
- super().__init__(booth_number, capacity)
- def reserve(self, number_of_people: int):
- self.price_for_reservation = self.PRICE_PER_PERSON * number_of_people
- self.is_reserved = True
- ==========================================================================================
- # private_booth.py
- from project.booths.booth import Booth
- class PrivateBooth(Booth):
- PRICE_PER_PERSON = 3.50
- def __init__(self, booth_number: int, capacity: int):
- super().__init__(booth_number, capacity)
- def reserve(self, number_of_people: int):
- self.price_for_reservation = self.PRICE_PER_PERSON * number_of_people
- self.is_reserved = True
- ==========================================================================================
- # delicacy.py
- from abc import ABC, abstractmethod
- class Delicacy(ABC):
- def __init__(self, name: str, portion: int, price: float):
- self.name = name
- self.portion = portion # Grams
- self.price = price
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, value):
- if value.strip() == '':
- raise ValueError("Name cannot be null or whitespace!")
- 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 or equal to zero!")
- self.__price = value
- @abstractmethod
- def details(self):
- ...
- ==========================================================================================
- # gingerbread.py
- from project.delicacies.delicacy import Delicacy
- class Gingerbread(Delicacy):
- INITIAL_GRAMS = 200
- def __init__(self, name: str, price: float):
- super().__init__(name, self.INITIAL_GRAMS, price)
- def details(self):
- return f"Gingerbread {self.name}: 200g - {self.price:.2f}lv."
- ==========================================================================================
- # stolen.py
- from project.delicacies.delicacy import Delicacy
- class Stolen(Delicacy):
- INITIAL_GRAMS = 250
- def __init__(self, name: str, price: float):
- super().__init__(name, self.INITIAL_GRAMS, price)
- def details(self):
- return f"Stolen {self.name}: 250g - {self.price:.2f}lv."
- ==========================================================================================
- # christmas_pastry_shop_app.py
- from project.booths.open_booth import OpenBooth
- from project.booths.private_booth import PrivateBooth
- from project.delicacies.gingerbread import Gingerbread
- from project.delicacies.stolen import Stolen
- class ChristmasPastryShopApp:
- def __init__(self):
- self.booths = [] # objs
- self.delicacies = [] # objs
- self.income = 0 # Total income of pastry shop!
- self.valid_delicacies_types = {"Gingerbread": Gingerbread, "Stolen": Stolen}
- self.valid_booth_types = {"Open Booth": OpenBooth, "Private Booth": PrivateBooth}
- def add_delicacy(self, type_delicacy: str, name: str, price: float):
- if name in [d.name for d in self.delicacies]:
- raise Exception(f"{name} already exists!")
- if type_delicacy not in self.valid_delicacies_types:
- raise Exception(f"{type_delicacy} is not on our delicacy menu!")
- new_delicacy = self.valid_delicacies_types[type_delicacy](name, price)
- self.delicacies.append(new_delicacy)
- return f'Added delicacy {name} - {type_delicacy} to the pastry shop.'
- def add_booth(self, type_booth: str, booth_number: int, capacity: int):
- if booth_number in [b.booth_number for b in self.booths]:
- raise Exception(f"Booth number {booth_number} already exists!")
- if type_booth not in self.valid_booth_types:
- raise Exception(f"{type_booth} is not a valid booth!")
- new_booth = self.valid_booth_types[type_booth](booth_number, capacity)
- self.booths.append(new_booth)
- return f'Added booth number {booth_number} in the pastry shop.'
- def reserve_booth(self, number_of_people: int):
- booth = self.__find_booth_for_reserve(number_of_people)
- booth.reserve(number_of_people)
- return f"Booth {booth.booth_number} has been reserved for {number_of_people} people."
- def __find_booth_for_reserve(self, number_of_people_):
- searched_booth = [b for b in self.booths if b.capacity >= number_of_people_
- and not b.is_reserved]
- if not searched_booth:
- raise Exception(f'No available booth for {number_of_people_} people!')
- return searched_booth[0]
- def order_delicacy(self, booth_number: int, delicacy_name: str):
- booth = self.__find_booth_by_number(booth_number)
- delicacy = self.__find_delicacy_by_name(delicacy_name)
- booth.delicacy_orders.append(delicacy)
- return f"Booth {booth_number} ordered {delicacy_name}."
- def __find_delicacy_by_name(self, delicacy_name_):
- for delicacy_obj in self.delicacies:
- if delicacy_obj.name == delicacy_name_:
- return delicacy_obj
- raise Exception(f"No {delicacy_name_} in the pastry shop!")
- def __find_booth_by_number(self, booth_number_):
- for booth_obj in self.booths:
- if booth_obj.booth_number == booth_number_:
- return booth_obj
- raise Exception(f"Could not find booth {booth_number_}!")
- def leave_booth(self, booth_number: int):
- booth = self.__find_booth_by_number(booth_number)
- delicacies_sum = self.__calculate_delicacies_income_for_booth(booth)
- self.income += delicacies_sum
- self.income += booth.price_for_reservation
- final_sum = delicacies_sum + booth.price_for_reservation
- booth.price_for_reservation = 0
- booth.is_reserved = False
- booth.delicacy_orders = []
- return f"Booth {booth_number}:\nBill: {final_sum:.2f}lv."
- @staticmethod
- def __calculate_delicacies_income_for_booth(booth):
- total_sum = 0
- for delicacy_obj in booth.delicacy_orders:
- total_sum += delicacy_obj.price
- return total_sum
- def get_income(self):
- return f"Income: {self.income:.2f}lv."
- ==========================================================================================
- ==========================================================================================
- # Python OOP Exam - 10 December 2022 - UNIT TESTING !
- from project.toy_store import ToyStore
- from unittest import TestCase, main
- class ToyStoreTests(TestCase):
- def setUp(self) -> None:
- self.store = ToyStore()
- def test_init(self):
- result = {
- "A": None,
- "B": None,
- "C": None,
- "D": None,
- "E": None,
- "F": None,
- "G": None,
- }
- self.assertEqual(result, self.store.toy_shelf)
- def test_add_toy_raises_if_shelf_not_exist(self):
- with self.assertRaises(Exception) as ex:
- self.store.add_toy('Z', 'toy1')
- self.assertEqual("Shelf doesn't exist!", str(ex.exception))
- def test_add_toy_raises_if_shelf_name_is_the_same_like_the_toy_name(self):
- self.store.add_toy('B', 'toy1')
- with self.assertRaises(Exception) as ex:
- self.store.add_toy('B', 'toy1')
- self.assertEqual("Toy is already in shelf!", str(ex.exception))
- def test_add_toy_raises_if_shelf_is_not_none(self):
- self.store.add_toy('A', 'toy1')
- with self.assertRaises(Exception) as ex:
- self.store.add_toy('A', 'toy2')
- self.assertEqual("Shelf is already taken!", str(ex.exception))
- def test_add_toy_successfully(self):
- self.assertEqual(f"Toy:toy1 placed successfully!", self.store.add_toy('A', 'toy1'))
- self.assertEqual(True, 'toy1' in self.store.toy_shelf['A'])
- self.assertEqual(f"Toy:toy2 placed successfully!", self.store.add_toy('B', 'toy2'))
- self.assertEqual(True, 'toy2' in self.store.toy_shelf['B'])
- def test_remove_toy_raises_if_shelf_not_exist(self):
- with self.assertRaises(Exception) as ex:
- self.store.remove_toy('Z', 'toy1')
- self.assertEqual("Shelf doesn't exist!", str(ex.exception))
- def test_remove_toy_raises_if_toy_in_that_shelf_not_exist(self):
- self.store.add_toy('A', 'toy1')
- with self.assertRaises(Exception) as ex:
- self.store.remove_toy('A', 'toy2')
- self.assertEqual("Toy in that shelf doesn't exists!", str(ex.exception))
- def test_remove_toy_successfully(self):
- self.store.add_toy('A', 'toy1')
- self.store.add_toy('B', 'toy2')
- self.assertEqual(f"Remove toy:toy1 successfully!", self.store.remove_toy('A', 'toy1'))
- self.assertEqual(None, self.store.toy_shelf['A'])
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement