diff --git a/3. Übungen/07.04.2022/Getränke und Snack automat/Getränke_automat_2.py b/3. Übungen/07.04.2022/Getränke und Snack automat/Getränke_automat_2.py new file mode 100644 index 0000000..06bf920 --- /dev/null +++ b/3. Übungen/07.04.2022/Getränke und Snack automat/Getränke_automat_2.py @@ -0,0 +1,195 @@ +# Getränke und Snackautomat +# Uses database to store the products and the amount of money + +# Imports +import sqlite3 +import os +import sys +import time +import datetime + +# Make a new database +conn = sqlite3.connect('Getränkeautomat.db') +c = conn.cursor() +# Create a table for the products +c.execute("CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY, name TEXT, price REAL)") + +# Make an admin mode to add new products and change the price and add an id +def admin_mode(): + weiter_admin=True + while (weiter_admin==True): + # Print the admin menu + print("1. Add Product") + print("2. Change Price") + print("3. Delete Product") + # Ask for the user input + admin_input=input("Please choose an option: ") + # Check if the input is a number + try: + admin_input=int(admin_input) + except ValueError: + print("Please enter a number") + continue + # Check if the input is in the range of the admin menu + if (admin_input<1 or admin_input>4): + print("Please enter a number between 1 and 4") + continue + # Add a new product + # Ask for the product name + # Ask for the product price + # Ask for the product id + # Add the product to the database + if (admin_input==1): + product_name=input("Please enter the product name: ") + product_price=input("Please enter the product price: ") + product_id=input("Please enter the product id: ") + c.execute("INSERT INTO products (name, price, id) VALUES (?, ?, ?)", (product_name, float(product_price), int(product_id))) + conn.commit() + print("Product added") + continue + # Change the price of a product + # Ask for the product id + # Ask for the new price + # Change the price in the database + # Print the old price and the new price + if (admin_input==2): + product_id=input("Please enter the product id: ") + new_price=input("Please enter the new price: ") + c.execute("UPDATE products SET price=? WHERE id=?", (float(new_price), int(product_id))) + conn.commit() + print("Old price: " + str(c.execute("SELECT price FROM products WHERE id=?", (int(product_id),)).fetchone()[0])) + print("New price: " + str(new_price)) + continue + # Delete a product + # Ask for the product id + # Delete the product from the database + if (admin_input==3): + product_id=input("Please enter the product id: ") + c.execute("DELETE FROM products WHERE id=?", (int(product_id),)) + conn.commit() + print("Product deleted") + continue + # Ask if the user wants to continue in the admin mode + if (admin_input==4): + weiter_admin=False + normal_mode() + +# Make the normal operation mode +def normal_mode(): + print("Willkommen beim Getränke und Snackautomat") + print("Drücken Sie 1 um zu starten") + print("Drücken 2 um zum Adminmodus zu wechseln") + print("Drücken 3 um das Programm zu beenden") + # Ask for the user input + user_input=input("Please choose an option: ") + # Check if the input is a number + try: + user_input=int(user_input) + except ValueError: + print("Bitte geben Sie eine Nummer ein") + return + # Check if the input is in the range of the admin menu + if user_input==2: + # Ask for the admin password + admin_password=input("Please enter the admin password: ") + # Check if the password is correct + if (admin_password=="admin"): + # Start the admin mode + admin_mode() + return + else: + print("Wrong password") + return + if user_input==3: + # Exit the program + sys.exit() + if user_input==1: + sum_of_price=0 + normal_continue=True + while (normal_continue==True): + # Print all the products and their price and the id of the product + for row in c.execute("SELECT * FROM products"): + print(row) + # Ask for the product id + product_id=input("Bitte Nummer des gewünschten Produktes eingeben: ") + # Check if the product id is in the database + if (c.execute("SELECT * FROM products WHERE id=?", (int(product_id),)).fetchone()==None): + print("Product nicht gefunden") + return + # Get the price of the product + product_price=c.execute("SELECT price FROM products WHERE id=?", (int(product_id),)).fetchone()[0] + # Ask the amount of the product + product_amount=int(input("Bitte Anzahl des Produktes Eingeben: ")) + sum_of_price=product_price*product_amount + # Print the price of the sum + print("Summe:%s€" % sum_of_price) + # Ask if the user wants to continue + if (input("Weiter Einkaufen? (j/n): ")=="n"): + normal_continue=False + payment(sum_of_price) + return +# Define a function to handle the payment +def payment(sum_of_price): + # Print the price of the sum + print("Summe: %s€" % sum_of_price) + # Ask for the money put in the machine + money_in_machine=input("Bitte Geld eingeben: ") + # Check if the money is a number + try: + money_in_machine=int(money_in_machine) + except ValueError: + print("Bitte geben Sie eine Nummer ein") + return + # Check if the money is enough + if (money_in_machine