-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupermarket.py
More file actions
78 lines (65 loc) · 2.23 KB
/
Copy pathsupermarket.py
File metadata and controls
78 lines (65 loc) · 2.23 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
68
69
70
71
72
73
74
75
76
77
78
# Supermarket Billing System in Python
# Define the Product class
class Product:
def __init__(self, id, name, price):
self.id = id
self.name = name
self.price = price
# Define the User class
class User:
def __init__(self, id, name):
self.id = id
self.name = name
# Define the Order class
class Order:
def __init__(self, user, product, quantity):
self.user = user
self.product = product
self.quantity = quantity
# Define the Supermarket class
class Supermarket:
def __init__(self):
self.users = []
self.products = []
self.orders = []
def add_user(self, id, name):
user = User(id, name)
self.users.append(user)
def add_product(self, id, name, price):
product = Product(id, name, price)
self.products.append(product)
def add_order(self, user_id, product_id, quantity):
user = None
product = None
for u in self.users:
if u.id == user_id:
user = u
break
for p in self.products:
if p.id == product_id:
product = p
break
if user is not None and product is not None:
order = Order(user, product, quantity)
self.orders.append(order)
def view_orders(self):
total_amount = 0
for order in self.orders:
print(f"{order.user.name} ordered {order.quantity} {order.product.name}(s) for {order.product.price * order.quantity} dollars.")
total_amount += order.product.price * order.quantity
print(f"Total Amount: {total_amount} dollars.")
# Create an instance of the Supermarket class
supermarket = Supermarket()
# Add users to the supermarket
supermarket.add_user(1, "John")
supermarket.add_user(2, "Jane")
# Add products to the supermarket
supermarket.add_product(1, "Apple", 0.5)
supermarket.add_product(2, "Banana", 0.25)
supermarket.add_product(3, "Orange", 0.75)
# Add orders to the supermarket
supermarket.add_order(1, 1, 2)
supermarket.add_order(2, 2, 5)
supermarket.add_order(1, 3, 1)
# View the orders in the supermarket
supermarket.view_orders()