Shop.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import mysql.connector
  2. #-----------------------------------------------------------------------#
  3. # Relevant System
  4. conn = mysql.connector.connect(
  5. host="localhost",
  6. user="root",
  7. password=""
  8. )
  9. cursor = conn.cursor()
  10. #cursor.execute("DROP DATABASE IF EXISTS Shop")
  11. cursor.execute("CREATE DATABASE IF NOT EXISTS Shop")
  12. conn.database = "Shop"
  13. cursor.execute("""
  14. CREATE TABLE IF NOT EXISTS products (
  15. id INT AUTO_INCREMENT PRIMARY KEY,
  16. name VARCHAR(100),
  17. price DECIMAL(10, 2),
  18. quantity INT
  19. )
  20. """)
  21. cursor.execute("""
  22. CREATE TABLE IF NOT EXISTS cart (
  23. id INT AUTO_INCREMENT PRIMARY KEY,
  24. user VARCHAR(100),
  25. name VARCHAR(100),
  26. quantity INT
  27. )
  28. """)
  29. cursor.execute("""
  30. CREATE TABLE IF NOT EXISTS purchases (
  31. id INT AUTO_INCREMENT PRIMARY KEY,
  32. user VARCHAR(100),
  33. name VARCHAR(100),
  34. quantity INT,
  35. purchase_date DATETIME DEFAULT CURRENT_TIMESTAMP,
  36. total_price DECIMAL(10, 2)
  37. )
  38. """)
  39. def buy_products():
  40. global trigger
  41. global user
  42. global cursor
  43. global conn
  44. global select_quantity
  45. global selection
  46. print("Available Products:")
  47. cursor.execute("SELECT name, price, quantity FROM products")
  48. purchases = cursor.fetchall()
  49. if not purchases:
  50. print("No purchases found.")
  51. else:
  52. print(f"{'Product':<15} {'Price':<15} {'Quantity':<10}")
  53. print("-" * 50)
  54. for row in purchases:
  55. print(f"{str(row[0]):<15} {str(row[1]):<15} {str(row[2]):<10}")
  56. while trigger.lower() == "yes":
  57. selection = input("Which Product do you want to buy? ")
  58. if not selection:
  59. print("Please select a product.")
  60. continue
  61. cursor.execute("SELECT name FROM products")
  62. product_names = [row[0] for row in cursor.fetchall()]
  63. if selection not in product_names:
  64. print("Product not found. Please select a valid product.")
  65. continue
  66. else:
  67. select_quantity = input("How many products do you want to buy? ")
  68. cursor.executemany("""
  69. INSERT INTO cart (user, name, quantity)
  70. VALUES (%s, %s, %s)
  71. """, [
  72. (user, selection, select_quantity)
  73. ])
  74. cursor.execute("UPDATE products SET quantity = quantity - %s WHERE name = %s", (select_quantity, selection))
  75. trigger = input("Do you want to buy another product? (yes/no) ").strip().lower()
  76. cart_func()
  77. conn.commit()
  78. def cart_func():
  79. global user
  80. global cursor
  81. global trigger
  82. global select_quantity
  83. global selection
  84. cursor.execute("SELECT name, quantity FROM cart WHERE user = %s", (user,))
  85. print("Your Cart:")
  86. cart2 = cursor.fetchall()
  87. print(f"{'Product':<15} {'Quantity':<10}")
  88. print("-" * 30)
  89. for row in cart2:
  90. print(f"{str(row[0]):<15} {str(row[1]):<10}")
  91. buy = input("Do you want to proceed to buy the products in your cart? (yes/no) ").strip().lower()
  92. if buy == "yes":
  93. cursor.execute("""
  94. INSERT INTO purchases (user, name, quantity, purchase_date, total_price)
  95. SELECT c.user, c.name, c.quantity, NOW(), p.price * c.quantity
  96. FROM cart c
  97. JOIN products p ON c.name = p.name
  98. WHERE c.user = %s
  99. """, (user,))
  100. print("Thank you for your Purchase")
  101. cursor.execute("DELETE FROM cart WHERE user = %s", (user,))
  102. conn.commit()
  103. else:
  104. trigger2 = input("Do you want to buy another product? (yes/no) ").strip().lower()
  105. if trigger2 == "yes":
  106. trigger = "yes"
  107. buy_products()
  108. else:
  109. print("Purchase cancelled.")
  110. cursor.execute("UPDATE products SET quantity = quantity + %s WHERE name = %s", (select_quantity, selection))
  111. cursor.execute("DELETE FROM cart WHERE user = %s", (user,))
  112. conn.commit()
  113. #-----------------------------------------------------------------------#
  114. # User Interaction
  115. trigger = "yes"
  116. user = input("Enter your username: ")
  117. buy_products()