Online Shopping System
Code-
import pickle
import os
# Sample product data
products = [
{"id": 1, "name": "Laptop", "price": 80000},
{"id": 2, "name": "Smartphone", "price": 50000},
{"id": 3, "name": "Headphones", "price": 10000},
{"id": 4, "name": "PS5", "price": 70000},
{"id": 5, "name": "AirPods", "price": 20000},
# Function to display products
def display_products():
print("\nAvailable Products:")
for product in products:
print("ID: {}, Name: {}, Price: ${}".format(product['id'], product['name'],
product['price']))
# Function to register a user
def register_user():
username = raw_input("Enter a username: ")
password = raw_input("Enter a password: ")
user_data = {"username": username, "password": password}
# Save user data to a file using pickle
with open("users.pkl", "ab") as file: # Append in binary mode
pickle.dump(user_data, file)
print("User registered successfully!")
# Function to authenticate a user
def authenticate_user():
username = raw_input("Enter your username: ")
password = raw_input("Enter your password: ")
if not os.path.exists("users.pkl"):
print("No users registered yet.")
return False
with open("users.pkl", "rb") as file: # Read in binary mode
while True:
try:
user_data = pickle.load(file)
if user_data["username"] == username and user_data["password"] ==
password:
print("Login successful!")
return True
except EOFError:
break # End of file reached
print("Invalid username or password.")
return False
# Function to place an order
def place_order():
cart = []
while True:
display_products()
product_id = raw_input("Enter the product ID to add to cart (0 to checkout):
")
# Validate input to ensure it's an integer
if product_id.isdigit():
product_id = int(product_id)
else:
print("Please enter a valid product ID.")
continue
if product_id == 0:
break
product = next((p for p in products if p["id"] == product_id), None)
if product:
cart.append(product)
print("{} added to cart.".format(product['name']))
else:
print("Invalid product ID.")
if cart:
total = sum(item["price"] for item in cart)
print("\nYour Cart:")
for item in cart:
print("{} - ${}".format(item['name'], item['price']))
print("Total: ${}".format(total))
else:
print("Your cart is empty.")
# Main function
def main():
while True:
print("\nWelcome to the Online Shopping System")
print("1. Register")
print("2. Login")
print("3. Exit")
choice = raw_input("Choose an option: ").strip() # Strip whitespace from
input
if choice == "1":
register_user()
elif choice == "2":
if authenticate_user():
place_order()
elif choice == "3":
print("Thank you for using the Online Shopping System!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Output-
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 1
Enter a username: Sak
Enter a password: 1234
User registered successfully!
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 2
Enter your username: Sak
Enter your password: 1234
Login successful!
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 1
Laptop added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 2
Smartphone added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 0
Your Cart:
Laptop - $80000
Smartphone - $50000
Total: $130000
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 1
Enter a username: Adi
Enter a password: 1357
User registered successfully!
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 2
Enter your username: Adi
Enter your password: 1357
Login successful!
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 1
Laptop added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 3
Headphones added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 5
Air Pods added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: AirPods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 0
Your Cart:
Laptop - $80000
Headphones - $10000
Air Pods - $20000
Total: $110000
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 1
Enter a username: Dis
Enter a password: 1209
User registered successfully!
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 2
Enter your username: Dis
Enter your password: 1209
Login successful!
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 2
Smartphone added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 5
Air Pods added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 0
Your Cart:
Smartphone - $50000
Air Pods - $20000
Total: $70000
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 1
Enter a username: Mil
Enter a password: 1423
User registered successfully!
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 2
Enter your username: Mil
Enter your password: 1423
Login successful!
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 1
Laptop added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 2
Smartphone added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 3
Headphones added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 4
PS5 added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 5
Air Pods added to cart.
Available Products:
ID: 1, Name: Laptop, Price: $80000
ID: 2, Name: Smartphone, Price: $50000
ID: 3, Name: Headphones, Price: $10000
ID: 4, Name: PS5, Price: $70000
ID: 5, Name: Air Pods, Price: $20000
Enter the product ID to add to cart (0 to checkout): 0
Your Cart:
Laptop - $80000
Smartphone - $50000
Headphones - $10000
PS5 - $70000
AirPods - $20000
Total: $230000
Welcome to the Online Shopping System
1. Register
2. Login
3. Exit
Choose an option: 3
Thank you for using the Online Shopping System!