shop_seller.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. def add_to_stock():
  14. global cursor
  15. global conn
  16. trigger = "yes"
  17. print("Adding new products to the stock.")
  18. while trigger == "yes":
  19. add_product = input("What product do you want to add to your stock? ")
  20. add_price = input("Which price should the product have? ")
  21. add_quantity = input("How much products do you want to add? ")
  22. cursor.executemany("""
  23. INSERT INTO products (name, price, quantity)
  24. VALUES (%s, %s, %s)
  25. """, [
  26. (add_product, add_price, add_quantity)
  27. ])
  28. trigger = input("Do you want to add more Products to your range? (yes/no) ").strip().lower()
  29. print("Updated stock:")
  30. cursor.execute("SELECT name, price, quantity FROM products")
  31. products = cursor.fetchall()
  32. if not products:
  33. print("No products in stock.")
  34. else:
  35. print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
  36. print("-" * 40)
  37. for row in products:
  38. print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
  39. conn.commit()
  40. def update_stock():
  41. global cursor
  42. global conn
  43. trigger2 = "yes"
  44. print("Adding existing products to the stock.")
  45. print("Available stock:")
  46. cursor.execute("SELECT name, price, quantity FROM products")
  47. products = cursor.fetchall()
  48. if not products:
  49. print("No products in stock.")
  50. else:
  51. print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
  52. print("-" * 40)
  53. for row in products:
  54. print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
  55. while trigger2 == "yes":
  56. update_product = input("For which product do want to update the quantity ")
  57. if not update_product:
  58. print("Please select a product.")
  59. continue
  60. cursor.execute("SELECT name FROM products")
  61. product_names = [row[0] for row in cursor.fetchall()]
  62. if update_product not in product_names:
  63. print("Product not found. Please select a valid product.")
  64. continue
  65. else:
  66. update_quantity = input("How many products do you want to add to the stock? ")
  67. cursor.execute("UPDATE products SET quantity = quantity + %s WHERE name = %s", (update_quantity, update_product))
  68. trigger2 = input("Do you want to update more products in your stock? (yes/no) ").strip().lower()
  69. print("Updated stock:")
  70. cursor.execute("SELECT name, price, quantity FROM products")
  71. products = cursor.fetchall()
  72. if not products:
  73. print("No products in stock.")
  74. else:
  75. print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
  76. print("-" * 40)
  77. for row in products:
  78. print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
  79. conn.commit()
  80. def view_stock():
  81. global cursor
  82. global conn
  83. print("Current stock")
  84. print("")
  85. cursor.execute("SELECT name, price, quantity FROM products")
  86. products = cursor.fetchall()
  87. if not products:
  88. print("No products in stock.")
  89. else:
  90. print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
  91. print("-" * 40)
  92. for row in products:
  93. print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
  94. def view_purchases():
  95. global cursor
  96. print("All purchases")
  97. print("")
  98. cursor.execute("SELECT user, name, quantity, total_price, purchase_date FROM purchases ORDER BY purchase_date DESC")
  99. purchases = cursor.fetchall()
  100. if not purchases:
  101. print("No purchases found.")
  102. else:
  103. print(f"{'User':<15} {'Product':<15} {'Quantity':<10} {'Total Price':<15} {'Date':<25}")
  104. print("-" * 80)
  105. for row in purchases:
  106. print(f"{str(row[0]):<15} {str(row[1]):<15} {str(row[2]):<10} {str(row[3]):<15} {str(row[4]):<25}")
  107. def view_carts():
  108. global cursor
  109. print("Aktive carts")
  110. print("")
  111. cursor.execute("SELECT user, name, quantity FROM cart ORDER BY user")
  112. purchases = cursor.fetchall()
  113. if not purchases:
  114. print("No carts found.")
  115. else:
  116. print(f"{'User':<15} {'Product':<15} {'Quantity':<10}")
  117. print("-" * 80)
  118. for row in purchases:
  119. print(f"{str(row[0]):<15} {str(row[1]):<15} {str(row[2]):<10}")
  120. username_db = "admin"
  121. password_db = "admin"
  122. #-----------------------------------------------------------------------#
  123. # Seller System
  124. print("Seller login")
  125. print("")
  126. username = input("Enter your username: ")
  127. password = input("Enter your password: ")
  128. action = ""
  129. if username == username_db and password == password_db:
  130. while action != "logout":
  131. action = input("What do you want to do? (add, update, view stock, view purchases, view carts logout) ")
  132. print("")
  133. if action == "add":
  134. add_to_stock()
  135. elif action == "update":
  136. update_stock()
  137. elif action == "view stock":
  138. view_stock()
  139. elif action == "view purchases":
  140. view_purchases()
  141. elif action == "view carts":
  142. view_carts()
  143. elif action == "logout":
  144. print("Logout successfully!")
  145. break
  146. else:
  147. print("Use one of the displayed actions.")
  148. else:
  149. print("Username oder pasword is wrong!")