-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcommerce.py
More file actions
67 lines (53 loc) · 1.68 KB
/
Ecommerce.py
File metadata and controls
67 lines (53 loc) · 1.68 KB
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
class Customer:
def __init__(self, name, email):
self.name = name
self.email = email
self.purchases = []
def purchase(self, inventory, product):
inventory_dict = inventory.inventory
if product in inventory_dict:
if inventory_dict[product] > 0:
self.purchases.append(product)
inventory_dict[product] -= 1
else:
print('We are out of stock!')
else:
print("We don't have that product!")
def print_purchases(self):
print("The customer has purchased")
for item in self.purchases:
print(item.name)
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
class Inventory:
def __init__(self):
self.inventory = {}
def add_product(self, product, quantity):
if product not in self.inventory:
self.inventory[product] = quantity
else:
self.inventory[product] += quantity
def print_inventory(self):
for key, value in self.inventory.items():
print(key.name + ':' + str(value))
print()
customer = Customer('Joe', 'joe@gmail.com')
#print(customer.name)
#print(customer.email)
apple_watch = Product('Apple Watch', 299)
#print(apple_watch.name)
#print(apple_watch.price)
mac = Product('Mac', 1999)
#print(mac.name)
#print(mac.price)
inventory = Inventory()
inventory.add_product(apple_watch, 100)
#inventory.print_inventory()
inventory.add_product(mac, 498)
inventory.print_inventory()
customer.purchase(inventory, apple_watch)
customer.purchase(inventory, mac)
inventory.print_inventory()
customer.print_purchases()