baumsplitter41 преди 6 месеца
родител
ревизия
206e524e3c
променени са 2 файла, в които са добавени 198 реда и са изтрити 9 реда
  1. 16 9
      Python/Shop.py
  2. 182 0
      Python/shop_seller.py

+ 16 - 9
Python/Shop.py

@@ -55,8 +55,14 @@ def buy_products():
 
     print("Available Products:")
     cursor.execute("SELECT name, price, quantity FROM products")
-    for row in cursor.fetchall():
-        print(row)
+    purchases = cursor.fetchall()
+    if not purchases:
+        print("No purchases found.")
+    else:
+        print(f"{'Product':<15}  {'Price':<15} {'Quantity':<10}")
+        print("-" * 50)
+        for row in purchases:
+            print(f"{str(row[0]):<15} {str(row[1]):<15} {str(row[2]):<10}")
 
     while trigger.lower() == "yes":
         
@@ -72,7 +78,7 @@ def buy_products():
             print("Product not found. Please select a valid product.")
             continue
         else:
-            select_quantity = input("How many product do you want to buy? ")
+            select_quantity = input("How many products do you want to buy? ")
 
         cursor.executemany("""
         INSERT INTO cart (user, name, quantity)
@@ -98,8 +104,12 @@ def cart_func():
 
     cursor.execute("SELECT name, quantity FROM cart WHERE user = %s", (user,))
     print("Your Cart:")
-    for row in cursor.fetchall():
-        print(row)
+    cart2 = cursor.fetchall()
+
+    print(f"{'Product':<15} {'Quantity':<10}")
+    print("-" * 30)
+    for row in cart2:
+        print(f"{str(row[0]):<15} {str(row[1]):<10}")
 
     buy = input("Do you want to proceed to buy the products in your cart? (yes/no) ").strip().lower()
     if buy == "yes":
@@ -124,18 +134,15 @@ def cart_func():
             cursor.execute("DELETE FROM cart WHERE user = %s", (user,))
             conn.commit()
 
-
 #-----------------------------------------------------------------------#
 # User Interaction
 
 trigger = "yes"
-user = input("Enter your username: ")
 
+user = input("Enter your username: ")
 buy_products()
 
 
 
 
 
-
-

+ 182 - 0
Python/shop_seller.py

@@ -0,0 +1,182 @@
+import mysql.connector
+
+#-----------------------------------------------------------------------#
+# Relevant System
+
+conn = mysql.connector.connect(
+    host="localhost",
+    user="root",
+    password=""
+)
+cursor = conn.cursor()
+
+#cursor.execute("DROP DATABASE IF EXISTS Shop")
+cursor.execute("CREATE DATABASE IF NOT EXISTS Shop")
+conn.database = "Shop"
+
+def add_to_stock():
+    global cursor
+    global conn
+    trigger = "yes"
+
+    print("Adding new products to the stock.")
+    while trigger == "yes":
+        
+        add_product = input("What product do you want to add to your stock? ")
+        add_price = input("Which price should the product have? ")
+        add_quantity = input("How much products do you want to add? ")
+
+        cursor.executemany("""
+        INSERT INTO products (name, price, quantity)
+        VALUES (%s, %s, %s)
+        """, [
+            (add_product, add_price, add_quantity)
+        ])
+
+        trigger = input("Do you want to add more Products to your range? (yes/no) ").strip().lower()
+    
+    print("Updated stock:")
+    cursor.execute("SELECT name, price, quantity FROM products")
+    products = cursor.fetchall()
+    if not products:
+        print("No products in stock.")
+    else:
+        print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
+        print("-" * 40)
+        for row in products:
+            print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
+    conn.commit()
+
+
+
+def update_stock():
+    global cursor
+    global conn
+    trigger2 = "yes"
+
+    print("Adding existing products to the stock.")
+    print("Available stock:")
+    cursor.execute("SELECT name, price, quantity FROM products")
+    products = cursor.fetchall()
+    if not products:
+        print("No products in stock.")
+    else:
+        print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
+        print("-" * 40)
+        for row in products:
+            print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
+
+
+    while trigger2 == "yes":
+        update_product = input("For which product do want to update the quantity ")
+        if not update_product:
+            print("Please select a product.")
+            continue
+        
+        cursor.execute("SELECT name FROM products")
+        product_names = [row[0] for row in cursor.fetchall()]
+        
+        if update_product not in product_names:
+            print("Product not found. Please select a valid product.")
+            continue
+        else:
+            update_quantity = input("How many products do you want to add to the stock? ")
+        
+        cursor.execute("UPDATE products SET quantity = quantity + %s WHERE name = %s", (update_quantity, update_product))
+
+        trigger2 = input("Do you want to update more products in your stock? (yes/no) ").strip().lower()
+
+    print("Updated stock:")
+    cursor.execute("SELECT name, price, quantity FROM products")
+    products = cursor.fetchall()
+    
+    if not products:
+        print("No products in stock.")
+    else:
+        print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
+        print("-" * 40)
+        for row in products:
+            print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
+    conn.commit()
+
+
+def view_stock():
+    global cursor
+    global conn
+    print("Current stock")
+    print("")
+    cursor.execute("SELECT name, price, quantity FROM products")
+    products = cursor.fetchall()
+    if not products:
+        print("No products in stock.")
+    else:
+        print(f"{'Product':<20} {'Price':<10} {'Quantity':<10}")
+        print("-" * 40)
+        for row in products:
+            print(f"{str(row[0]):<20} {str(row[1]):<10} {str(row[2]):<10}")
+
+def view_purchases():
+    global cursor
+    print("All purchases")
+    print("")
+    cursor.execute("SELECT user, name, quantity, total_price, purchase_date FROM purchases ORDER BY purchase_date DESC")
+    purchases = cursor.fetchall()
+    if not purchases:
+        print("No purchases found.")
+    else:
+        print(f"{'User':<15} {'Product':<15} {'Quantity':<10} {'Total Price':<15} {'Date':<25}")
+        print("-" * 80)
+        for row in purchases:
+            print(f"{str(row[0]):<15} {str(row[1]):<15} {str(row[2]):<10} {str(row[3]):<15} {str(row[4]):<25}")
+
+def view_carts():
+    global cursor
+    print("Aktive carts")
+    print("")
+    cursor.execute("SELECT user, name, quantity FROM cart ORDER BY user")
+    purchases = cursor.fetchall()
+    if not purchases:
+        print("No carts found.")
+    else:
+        print(f"{'User':<15} {'Product':<15} {'Quantity':<10}")
+        print("-" * 80)
+        for row in purchases:
+            print(f"{str(row[0]):<15} {str(row[1]):<15} {str(row[2]):<10}")
+
+
+username_db = "admin"
+password_db = "admin"
+
+#-----------------------------------------------------------------------#
+# Seller System
+
+print("Seller login")
+print("")
+
+
+username = input("Enter your username: ")
+password = input("Enter your password: ")
+action = ""
+
+if username == username_db and password == password_db:
+    while action != "logout":
+        action = input("What do you want to do? (add, update, view stock, view purchases, view carts logout) ")
+        print("")
+        if action == "add":
+            add_to_stock()
+        elif action == "update":
+            update_stock()
+        elif action == "view stock":
+            view_stock()
+        elif action == "view purchases":
+            view_purchases()
+        elif action == "view carts":
+            view_carts()
+        elif action == "logout":
+            print("Logout successfully!")
+            break
+        else:
+            print("Use one of the displayed actions.")
+
+else:
+    print("Username oder pasword is wrong!")