Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #imports
- import os
- from pathlib import Path
- #funciones
- def crearRuta():
- p=Path.home()/'GestorDeCompras' #ruta de almacenamiento
- os.makedirs(p, exist_ok=True) #verificacion y creacion del path
- def crearArchivosDeGuardado():
- data=[[], [], [], []]
- return data
- def verLista(dataL):
- print('\nCODIGO\t\tPRECIO UNITARIO\t\tCANTIDAD\t\tPRECIO TOTAL\t\tNOMBRE')
- for i in range(len(dataL[0])):
- for j in range(1):
- 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]}')
- def agregarProducto(dataL):
- nombre=input('\nNombre de producto: ')
- if nombre!='-1':
- precio=float(input('\nPrecio por unidad: $'))
- if precio!=-1:
- cantidad=int(input('\nCantidad a agregar: '))
- if cantidad!=-1:
- #filtro de guardado ↓
- if nombre in dataL[0]:
- #producto ya ingresado
- key=dataL[0].index(nombre)
- dataL[2][key]+=cantidad
- dataL[3][key]+=(cantidad*dataL[1][key])
- else:
- #producto nuevo
- dataL[0].append(nombre)
- dataL[1].append(precio)
- dataL[2].append(cantidad)
- dataL[3].append((dataL[1][len(dataL[0])-1])*(dataL[2][len(dataL[0])-1])) #precio x cantidad
- else:
- pass
- else:
- pass
- else:
- pass
- return dataL
- def quitarProducto(dataL):
- try:
- key=int(input('\nIngrese codigo de producto: '))
- if key!=-1 and key<=len(dataL[0]): # <- verificacion de key
- try:
- cant=int(input('\nIngrese cantidad a quitar (0 -> quitar todo): '))
- if cant==0:
- #elimina el elemento de la lista
- for i in dataL:
- i.pop(key)
- elif cant>0:
- #quita una cantidad x del total de un producto
- if cant>dataL[2][key]:
- dataL[2][key]=0 #si la cantidad a eliminar supera el total disponible, la disponibilidad se iguala a cero
- else:
- dataL[2][key]-=cant
- dataL[3][key]-=(cant*dataL[1][key])
- elif cant==-1:
- pass
- else:
- print('\nError: la cantidad a eliminar es invalida!')
- except ValueError:
- print('\nError: la cantidad de producto debe ser un valor numerico')
- elif key==-1:
- pass
- else:
- print('\nError: el producto especificado no se encuentra en la lista!')
- except ValueError:
- print('\nError: el codigo de producto debe ser un valor numerico')
- return dataL
- def vaciarLista(dataL):
- for i in dataL:
- i.clear()
- print('\nLa lista de compras ah sido vaciada exitosamente!')
- return dataL
- def guardarLista(dataL,nameDefault='listaDeCompras 0'):
- p=Path.home()/'GestorDeCompras\\ListasDeCompras' #ruta de guardado de listas de compra
- os.makedirs(p, exist_ok=True) #verificacion y creacion del path
- os.chdir(p)
- #creacion del archivo
- if Path.exists(Path.cwd()/f'{nameDefault}.txt'):
- tempName=nameDefault.split(' ')
- nameDefault=f'{tempName[0]} {int(tempName[1])+1}'
- guardarLista(dataList, nameDefault)
- else:
- aux=open(f'{nameDefault}.txt', 'a', encoding='UTF-8')
- #escritura del archivo
- aux.writelines('CODIGO\t\tPRECIO UNITARIO\t\tCANTIDAD\t\tPRECIO TOTAL\t\tNOMBRE')
- for i in range(len(dataL[0])):
- for j in range(1):
- 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]}')
- #total a pagar
- totalApagar=0
- for i in dataL[3]:
- totalApagar+=i
- aux.write(f'\n\n|Total a pagar| -> {totalApagar}')
- aux.close()
- print('\nSu lista a sido guardada con exito! -> ',Path.cwd())
- #main
- print('|Ej 11|')
- crearRuta()
- dataList=crearArchivosDeGuardado()
- print('\n|Gestor de lista de compras|')
- #bucle de menu principal ↓
- while True:
- print('\nOpciones:\n1. Ver Lista\n2. Agregar un producto\n3. Quitar un producto\n4. Vaciar lista\n5. Guardar lista\n0. Salir')
- print('\nNota: utilizar -1 en los sub menus, sirve para volver al menu anterior.')
- opc=input('\n<opcion>: ')
- if opc=='0':
- break
- elif opc=='1':
- #visor de lista
- verLista(dataList)
- elif opc=='2':
- #agregar a lista
- dataList=agregarProducto(dataList)
- elif opc=='3':
- #quitar de lista
- dataList=quitarProducto(dataList)
- elif opc=='4':
- #reiniciar lista
- dataList=vaciarLista(dataList)
- elif opc=='5':
- #guardar lista
- guardarLista(dataList)
- else:
- print('\nError: la opcion solicitada es invalida!')
- print('\nFin de la ejecucion!\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement