Advertisement
teslariu

base resto

Sep 6th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import sqlite3
  5.  
  6. def mostrar_menu():
  7.     return """
  8.         Menu de administracion del restaurante
  9.     *-------------------------------------------*
  10.         1. Agregar categoria
  11.         2. Agregar plato
  12.         3. Mostrar carta
  13.         4. Salir
  14.     *-------------------------------------------*
  15.     """
  16.  
  17. def agregar_categoria(nombre):
  18.     try:
  19.         conn.execute("INSERT INTO categoria VALUES (null, ?)", (nombre,))
  20.     except sqlite3.IntegrityError:
  21.         print(f"Error, ya existe la categoria {nombre}")
  22.     else:
  23.         conn.commit()
  24.  
  25.  
  26.    
  27. def agregar_plato():
  28.     try:
  29.         cursor.execute("SELECT * FROM categoria ORDER BY id")
  30.     except sqlite3.OperationalError:
  31.         print(f"No se pudieron cargar categorias")
  32.     else:
  33.         categorias = cursor.fetchall()
  34.         if categorias:
  35.             id_categoria = []
  36.             print("Categorias")
  37.             print("==========")
  38.             print("ID | nombre")
  39.             print("-------------")
  40.             for ID,nombre in categorias:
  41.                 id_categoria.append(ID)
  42.                 print(f"{ID} | {nombre}")
  43.             while True:
  44.                 try:
  45.                     ID = int(input("\nSeleccione un id: "))
  46.                 except ValueError:
  47.                     print("Debe elegir un nro entero")
  48.                 else:
  49.                     if ID in id_categoria:
  50.                         break
  51.                     print(f"ID {ID} 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.  
  64. def mostrar_carta():
  65.     query = "SELECT categoria.id, categoria.nombre, platos.id, platos.nombre FROM categoria\
  66.             LEFT JOIN platos\
  67.             ON platos.id_categoria = categoria.id\
  68.             ORDER BY categoria.id, platos.id"
  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("{:<10}".format(elemento), end=" ")
  79.                
  80.                 if isinstance(elemento,int):
  81.                     print("{:^3}".format(elemento), end=" ")
  82.         print()
  83.     else:
  84.         print("No hay ningún plato en la carta")
  85.  
  86.  
  87.  
  88. while True:
  89.     conn = sqlite3.connect("restaurante.sqlite")
  90.     cursor = conn.cursor()
  91.    
  92.     print(mostrar_menu())
  93.    
  94.     opcion = input("Seleccione una opción: ")
  95.    
  96.     if opcion == "1":
  97.         agregar_categoria(input("Ingrese el nombre: "))
  98.        
  99.     elif opcion == "2":
  100.         agregar_plato()
  101.        
  102.     elif opcion == "3":
  103.         mostrar_carta()
  104.        
  105.     elif opcion == "4":
  106.         conn.close()
  107.         print("Hasta luego...")
  108.         break
  109.        
  110.     else:
  111.         print("Opción incorrecta")
  112.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement