Advertisement
teslariu

menu resto

Nov 27th, 2021
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import sqlite3
  5.  
  6. def mostrar_opciones():
  7.     return """
  8.         Menú de administración del restaurante
  9.     *-------------------------------------------*
  10.         1. Agregar categoría
  11.         2. Agregar plato
  12.         3. Mostrar menu
  13.         4. Salir
  14.     *-------------------------------------------*
  15.     """
  16.    
  17. def agregar_categoria(nombre):
  18.     try:
  19.         conn.execute("INSERT INTO categorias VALUES (null,?)", (nombre,))
  20.     except sqlite3.IntegrityError:
  21.         print(f"Error, ya existe la categoria '{nombre}'")
  22.     else:
  23.         conn.commit()
  24.    
  25.    
  26. def agregar_plato():
  27.     try:
  28.         cursor.execute("SELECT * FROM categorias ORDER BY id")
  29.     except sqlite3.OperationalError:
  30.         print("Error en consulta de categorias")
  31.     else:
  32.         categorias = cursor.fetchall()
  33.         if categorias:
  34.             id_categorias = []
  35.             print("Categorias")
  36.             print("----------")
  37.             print("ID | nombre")
  38.             print("-----------")
  39.             for ID,nombre in categorias:
  40.                 id_categorias.append(ID)
  41.                 print(f"{ID} | {nombre}")
  42.                
  43.             while True:
  44.                 try:
  45.                     ID = int(input("\nSeleccione un ID: "))
  46.                 except ValueError:
  47.                     print("El ID debe ser un nro entero")
  48.                 else:
  49.                     if ID in id_categorias:
  50.                         break
  51.                     print("ID de categoria inexistente")
  52.             nombre_plato = input("Ingrese el nombre del plato: ")
  53.             try:
  54.                 conn.execute("INSERT INTO platos VALUES (null,?,?)", (nombre_plato,ID))
  55.             except sqlite3.IntegrityError:
  56.                 print(f"Error, ya existe el plato '{nombre_plato}'")
  57.             else:
  58.                 conn.commit()
  59.         else:
  60.             print("No hay categorias de platos")
  61.  
  62.    
  63. def mostrar_menu():
  64.     query = 'SELECT categorias.id, categorias.nombre, platos.id, platos.nombre FROM categorias\
  65.             LEFT JOIN platos\
  66.             ON platos.id_categoria=categorias.id\
  67.             ORDER BY categorias.id,platos.id'
  68.    
  69.     cursor.execute(query)
  70.     platos = cursor.fetchall()
  71.     if platos:
  72.         print("\nID categoria      ID plato  ")
  73.         print("------------------------------")
  74.         for plato in platos:
  75.             print()
  76.             for elemento in plato:
  77.                 if isinstance(elemento,str):
  78.                     print("{:<12}".format(elemento), end=" ")
  79.                 if isinstance(elemento,int):
  80.                     print("{:^3}".format(elemento), end=" ")   
  81.         print()
  82.     else:
  83.         print("No hay platos en el menu")
  84.    
  85.    
  86. conn = sqlite3.connect("restaurante.db")
  87. cursor = conn.cursor()
  88.  
  89. while True:
  90.     print(mostrar_opciones())
  91.     opcion = input("Seleccione una opción: ")
  92.    
  93.     if opcion == "1":
  94.         agregar_categoria(input("Ingrese el nombre: "))
  95.        
  96.     elif opcion == "2":
  97.         agregar_plato()
  98.        
  99.     elif opcion == "3":
  100.         mostrar_menu()
  101.        
  102.     elif opcion == "4":
  103.         print("Cerrando la base...")
  104.         conn.close()
  105.         break
  106.        
  107.     else:
  108.         print("Opcion incorrecta") 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement