196 lines
6.8 KiB
Python
Raw Normal View History

2022-04-07 14:43:17 +02:00
# 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<sum_of_price):
print("Zu wenig Geld")
return
# Print the change
print("Rückgeld: %s" % (money_in_machine-sum_of_price))
# Stückelung
rest_euro=money_in_machine-sum_of_price
rest_cent=rest_euro*100
# 10 Euro
rest_euro_10=rest_cent//10000
rest_cent=rest_cent-rest_euro_10*10000
# 5 Euro
rest_euro_5=rest_cent//5000
rest_cent=rest_cent-rest_euro_5*5000
# 2 Euro
rest_euro_2=rest_cent//2000
rest_cent=rest_cent-rest_euro_2*2000
# 1 Euro
rest_euro_1=rest_cent//1000
rest_cent=rest_cent-rest_euro_1*1000
# 50 Cent
rest_cent_50=rest_cent//500
rest_cent=rest_cent-rest_cent_50*500
# 20 Cent
rest_cent_20=rest_cent//200
rest_cent=rest_cent-rest_cent_20*200
# 10 Cent
rest_cent_10=rest_cent//100
rest_cent=rest_cent-rest_cent_10*100
# 5 Cent
rest_cent_5=rest_cent//50
rest_cent=rest_cent-rest_cent_5*50
print ("10 Euro: %s" % rest_euro_10)
print ("5 Euro: %s" % rest_euro_5)
print ("2 Euro: %s" % rest_euro_2)
print ("1 Euro: %s" % rest_euro_1)
print ("50 Cent: %s" % rest_cent_50)
print ("20 Cent: %s" % rest_cent_20)
print ("10 Cent: %s" % rest_cent_10)
print ("5 Cent: %s" % rest_cent_5)
normal_mode()