-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.py
68 lines (67 loc) · 2.09 KB
/
store.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
class StoreKeeper:
def __init__(self, name = 'storekeeper'):
self._store = {}
self.add('name', name)
def add(self, key, value):
self._store[key] = value
def get(self, key):
return self._store.get(key)
def remove(self, key):
del self._store[key]
def clear(self):
self._store.clear()
#Sets name and prices of each item
def getLook(self):
return "B"
def setName(self, name):
self.add('name', name)
def addItemPrice(self, item, price):
self.add(item, price)
#Gets name and prices of each item
def getItemPrice(self, item):
return self.get(item)
def getName(self):
return self.get('name')
#Gets all items minus name
def getItems(self):
items = list(self._store.keys())
items.remove('name')
return items
def buy(self, item):
if item in self.getItems():
print('You bought ', item.capitalize(), ' for $ ', self.getItemPrice(item), sep = '')
else:
print('Sorry, we do not have', item)
#Gets all prices minus name
def getPrices(self):
prices = list(self._store.values())
prices.remove(self.get('name'))
return prices
#Gets all items and prices minus name
def getItemsAndPrices(self):
items = self.getItems()
prices = self.getPrices()
itemsAndPrices = {}
for i in range(len(items)):
itemsAndPrices[items[i]] = prices[i]
return itemsAndPrices
#prints out prices nicely
def printPrices(self):
itemsAndPrices = self.getItemsAndPrices()
for item in itemsAndPrices:
items = item.capitalize()
print(items, ': $', itemsAndPrices[item], sep = '')
def printSeller(self):
print('Seller:', self.getName())
bob = StoreKeeper()
bob.setName('Bob')
bob.addItemPrice('apple', 1.00)
bob.addItemPrice('banana', 0.50)
bob.addItemPrice('orange', 1.50)
bob.buy('apple')
# print(bob)
# print(bob.getItems())
# print(bob.getPrices())
# print(bob.getItemsAndPrices())
bob.printSeller()
bob.printPrices()