|
| 1 | +#MANEJO DE FICHEROS |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +try: |
| 6 | + #Creacion y escritura |
| 7 | + with open("jptxaya.txt","w+") as my_fich: |
| 8 | + my_fich.writelines(["Jptxaya \n","45 \n"]) |
| 9 | + my_fich.write("Java") |
| 10 | + #Lectura Fichero |
| 11 | + with open("jptxaya.txt","r") as my_fich: |
| 12 | + for line in my_fich: |
| 13 | + print(line) |
| 14 | + #Borrado del fichero |
| 15 | + os.remove("jptxaya.txt") |
| 16 | +except Exception as exc: |
| 17 | + print(f"Exception {exc}") |
| 18 | + |
| 19 | +#Dificultad Extra |
| 20 | + |
| 21 | +def fich_lines_to_list(): |
| 22 | + with open("jptx_ventas.txt","r") as my_fich: |
| 23 | + new_list = [] |
| 24 | + for line in my_fich.readlines(): |
| 25 | + lista = line.split(";") |
| 26 | + new_list.append(lista) |
| 27 | + return new_list |
| 28 | + |
| 29 | +def list_to_fich(lista): |
| 30 | + with open("jptx_ventas.txt","w") as my_fich: |
| 31 | + for elem in lista: |
| 32 | + line = ";".join([elem[0],str(elem[1]),str(elem[2]),"\n"]) |
| 33 | + my_fich.write(line) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +print("Dificultad Extra") |
| 40 | +while True: |
| 41 | + print("Gestion de Ventas") |
| 42 | + print("1-Añadir") |
| 43 | + print("2-Consultar") |
| 44 | + print("3-Actualizar") |
| 45 | + print("4-Eliminar") |
| 46 | + print("5-Venta Total") |
| 47 | + print("6-Salir") |
| 48 | + option = input("Selecciona la opcion:") |
| 49 | + match option: |
| 50 | + case "6": |
| 51 | + try: |
| 52 | + os.remove("jptx_ventas.txt") |
| 53 | + break |
| 54 | + except Exception as excep: |
| 55 | + print("Nos se ha podido borrar el fichero porque no existia") |
| 56 | + case "1": |
| 57 | + try: |
| 58 | + nombre_producto = input("Introduce el nombre producto:") |
| 59 | + cantidad_vendida = int(input("Cantidad Vendida:")) |
| 60 | + precio = int(input("Introducir Precio:")) |
| 61 | + with open("jptx_ventas.txt","a+") as my_fich: |
| 62 | + line = ";".join([nombre_producto,str(cantidad_vendida),str(precio),"\n"]) |
| 63 | + my_fich.write(line) |
| 64 | + except Exception as exc: |
| 65 | + print("Datos incorrectos introducidos") |
| 66 | + case "2": |
| 67 | + lista_total = fich_lines_to_list() |
| 68 | + count = 1 |
| 69 | + for lista in lista_total: |
| 70 | + print(f"Linea {str(count)} Product: {lista[0]} Cantidad:{lista[1]} Precio:{lista[2]}") |
| 71 | + count += 1 |
| 72 | + case "3": |
| 73 | + try: |
| 74 | + lista_total = fich_lines_to_list() |
| 75 | + count = 1 |
| 76 | + for lista in lista_total: |
| 77 | + print(f"Linea {str(count)} Product: {lista[0]} Cantidad:{lista[1]} Precio:{lista[2]}") |
| 78 | + count += 1 |
| 79 | + lin_act = int(input("Introducir la el número linea a actualizar:")) |
| 80 | + if lin_act <= len(lista_total) and lin_act >= 1: |
| 81 | + nombre_producto = input("Introduce el nombre producto:") |
| 82 | + cantidad_vendida = int(input("Cantidad Vendida:")) |
| 83 | + precio = int(input("Introducir Precio:")) |
| 84 | + lista_total[lin_act-1] = [nombre_producto,cantidad_vendida,precio] |
| 85 | + list_to_fich(lista_total) |
| 86 | + except Exception as excp: |
| 87 | + print(f"Exception {excp}") |
| 88 | + case "4": |
| 89 | + try: |
| 90 | + lista_total = fich_lines_to_list() |
| 91 | + count = 1 |
| 92 | + for lista in lista_total: |
| 93 | + print(f"Linea {str(count)} Product: {lista[0]} Cantidad:{lista[1]} Precio:{lista[2]}") |
| 94 | + count += 1 |
| 95 | + lin_act = int(input("Introducir el número linea a eliminar:")) |
| 96 | + if lin_act <= len(lista_total) and lin_act >= 1: |
| 97 | + lista_total.pop(lin_act-1) |
| 98 | + list_to_fich(lista_total) |
| 99 | + except Exception as excp: |
| 100 | + print(f"Exception {excp}") |
| 101 | + case "5": |
| 102 | + try: |
| 103 | + lista_total = fich_lines_to_list() |
| 104 | + my_dict = {} |
| 105 | + for elem in lista_total: |
| 106 | + my_dict[elem[0]] = my_dict.get(elem[0],0) + (int(elem[1]) * int(elem[2])) |
| 107 | + venta_total = 0 |
| 108 | + for elem in my_dict.keys(): |
| 109 | + print(f"Producto {elem} Ventas:{my_dict[elem]}") |
| 110 | + venta_total += int(my_dict[elem]) |
| 111 | + print(f"Ventas Totales:{venta_total}") |
| 112 | + except Exception as excp: |
| 113 | + print(f"Exception {excp}") |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments