Advertisement
GastonPalazzo

Condicionales - Ej-11

Aug 19th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. #imports
  2. import os
  3. from pathlib import Path
  4.  
  5. #funciones
  6. def crearRuta():
  7.     p=Path.home()/'GestorDeCompras' #ruta de almacenamiento
  8.     os.makedirs(p, exist_ok=True) #verificacion y creacion del path
  9.  
  10. def crearArchivosDeGuardado():
  11.     data=[[], [], [], []]
  12.     return data
  13.  
  14.  
  15. def verLista(dataL):
  16.     print('\nCODIGO\t\tPRECIO UNITARIO\t\tCANTIDAD\t\tPRECIO TOTAL\t\tNOMBRE')
  17.     for i in range(len(dataL[0])):
  18.         for j in range(1):
  19.             print(f'{i}\t\t{dataL[j+1][i]}\t\t\t{dataL[j+2][i]}\t\t\t{dataL[j+3][i]}\t\t\t{dataL[j][i]}')
  20.  
  21. def agregarProducto(dataL):
  22.     nombre=input('\nNombre de producto: ')
  23.     if nombre!='-1':
  24.         precio=float(input('\nPrecio por unidad: $'))
  25.         if precio!=-1:
  26.             cantidad=int(input('\nCantidad a agregar: '))
  27.             if cantidad!=-1:
  28.                 #filtro de guardado ↓
  29.                 if nombre in dataL[0]:
  30.                     #producto ya ingresado
  31.                     key=dataL[0].index(nombre)
  32.                     dataL[2][key]+=cantidad
  33.                     dataL[3][key]+=(cantidad*dataL[1][key])
  34.                 else:
  35.                     #producto nuevo
  36.                     dataL[0].append(nombre)
  37.                     dataL[1].append(precio)
  38.                     dataL[2].append(cantidad)
  39.                     dataL[3].append((dataL[1][len(dataL[0])-1])*(dataL[2][len(dataL[0])-1])) #precio x cantidad
  40.             else:
  41.                 pass
  42.         else:
  43.             pass
  44.     else:
  45.         pass
  46.     return dataL
  47.  
  48. def quitarProducto(dataL):
  49.     try:
  50.         key=int(input('\nIngrese codigo de producto: '))
  51.         if key!=-1 and key<=len(dataL[0]): # <- verificacion de key
  52.             try:
  53.                 cant=int(input('\nIngrese cantidad a quitar (0 -> quitar todo): '))
  54.                 if cant==0:
  55.                     #elimina el elemento de la lista
  56.                     for i in dataL:
  57.                         i.pop(key)
  58.                 elif cant>0:
  59.                     #quita una cantidad x del total de un producto
  60.                     if cant>dataL[2][key]:
  61.                         dataL[2][key]=0 #si la cantidad a eliminar supera el total disponible, la disponibilidad se iguala a cero
  62.                     else:
  63.                         dataL[2][key]-=cant
  64.                     dataL[3][key]-=(cant*dataL[1][key])
  65.                 elif cant==-1:
  66.                     pass
  67.                 else:
  68.                     print('\nError: la cantidad a eliminar es invalida!')
  69.             except ValueError:
  70.                 print('\nError: la cantidad de producto debe ser un valor numerico')
  71.         elif key==-1:
  72.                 pass
  73.         else:
  74.             print('\nError: el producto especificado no se encuentra en la lista!')
  75.     except ValueError:
  76.         print('\nError: el codigo de producto debe ser un valor numerico')
  77.     return dataL
  78.  
  79. def vaciarLista(dataL):
  80.     for i in dataL:
  81.         i.clear()
  82.     print('\nLa lista de compras ah sido vaciada exitosamente!')
  83.     return dataL
  84.  
  85. def guardarLista(dataL,nameDefault='listaDeCompras 0'):
  86.     p=Path.home()/'GestorDeCompras\\ListasDeCompras' #ruta de guardado de listas de compra
  87.     os.makedirs(p, exist_ok=True) #verificacion y creacion del path
  88.     os.chdir(p)
  89.     #creacion del archivo
  90.     if Path.exists(Path.cwd()/f'{nameDefault}.txt'):
  91.         tempName=nameDefault.split(' ')
  92.         nameDefault=f'{tempName[0]} {int(tempName[1])+1}'
  93.         guardarLista(dataList, nameDefault)
  94.     else:
  95.         aux=open(f'{nameDefault}.txt', 'a', encoding='UTF-8')
  96.         #escritura del archivo
  97.         aux.writelines('CODIGO\t\tPRECIO UNITARIO\t\tCANTIDAD\t\tPRECIO TOTAL\t\tNOMBRE')
  98.         for i in range(len(dataL[0])):
  99.             for j in range(1):
  100.                 aux.writelines(f'\n{i}\t\t{dataL[j+1][i]}\t\t\t{dataL[j+2][i]}\t\t\t{dataL[j+3][i]}\t\t\t{dataL[j][i]}')
  101.         #total a pagar
  102.         totalApagar=0
  103.         for i in dataL[3]:
  104.             totalApagar+=i
  105.         aux.write(f'\n\n|Total a pagar| -> {totalApagar}')
  106.         aux.close()
  107.         print('\nSu lista a sido guardada con exito! -> ',Path.cwd())
  108.  
  109. #main
  110. print('|Ej 11|')
  111. crearRuta()
  112. dataList=crearArchivosDeGuardado()
  113.  
  114. print('\n|Gestor de lista de compras|')
  115. #bucle de menu principal ↓
  116. while True:
  117.     print('\nOpciones:\n1. Ver Lista\n2. Agregar un producto\n3. Quitar un producto\n4. Vaciar lista\n5. Guardar lista\n0. Salir')
  118.     print('\nNota: utilizar -1 en los sub menus, sirve para volver al menu anterior.')
  119.     opc=input('\n<opcion>: ')
  120.     if opc=='0':
  121.         break
  122.     elif opc=='1':
  123.         #visor de lista
  124.         verLista(dataList)
  125.     elif opc=='2':
  126.         #agregar a lista
  127.         dataList=agregarProducto(dataList)
  128.     elif opc=='3':
  129.         #quitar de lista
  130.         dataList=quitarProducto(dataList)
  131.     elif opc=='4':
  132.         #reiniciar lista
  133.         dataList=vaciarLista(dataList)
  134.     elif opc=='5':
  135.         #guardar lista
  136.         guardarLista(dataList)
  137.     else:
  138.         print('\nError: la opcion solicitada es invalida!')
  139.  
  140. print('\nFin de la ejecucion!\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement