Skip to content

Commit f1a475f

Browse files
committed
Added Exam Preparation/Python OOP Exam - 10 December 2022
1 parent 7ac1420 commit f1a475f

File tree

15 files changed

+276
-0
lines changed

15 files changed

+276
-0
lines changed

OOP/C.Exam Preparation/Python OOP Exam - 10 December 2022/Structure and Functionality/project/booths/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Booth(ABC):
5+
6+
def __init__(self, booth_number: int, capacity: int):
7+
self.booth_number = booth_number
8+
self.capacity = capacity
9+
self.delicacy_orders = [] # objects
10+
self.price_for_reservation = 0
11+
self.is_reserved = False
12+
13+
@property
14+
def capacity(self):
15+
return self.__capacity
16+
17+
@capacity.setter
18+
def capacity(self, value):
19+
if value < 0:
20+
raise ValueError("Capacity cannot be a negative number!")
21+
22+
self.__capacity = value
23+
24+
@abstractmethod
25+
def reserve(self, number_of_people: int):
26+
pass
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from project.booths.booth import Booth
2+
3+
4+
class OpenBooth(Booth):
5+
price_per_person = 2.50
6+
7+
def reserve(self, number_of_people: int):
8+
price = OpenBooth.price_per_person * number_of_people
9+
self.price_for_reservation = price
10+
self.is_reserved = True
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from project.booths.booth import Booth
2+
3+
4+
class PrivateBooth(Booth):
5+
price_per_person = 3.50
6+
7+
def reserve(self, number_of_people: int):
8+
price = PrivateBooth.price_per_person * number_of_people
9+
self.price_for_reservation = price
10+
self.is_reserved = True
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from project.delicacies.gingerbread import Gingerbread
2+
from project.delicacies.stolen import Stolen
3+
from project.booths.open_booth import OpenBooth
4+
from project.booths.private_booth import PrivateBooth
5+
6+
7+
class ChristmasPastryShopApp:
8+
9+
def __init__(self):
10+
self.booths = [] # objects
11+
self.delicacies = [] # objects
12+
self.income = 0.0
13+
14+
def add_delicacy(self, type_delicacy: str, name: str, price: float):
15+
if name in [x.name for x in self.delicacies]:
16+
raise Exception(f"{name} already exists!")
17+
18+
if type_delicacy not in ["Gingerbread", "Stolen"]:
19+
raise Exception(f"{type_delicacy} is not on our delicacy menu!")
20+
21+
if type_delicacy == "Gingerbread":
22+
delicacy = Gingerbread(name, price)
23+
else:
24+
delicacy = Stolen(name, price)
25+
26+
self.delicacies.append(delicacy)
27+
28+
return f"Added delicacy {name} - {type_delicacy} to the pastry shop."
29+
30+
def add_booth(self, type_booth: str, booth_number: int, capacity: int):
31+
if booth_number in [x.booth_number for x in self.booths]:
32+
raise Exception(f"Booth number {booth_number} already exists!")
33+
34+
if type_booth not in ["Open Booth", "Private Booth"]:
35+
raise Exception(f"{type_booth} is not a valid booth!")
36+
37+
if type_booth == "Open Booth":
38+
booth = OpenBooth(booth_number, capacity)
39+
else:
40+
booth = PrivateBooth(booth_number, capacity)
41+
42+
self.booths.append(booth)
43+
44+
return f"Added booth number {booth_number} in the pastry shop."
45+
46+
def reserve_booth(self, number_of_people: int):
47+
booth = [x for x in self.booths if x.capacity >= number_of_people and not x.is_reserved]
48+
if not booth:
49+
raise Exception(f"No available booth for {number_of_people} people!")
50+
51+
booth = booth[0]
52+
booth.reserve(number_of_people)
53+
54+
return f"Booth {booth.booth_number} has been reserved for {number_of_people} people."
55+
56+
def order_delicacy(self, booth_number: int, delicacy_name: str):
57+
if booth_number not in [x.booth_number for x in self.booths]:
58+
raise Exception(f"Could not find booth {booth_number}!")
59+
60+
if delicacy_name not in [x.name for x in self.delicacies]:
61+
raise Exception(f"No {delicacy_name} in the pastry shop!")
62+
63+
booth = [x for x in self.booths if x.booth_number == booth_number][0]
64+
delicacy = [x for x in self.delicacies if x.name == delicacy_name][0]
65+
booth.delicacy_orders.append(delicacy)
66+
67+
return f"Booth {booth_number} ordered {delicacy_name}."
68+
69+
def leave_booth(self, booth_number: int):
70+
booth = [x for x in self.booths if x.booth_number == booth_number][0]
71+
bill = booth.price_for_reservation + sum([x.price for x in booth.delicacy_orders])
72+
self.income += bill
73+
74+
booth.delicacy_orders = []
75+
booth.is_reserved = False
76+
booth.price_for_reservation = 0
77+
78+
return f"Booth {booth_number}:\nBill: {bill:.2f}lv."
79+
80+
def get_income(self):
81+
return f"Income: {self.income:.2f}lv."
82+

OOP/C.Exam Preparation/Python OOP Exam - 10 December 2022/Structure and Functionality/project/delicacies/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Delicacy(ABC):
5+
6+
def __init__(self, name: str, portion: int, price: float):
7+
self.name = name
8+
self.portion = portion
9+
self.price = price
10+
11+
@property
12+
def name(self):
13+
return self.__name
14+
15+
@name.setter
16+
def name(self, value):
17+
if value.rstrip() == "":
18+
raise ValueError("Name cannot be null or whitespace!")
19+
20+
self.__name = value
21+
22+
@property
23+
def price(self):
24+
return self.__price
25+
26+
@price.setter
27+
def price(self, value):
28+
if value <= 0:
29+
raise ValueError("Price cannot be less or equal to zero!")
30+
31+
self.__price = value
32+
33+
@abstractmethod
34+
def details(self):
35+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from project.delicacies.delicacy import Delicacy
2+
3+
4+
class Gingerbread(Delicacy):
5+
portion = 200
6+
7+
def __init__(self, name: str, price: float):
8+
super().__init__(name, Gingerbread.portion, price)
9+
10+
def details(self):
11+
return f"Gingerbread {self.name}: 200g - {self.price:.2f}lv."
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from project.delicacies.delicacy import Delicacy
2+
3+
4+
class Stolen(Delicacy):
5+
portion = 250
6+
7+
def __init__(self, name: str, price: float):
8+
super().__init__(name, Stolen.portion, price)
9+
10+
def details(self):
11+
return f"Stolen {self.name}: 250g - {self.price:.2f}lv."
12+
Binary file not shown.

OOP/C.Exam Preparation/Python OOP Exam - 10 December 2022/Unit Testing/test/__init__.py

Whitespace-only changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from unittest import TestCase, main
2+
from project.toy_store import ToyStore
3+
4+
5+
class TestToyStore(TestCase):
6+
7+
def setUp(self) -> None:
8+
self.toy_store = ToyStore()
9+
10+
def test_successful_initialization(self):
11+
shelf = {
12+
"A": None,
13+
"B": None,
14+
"C": None,
15+
"D": None,
16+
"E": None,
17+
"F": None,
18+
"G": None,
19+
}
20+
self.assertEqual(shelf, self.toy_store.toy_shelf)
21+
22+
def test_add_toy_invalid_shelf(self):
23+
with self.assertRaises(Exception) as ex:
24+
self.toy_store.add_toy("7", "Mark")
25+
26+
self.assertEqual("Shelf doesn't exist!", str(ex.exception))
27+
28+
def test_add_toy_already_in_shelf(self):
29+
self.toy_store.toy_shelf = {"A": "Mark"}
30+
31+
with self.assertRaises(Exception) as ex:
32+
self.toy_store.add_toy("A", "Mark")
33+
34+
self.assertEqual("Toy is already in shelf!", str(ex.exception))
35+
36+
def test_add_toy_shelf_already_taken(self):
37+
self.toy_store.toy_shelf["A"] = "Mark"
38+
39+
with self.assertRaises(Exception) as ex:
40+
self.toy_store.add_toy("A", "John")
41+
42+
self.assertEqual("Shelf is already taken!", str(ex.exception))
43+
44+
def test_successful_add_toy(self):
45+
result = self.toy_store.add_toy("A", "Mark")
46+
shelf = {
47+
"A": "Mark",
48+
"B": None,
49+
"C": None,
50+
"D": None,
51+
"E": None,
52+
"F": None,
53+
"G": None,
54+
}
55+
self.assertEqual(shelf, self.toy_store.toy_shelf)
56+
self.assertEqual(f"Toy:Mark placed successfully!", result)
57+
58+
def test_remove_toy_shelf_not_in_shelf_keys(self):
59+
with self.assertRaises(Exception) as ex:
60+
self.toy_store.remove_toy("7", "Mark")
61+
62+
self.assertEqual("Shelf doesn't exist!", str(ex.exception))
63+
64+
def test_remove_toy_name_not_in_shelf(self):
65+
with self.assertRaises(Exception) as ex:
66+
self.toy_store.remove_toy("A", "Mark")
67+
68+
self.assertEqual("Toy in that shelf doesn't exists!", str(ex.exception))
69+
70+
def test_successful_remove_toy(self):
71+
self.toy_store.toy_shelf = {
72+
"A": "Mark",
73+
"B": None,
74+
"C": None,
75+
"D": None,
76+
"E": None,
77+
"F": None,
78+
"G": None,
79+
}
80+
result = self.toy_store.remove_toy("A", "Mark")
81+
self.assertEqual(None, self.toy_store.toy_shelf["A"])
82+
self.assertEqual(f"Remove toy:Mark successfully!", result)
83+
84+
85+
if __name__ == '__main__':
86+
main()

0 commit comments

Comments
 (0)