Advertisement
GeorgiLukanov87

Python OOP Exam - 10 December 2022

Dec 12th, 2022
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.09 KB | None | 0 0
  1. # Python OOP Exam - 10 December 2022
  2.  
  3. ==========================================================================================
  4. ==========================================================================================
  5. # booth.py
  6.  
  7. from abc import ABC, abstractmethod
  8.  
  9.  
  10. class Booth(ABC):
  11.     def __init__(self, booth_number: int, capacity: int):
  12.         self.booth_number = booth_number
  13.         self.capacity = capacity
  14.  
  15.         self.delicacy_orders = []
  16.         self.price_for_reservation = 0
  17.         self.is_reserved = False
  18.  
  19.     @property
  20.     def capacity(self):
  21.         return self.__capacity
  22.  
  23.     @capacity.setter
  24.     def capacity(self, value):
  25.         if value < 0:
  26.             raise ValueError("Capacity cannot be a negative number!")
  27.         self.__capacity = value
  28.  
  29.     @abstractmethod
  30.     def reserve(self, number_of_people: int):
  31.         ...
  32.  
  33. ==========================================================================================
  34.  
  35. # open_booth.py
  36.  
  37. from project.booths.booth import Booth
  38.  
  39.  
  40. class OpenBooth(Booth):
  41.     PRICE_PER_PERSON = 2.50
  42.  
  43.     def __init__(self, booth_number: int, capacity: int):
  44.         super().__init__(booth_number, capacity)
  45.  
  46.     def reserve(self, number_of_people: int):
  47.         self.price_for_reservation = self.PRICE_PER_PERSON * number_of_people
  48.         self.is_reserved = True
  49.  
  50. ==========================================================================================
  51.  
  52. # private_booth.py
  53.  
  54. from project.booths.booth import Booth
  55.  
  56.  
  57. class PrivateBooth(Booth):
  58.     PRICE_PER_PERSON = 3.50
  59.  
  60.     def __init__(self, booth_number: int, capacity: int):
  61.         super().__init__(booth_number, capacity)
  62.  
  63.     def reserve(self, number_of_people: int):
  64.         self.price_for_reservation = self.PRICE_PER_PERSON * number_of_people
  65.         self.is_reserved = True
  66.  
  67. ==========================================================================================
  68.  
  69. # delicacy.py
  70.  
  71. from abc import ABC, abstractmethod
  72.  
  73.  
  74. class Delicacy(ABC):
  75.     def __init__(self, name: str, portion: int, price: float):
  76.         self.name = name
  77.         self.portion = portion  # Grams
  78.         self.price = price
  79.  
  80.     @property
  81.     def name(self):
  82.         return self.__name
  83.  
  84.     @name.setter
  85.     def name(self, value):
  86.         if value.strip() == '':
  87.             raise ValueError("Name cannot be null or whitespace!")
  88.         self.__name = value
  89.  
  90.     @property
  91.     def price(self):
  92.         return self.__price
  93.  
  94.     @price.setter
  95.     def price(self, value):
  96.         if value <= 0:
  97.             raise ValueError("Price cannot be less or equal to zero!")
  98.         self.__price = value
  99.  
  100.     @abstractmethod
  101.     def details(self):
  102.         ...
  103.  
  104. ==========================================================================================
  105.  
  106. # gingerbread.py
  107.  
  108. from project.delicacies.delicacy import Delicacy
  109.  
  110.  
  111. class Gingerbread(Delicacy):
  112.     INITIAL_GRAMS = 200
  113.  
  114.     def __init__(self, name: str, price: float):
  115.         super().__init__(name, self.INITIAL_GRAMS, price)
  116.  
  117.     def details(self):
  118.         return f"Gingerbread {self.name}: 200g - {self.price:.2f}lv."
  119.  
  120. ==========================================================================================
  121.  
  122. # stolen.py
  123.  
  124. from project.delicacies.delicacy import Delicacy
  125.  
  126.  
  127. class Stolen(Delicacy):
  128.     INITIAL_GRAMS = 250
  129.  
  130.     def __init__(self, name: str, price: float):
  131.         super().__init__(name, self.INITIAL_GRAMS, price)
  132.  
  133.     def details(self):
  134.         return f"Stolen {self.name}: 250g - {self.price:.2f}lv."
  135.  
  136. ==========================================================================================
  137.  
  138. # christmas_pastry_shop_app.py
  139.  
  140. from project.booths.open_booth import OpenBooth
  141. from project.booths.private_booth import PrivateBooth
  142. from project.delicacies.gingerbread import Gingerbread
  143. from project.delicacies.stolen import Stolen
  144.  
  145.  
  146. class ChristmasPastryShopApp:
  147.     def __init__(self):
  148.         self.booths = []  # objs
  149.         self.delicacies = []  # objs
  150.         self.income = 0  # Total income of pastry shop!
  151.  
  152.         self.valid_delicacies_types = {"Gingerbread": Gingerbread, "Stolen": Stolen}
  153.         self.valid_booth_types = {"Open Booth": OpenBooth, "Private Booth": PrivateBooth}
  154.  
  155.     def add_delicacy(self, type_delicacy: str, name: str, price: float):
  156.         if name in [d.name for d in self.delicacies]:
  157.             raise Exception(f"{name} already exists!")
  158.         if type_delicacy not in self.valid_delicacies_types:
  159.             raise Exception(f"{type_delicacy} is not on our delicacy menu!")
  160.  
  161.         new_delicacy = self.valid_delicacies_types[type_delicacy](name, price)
  162.         self.delicacies.append(new_delicacy)
  163.         return f'Added delicacy {name} - {type_delicacy} to the pastry shop.'
  164.  
  165.     def add_booth(self, type_booth: str, booth_number: int, capacity: int):
  166.         if booth_number in [b.booth_number for b in self.booths]:
  167.             raise Exception(f"Booth number {booth_number} already exists!")
  168.         if type_booth not in self.valid_booth_types:
  169.             raise Exception(f"{type_booth} is not a valid booth!")
  170.         new_booth = self.valid_booth_types[type_booth](booth_number, capacity)
  171.         self.booths.append(new_booth)
  172.         return f'Added booth number {booth_number} in the pastry shop.'
  173.  
  174.     def reserve_booth(self, number_of_people: int):
  175.         booth = self.__find_booth_for_reserve(number_of_people)
  176.         booth.reserve(number_of_people)
  177.         return f"Booth {booth.booth_number} has been reserved for {number_of_people} people."
  178.  
  179.     def __find_booth_for_reserve(self, number_of_people_):
  180.         searched_booth = [b for b in self.booths if b.capacity >= number_of_people_
  181.                           and not b.is_reserved]
  182.         if not searched_booth:
  183.             raise Exception(f'No available booth for {number_of_people_} people!')
  184.         return searched_booth[0]
  185.  
  186.     def order_delicacy(self, booth_number: int, delicacy_name: str):
  187.         booth = self.__find_booth_by_number(booth_number)
  188.         delicacy = self.__find_delicacy_by_name(delicacy_name)
  189.         booth.delicacy_orders.append(delicacy)
  190.         return f"Booth {booth_number} ordered {delicacy_name}."
  191.  
  192.     def __find_delicacy_by_name(self, delicacy_name_):
  193.         for delicacy_obj in self.delicacies:
  194.             if delicacy_obj.name == delicacy_name_:
  195.                 return delicacy_obj
  196.         raise Exception(f"No {delicacy_name_} in the pastry shop!")
  197.  
  198.     def __find_booth_by_number(self, booth_number_):
  199.         for booth_obj in self.booths:
  200.             if booth_obj.booth_number == booth_number_:
  201.                 return booth_obj
  202.         raise Exception(f"Could not find booth {booth_number_}!")
  203.  
  204.     def leave_booth(self, booth_number: int):
  205.         booth = self.__find_booth_by_number(booth_number)
  206.         delicacies_sum = self.__calculate_delicacies_income_for_booth(booth)
  207.         self.income += delicacies_sum
  208.         self.income += booth.price_for_reservation
  209.         final_sum = delicacies_sum + booth.price_for_reservation
  210.  
  211.         booth.price_for_reservation = 0
  212.         booth.is_reserved = False
  213.         booth.delicacy_orders = []
  214.  
  215.         return f"Booth {booth_number}:\nBill: {final_sum:.2f}lv."
  216.  
  217.     @staticmethod
  218.     def __calculate_delicacies_income_for_booth(booth):
  219.         total_sum = 0
  220.         for delicacy_obj in booth.delicacy_orders:
  221.             total_sum += delicacy_obj.price
  222.         return total_sum
  223.  
  224.     def get_income(self):
  225.         return f"Income: {self.income:.2f}lv."
  226.  
  227.  
  228. ==========================================================================================
  229. ==========================================================================================
  230.  
  231. # Python OOP Exam - 10 December 2022 - UNIT TESTING !
  232.  
  233. from project.toy_store import ToyStore
  234.  
  235. from unittest import TestCase, main
  236.  
  237.  
  238. class ToyStoreTests(TestCase):
  239.     def setUp(self) -> None:
  240.         self.store = ToyStore()
  241.  
  242.     def test_init(self):
  243.         result = {
  244.             "A": None,
  245.             "B": None,
  246.             "C": None,
  247.             "D": None,
  248.             "E": None,
  249.             "F": None,
  250.             "G": None,
  251.         }
  252.         self.assertEqual(result, self.store.toy_shelf)
  253.  
  254.     def test_add_toy_raises_if_shelf_not_exist(self):
  255.         with self.assertRaises(Exception) as ex:
  256.             self.store.add_toy('Z', 'toy1')
  257.         self.assertEqual("Shelf doesn't exist!", str(ex.exception))
  258.  
  259.     def test_add_toy_raises_if_shelf_name_is_the_same_like_the_toy_name(self):
  260.         self.store.add_toy('B', 'toy1')
  261.         with self.assertRaises(Exception) as ex:
  262.             self.store.add_toy('B', 'toy1')
  263.         self.assertEqual("Toy is already in shelf!", str(ex.exception))
  264.  
  265.     def test_add_toy_raises_if_shelf_is_not_none(self):
  266.         self.store.add_toy('A', 'toy1')
  267.         with self.assertRaises(Exception) as ex:
  268.             self.store.add_toy('A', 'toy2')
  269.         self.assertEqual("Shelf is already taken!", str(ex.exception))
  270.  
  271.     def test_add_toy_successfully(self):
  272.         self.assertEqual(f"Toy:toy1 placed successfully!", self.store.add_toy('A', 'toy1'))
  273.         self.assertEqual(True, 'toy1' in self.store.toy_shelf['A'])
  274.  
  275.         self.assertEqual(f"Toy:toy2 placed successfully!", self.store.add_toy('B', 'toy2'))
  276.         self.assertEqual(True, 'toy2' in self.store.toy_shelf['B'])
  277.  
  278.     def test_remove_toy_raises_if_shelf_not_exist(self):
  279.         with self.assertRaises(Exception) as ex:
  280.             self.store.remove_toy('Z', 'toy1')
  281.         self.assertEqual("Shelf doesn't exist!", str(ex.exception))
  282.  
  283.     def test_remove_toy_raises_if_toy_in_that_shelf_not_exist(self):
  284.         self.store.add_toy('A', 'toy1')
  285.         with self.assertRaises(Exception) as ex:
  286.             self.store.remove_toy('A', 'toy2')
  287.         self.assertEqual("Toy in that shelf doesn't exists!", str(ex.exception))
  288.  
  289.     def test_remove_toy_successfully(self):
  290.         self.store.add_toy('A', 'toy1')
  291.         self.store.add_toy('B', 'toy2')
  292.  
  293.         self.assertEqual(f"Remove toy:toy1 successfully!", self.store.remove_toy('A', 'toy1'))
  294.         self.assertEqual(None, self.store.toy_shelf['A'])
  295.  
  296.  
  297. if __name__ == '__main__':
  298.     main()
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement