-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge3-1.py
108 lines (90 loc) · 3.28 KB
/
challenge3-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# A simple 2 class implementation for an online shopping
# application - this time raise and catching exceptions
# Define an exception for ItemNotPresentError ...
# Define an exception for InvalidDiscountRateError ...
class Customer:
"""Represents a customer for an online shop"""
def __init__(self, name, email):
self.name = name
self.email = email
self.basket = []
self.cost = 0.0
def buyItem(self, item):
self.basket.append(item)
self.cost += item.price
def removeItem(self, item):
"""Removes an item from the basket
Raises ItemNotPresentError if not present"""
try:
if item not in self.basket:
raise ItemNotPresentError('Not in the basket')
else:
self.cost -= item.price
self.basket.remove(item)
except:
pass
def removeItemByName(self, name):
"""Removes an item from the basket
Raises ItemNotPresentError if not present"""
try:
for i in self.basket:
if i.name == name:
self.cost -= i.price
self.basket.remove(i)
return
# If we get here, the named item was not in the basket
raise ItemNotPresentError('Not in the basket')
except:
pass
def showCost(self):
"""Returns the total cost of the basket"""
print(f'Total cost is £{self.cost:.2f}')
return self.cost
# Rewrite this code so that it raises an InvalidDiscountRateError
# if the discount rate is not in the stated range ...
def discountBasket(self, rate):
"""Make a discount of rate %
Returns True is rate is between 10 ro 50% inclusive, False otherwise"""
try:
if not isinstance(rate,(int,float)):
raise InvalidDiscountBasketError('Not Number')
if rate >= 10.0 and rate <= 50.0:
for i in self.basket:
disc = i.price*(rate/100)
i.price = i.price - disc
self.cost -= disc
return InvalidDiscountBasketError('Invalid Rate')
except InvalidDiscountBasketError:
pass
class ItemNotPresentError(Exception):
def __init__(self,message):
print(message)
class InvalidDiscountBasketError(Exception):
def __init__(self, message):
print(message)
class Item:
"""Represents something we can buy in an online shop"""
def __init__(self,name, price):
self.name = name
self.price = price
def changeName(self, newName):
self.name = newName
def main():
c1 = Customer('bob', '[email protected]')
c2 = Customer('alice', '[email protected]')
c3 = Customer('jimmy', '[email protected]')
i1 = Item('small table', 50.0)
i2 = Item('table lamp', 30.0)
i3 = Item('rug', 100.0)
c1.buyItem(i1)
c1.buyItem(i2)
c2.buyItem(i1)
c2.buyItem(i3)
c3.buyItem(i3)
# Try to remove items that are not present
c1.removeItemByName('pot')
c2.removeItem(i2)
c3.discountBasket(100)
c2.discountBasket('333')
if __name__ == '__main__':
main()