|
| 1 | +import os |
| 2 | + |
| 3 | +# def write_file(): |
| 4 | +# name = 'Lester David' |
| 5 | +# age = "25 de edad" |
| 6 | +# lenguage = 'python' |
| 7 | + |
| 8 | +# return f''' Nombre del estudiante: {name} |
| 9 | +# Edad: {age} |
| 10 | +# lenguaje favorito : {lenguage}''' |
| 11 | + |
| 12 | +# def file(name_file): |
| 13 | +# file = open(name_file, "r+") |
| 14 | +# #file.write(write_file()) |
| 15 | +# print(file.read()) |
| 16 | +# file.close() |
| 17 | + |
| 18 | +# #file('estudiante.txt') |
| 19 | + |
| 20 | +def delete_file(name_file): |
| 21 | + if os.path.exists(name_file): |
| 22 | + os.remove(name_file) |
| 23 | + else: |
| 24 | + print('El archivo no existe') |
| 25 | + |
| 26 | +# delete_file('estudiante.txt') |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +def writeFile(nameFile,product): |
| 31 | + with open(nameFile,'a') as file: |
| 32 | + file.write(product) |
| 33 | + |
| 34 | +def addProduct(): |
| 35 | + name = input('Nombre del producto: ') |
| 36 | + quantity = int(input('Cantidad vendida: ')) |
| 37 | + price = int(input('Precio del producto: ')) |
| 38 | + |
| 39 | + newProduct = f'Nombre Producto: {name}, Cantidad vendida: {quantity}, Precio Producto: {price} \n' |
| 40 | + writeFile('venta.txt',newProduct) |
| 41 | + |
| 42 | +# addProduct() |
| 43 | + |
| 44 | +def readFile(nameFile): |
| 45 | + with open(nameFile,'r')as file: |
| 46 | + print(file.read()) |
| 47 | + |
| 48 | +#readFile('venta.txt') |
| 49 | + |
| 50 | +def update(nameFile,nombre_producto,nuevo_nombre, nueva_cantidad, nuevo_precio): |
| 51 | + with open(nameFile,'r')as file: |
| 52 | + lineas = file.readlines() |
| 53 | + for i, linea in enumerate(lineas): |
| 54 | + if nombre_producto in linea: |
| 55 | + partes = linea.split(", ") |
| 56 | + partes[0] = f'Nombre: {nuevo_nombre}' |
| 57 | + partes[1] = f'Cantidad : {nueva_cantidad}' |
| 58 | + partes[2] = f'Precio: {nuevo_precio}' |
| 59 | + lineas[i] = ", ".join(partes) |
| 60 | + break |
| 61 | + with open(nameFile, 'w') as file: |
| 62 | + file.writelines(lineas) |
| 63 | + print(f'producto {nuevo_nombre} actualizado con éxito') |
| 64 | + |
| 65 | +#update('venta.txt') |
| 66 | + |
| 67 | +def calcularVenta(namefile,productName): |
| 68 | + |
| 69 | + with open(namefile, 'r') as file: |
| 70 | + lineas = file.readlines() |
| 71 | + |
| 72 | + for linea in lineas: |
| 73 | + if productName in linea: |
| 74 | + partes = linea.split(", ") |
| 75 | + |
| 76 | + # Validamos que las partes tengan el formato correcto |
| 77 | + if len(partes) == 3: |
| 78 | + # Extraer la cantidad |
| 79 | + cantidad_str = partes[1].split(": ") |
| 80 | + if len(cantidad_str) == 2: |
| 81 | + cantidad = int(cantidad_str[1]) # Convertir a entero |
| 82 | + else: |
| 83 | + print("Formato de cantidad incorrecto en la línea:", linea) |
| 84 | + continue |
| 85 | + |
| 86 | + # Extraer el precio |
| 87 | + precio_str = partes[2].split(": ") |
| 88 | + if len(precio_str) == 2: |
| 89 | + precio = float(precio_str[1]) # Convertir a flotante |
| 90 | + else: |
| 91 | + print("Formato de precio incorrecto en la línea:", linea) |
| 92 | + continue |
| 93 | + # Calcular el total |
| 94 | + resultado = cantidad * precio |
| 95 | + return resultado |
| 96 | + print(f'Producto {productName} no encontrado.') |
| 97 | + |
| 98 | +def NameProduct(fileName): |
| 99 | + list_name_products = [] |
| 100 | + with open(fileName, 'r') as file: |
| 101 | + lineas = file.readlines() |
| 102 | + |
| 103 | + for linea in lineas: |
| 104 | + partes = linea.split(", ") |
| 105 | + if len(partes) == 3: |
| 106 | + productName = partes[0].split(": ") |
| 107 | + if len(productName) == 2: |
| 108 | + nombres = productName[1] |
| 109 | + list_name_products.append(nombres) |
| 110 | + return list_name_products |
| 111 | + |
| 112 | + |
| 113 | +while True: |
| 114 | + print('\n') |
| 115 | + print('1. Añadir producto') |
| 116 | + print('2. Consultar producto') |
| 117 | + print('3. Actualizar producto') |
| 118 | + print('4. calcular venta total por poroducto') |
| 119 | + print('5. calcular venta total') |
| 120 | + print('6. Salir') |
| 121 | + print('\n') |
| 122 | + |
| 123 | + try: |
| 124 | + option = int(input('Ingrese la opción: ')) |
| 125 | + print('\n') |
| 126 | + |
| 127 | + if option == 1: |
| 128 | + addProduct() |
| 129 | + print('¿Producto agregado exitosamente!') |
| 130 | + elif option == 2: |
| 131 | + readFile('venta.txt') |
| 132 | + elif option == 3: |
| 133 | + |
| 134 | + name = input('Nombre del producto a actualizar: ') |
| 135 | + newName = input('Nombre: ') |
| 136 | + newQuantity = input('Cantidad: ') |
| 137 | + newPrice = input('Precio: ') |
| 138 | + update('venta.txt',name,newName,newQuantity,newPrice) |
| 139 | + |
| 140 | + elif option == 4: |
| 141 | + productName = input('Ingrese el nombre del producto a calcular: ') |
| 142 | + result = calcularVenta('venta.txt',productName) |
| 143 | + print(f'El total de ventas del producto {productName} es: {result}') |
| 144 | + |
| 145 | + elif option == 5: |
| 146 | + nombres = NameProduct("venta.txt") |
| 147 | + resultado = 0 |
| 148 | + for nombre in nombres: |
| 149 | + venta_producto = calcularVenta("venta.txt",nombre) |
| 150 | + resultado += venta_producto |
| 151 | + print(f'El monto total de los productos vendidos es de: {resultado}') |
| 152 | + |
| 153 | + elif option == 6: |
| 154 | + delete_file('venta.txt') |
| 155 | + else: |
| 156 | + print('Solo puedes seleccionar las opciones del 1 al 7 por foavor ingrese nuevamente: ') |
| 157 | + except ValueError as e: |
| 158 | + print('Ingrese solamente los numeros del 1 - 6') |
0 commit comments