-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.py
71 lines (58 loc) · 2.18 KB
/
cart.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
from inventory import inventory
from item import item
class cart:
#the main thing here is that I wanted the shopping cart to keep track of the items in the cart by appending to a list of item objects so first we need to import the item class I drafted up
def __init__(self):
self.total = 0
self.items = []
self.inv = inventory()
def getTotal(self):
return self.total
#this should append the added item to the list
# I was thinking about returning the index of the item added for use with the remove function but was undecided at the time
def addItem(self, item):
#make sure stock isn't empty
if (self.inv.get_stock(item.get_name()) <= 0):
return False
else:
#add item to items array
self.items.append(item)
#update total
self.total += item.get_price(item.get_name())
#update inventory
self.inv.rm_stock(item.get_name())
return True
#this should remove the iem from the lsit
def rmItem(self, name):
#rm item from items array
if len(self.items):
for i in self.items:
if (name.lower() == i.get_name().lower()):
#update total
self.total -= i.get_price(i.get_name())
#remove item
self.items.remove(i)
break
#if item not in cart or they mispelled item trigger warning
else:
print()
print()
print("Item not in cart or check spelling.")
def showCart(self):
print("---Items currently in your cart---")
if not self.items:
print("Cart is empty")
else:
for i in self.items:
print(i.get_name() + " $" + str(i.get_price(i.get_name())))
print("Total is $" + str(self.total))
print()
def emptyCart(self):
for i in self.items:
self.items.pop()
self.total = 0
def getItems(self):
tmp = []
for i in self.items:
tmp.append(i.get_name())
return tmp