|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const readline = require('readline'); |
| 5 | + |
| 6 | +// Definición de las funciones principales del programa |
| 7 | +const ventas = { |
| 8 | + archivo: 'ventas.txt', |
| 9 | + |
| 10 | + async agregarProducto(producto) { |
| 11 | + await appendToFile(this.archivo, `${producto.nombre}, ${producto.cantidad}, ${producto.precio}\n`); |
| 12 | + }, |
| 13 | + |
| 14 | + async consultarProductos() { |
| 15 | + const data = await readFile(this.archivo); |
| 16 | + console.log(data); |
| 17 | + }, |
| 18 | + |
| 19 | + async actualizarProducto(nombre, cantidad, precio) { |
| 20 | + const tempFile = 'temporal.txt'; |
| 21 | + const readStream = fs.createReadStream(this.archivo); |
| 22 | + const writeStream = fs.createWriteStream(tempFile); |
| 23 | + const rl = readline.createInterface({ |
| 24 | + input: readStream, |
| 25 | + output: writeStream |
| 26 | + }); |
| 27 | + |
| 28 | + for await (const line of rl) { |
| 29 | + const [nombreProducto, cantidadStr, precioStr] = line.split(','); |
| 30 | + if (nombreProducto.trim() === nombre) { |
| 31 | + writeStream.write(`${nombre}, ${cantidad}, ${precio}\n`); |
| 32 | + } else { |
| 33 | + writeStream.write(`${line}\n`); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + await new Promise(resolve => { |
| 38 | + readStream.close(); |
| 39 | + writeStream.close(); |
| 40 | + fs.unlink(this.archivo, () => { |
| 41 | + fs.rename(tempFile, this.archivo, resolve); |
| 42 | + }); |
| 43 | + }); |
| 44 | + }, |
| 45 | + |
| 46 | + async eliminarProducto(nombre) { |
| 47 | + const tempFile = 'temporal.txt'; |
| 48 | + const readStream = fs.createReadStream(this.archivo); |
| 49 | + const writeStream = fs.createWriteStream(tempFile); |
| 50 | + const rl = readline.createInterface({ |
| 51 | + input: readStream, |
| 52 | + output: writeStream |
| 53 | + }); |
| 54 | + |
| 55 | + for await (const line of rl) { |
| 56 | + const [nombreProducto, , ] = line.split(','); |
| 57 | + if (nombreProducto.trim() !== nombre) { |
| 58 | + writeStream.write(`${line}\n`); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + await new Promise(resolve => { |
| 63 | + readStream.close(); |
| 64 | + writeStream.close(); |
| 65 | + fs.unlink(this.archivo, () => { |
| 66 | + fs.rename(tempFile, this.archivo, resolve); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }, |
| 70 | + |
| 71 | + async calcularVentaTotal() { |
| 72 | + const data = await readFile(this.archivo); |
| 73 | + const lines = data.split('\n'); |
| 74 | + let total = 0; |
| 75 | + for (const line of lines) { |
| 76 | + const [cantidadStr, precioStr] = line.split(','); |
| 77 | + const cantidad = parseInt(cantidadStr.trim()); |
| 78 | + const precio = parseFloat(precioStr.trim()); |
| 79 | + total += cantidad * precio; |
| 80 | + } |
| 81 | + return total; |
| 82 | + }, |
| 83 | + |
| 84 | + async calcularVentaPorProducto(nombre) { |
| 85 | + const data = await readFile(this.archivo); |
| 86 | + const lines = data.split('\n'); |
| 87 | + for (const line of lines) { |
| 88 | + const [nombreProducto, cantidadStr, precioStr] = line.split(','); |
| 89 | + if (nombreProducto.trim() === nombre) { |
| 90 | + const cantidad = parseInt(cantidadStr.trim()); |
| 91 | + const precio = parseFloat(precioStr.trim()); |
| 92 | + return cantidad * precio; |
| 93 | + } |
| 94 | + } |
| 95 | + return 0; |
| 96 | + }, |
| 97 | + |
| 98 | + async salir() { |
| 99 | + await fs.promises.unlink(this.archivo); |
| 100 | + console.log("Archivo borrado. Saliendo del programa..."); |
| 101 | + process.exit(0); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +// Funciones de utilidad |
| 106 | +async function appendToFile(filename, data) { |
| 107 | + await fs.promises.appendFile(filename, data); |
| 108 | +} |
| 109 | + |
| 110 | +async function readFile(filename) { |
| 111 | + return await fs.promises.readFile(filename, 'utf8'); |
| 112 | +} |
| 113 | + |
| 114 | +// Lógica principal del programa |
| 115 | +async function main() { |
| 116 | + console.log("===== Gestión de Ventas ====="); |
| 117 | + console.log("1. Añadir producto"); |
| 118 | + console.log("2. Consultar productos"); |
| 119 | + console.log("3. Actualizar producto"); |
| 120 | + console.log("4. Eliminar producto"); |
| 121 | + console.log("5. Calcular venta total"); |
| 122 | + console.log("6. Calcular venta por producto"); |
| 123 | + console.log("7. Salir"); |
| 124 | + |
| 125 | + const rl = readline.createInterface({ |
| 126 | + input: process.stdin, |
| 127 | + output: process.stdout |
| 128 | + }); |
| 129 | + |
| 130 | + rl.question("Seleccione una opción: ", async (opcion) => { |
| 131 | + switch (opcion) { |
| 132 | + case '1': |
| 133 | + const producto = await obtenerDatosProducto(); |
| 134 | + await ventas.agregarProducto(producto); |
| 135 | + break; |
| 136 | + case '2': |
| 137 | + await ventas.consultarProductos(); |
| 138 | + break; |
| 139 | + case '3': |
| 140 | + const { nombre, cantidad, precio } = await obtenerDatosProducto(); |
| 141 | + await ventas.actualizarProducto(nombre, cantidad, precio); |
| 142 | + break; |
| 143 | + case '4': |
| 144 | + const nombreProductoEliminar = await obtenerNombreProducto(); |
| 145 | + await ventas.eliminarProducto(nombreProductoEliminar); |
| 146 | + break; |
| 147 | + case '5': |
| 148 | + console.log("Venta total: $" + await ventas.calcularVentaTotal()); |
| 149 | + break; |
| 150 | + case '6': |
| 151 | + const nombreProductoVenta = await obtenerNombreProducto(); |
| 152 | + console.log("Venta de " + nombreProductoVenta + ": $" + await ventas.calcularVentaPorProducto(nombreProductoVenta)); |
| 153 | + break; |
| 154 | + case '7': |
| 155 | + await ventas.salir(); |
| 156 | + break; |
| 157 | + default: |
| 158 | + console.log("Opción no válida"); |
| 159 | + break; |
| 160 | + } |
| 161 | + rl.close(); |
| 162 | + }); |
| 163 | +} |
| 164 | + |
| 165 | +async function obtenerDatosProducto() { |
| 166 | + return new Promise(resolve => { |
| 167 | + const producto = {}; |
| 168 | + const rl = readline.createInterface({ |
| 169 | + input: process.stdin, |
| 170 | + output: process.stdout |
| 171 | + }); |
| 172 | + rl.question("Nombre del producto: ", (nombre) => { |
| 173 | + rl.question("Cantidad vendida: ", (cantidad) => { |
| 174 | + rl.question("Precio: ", (precio) => { |
| 175 | + rl.close(); |
| 176 | + resolve({ nombre, cantidad: parseInt(cantidad), precio: parseFloat(precio) }); |
| 177 | + }); |
| 178 | + }); |
| 179 | + }); |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +async function obtenerNombreProducto() { |
| 184 | + return new Promise(resolve => { |
| 185 | + const rl = readline.createInterface({ |
| 186 | + input: process.stdin, |
| 187 | + output: process.stdout |
| 188 | + }); |
| 189 | + rl.question("Nombre del producto: ", (nombre) => { |
| 190 | + rl.close(); |
| 191 | + resolve(nombre); |
| 192 | + }); |
| 193 | + }); |
| 194 | +} |
| 195 | + |
| 196 | +// Ejecución del programa |
| 197 | +main().catch(error => console.error(error)); |
| 198 | + |
0 commit comments