1
+ const fs = require ( 'fs' ) . promises ;
2
+ const readline = require ( 'readline' ) ;
3
+
4
+ // Constantes para los nombres de archivo
5
+ const FILENAME = 'info_personal.txt' ;
6
+ const SALES_FILENAME = 'ventas.txt' ;
7
+
8
+ // Configuración de la interfaz de lectura
9
+ const rl = readline . createInterface ( {
10
+ input : process . stdin ,
11
+ output : process . stdout
12
+ } ) ;
13
+
14
+ // Función para preguntar al usuario
15
+ const pregunta = ( pregunta ) => {
16
+ return new Promise ( ( resolve ) => {
17
+ rl . question ( pregunta , resolve ) ;
18
+ } ) ;
19
+ } ;
20
+
21
+ // Función principal
22
+ async function main ( ) {
23
+ await crearArchivoPersonal ( ) ;
24
+ await gestionVentas ( ) ;
25
+ }
26
+
27
+ // Función para crear y manipular el archivo personal
28
+ async function crearArchivoPersonal ( ) {
29
+ try {
30
+ // Crear y escribir en el archivo
31
+ await fs . writeFile ( FILENAME , 'Nombre: Juan\nEdad: 30\nLenguaje de programación favorito: JavaScript' ) ;
32
+ console . log ( 'Archivo creado y escrito con éxito.' ) ;
33
+
34
+ // Leer y mostrar el contenido
35
+ const contenido = await fs . readFile ( FILENAME , 'utf8' ) ;
36
+ console . log ( 'Contenido del archivo:' ) ;
37
+ console . log ( contenido ) ;
38
+
39
+ // Borrar el archivo
40
+ await fs . unlink ( FILENAME ) ;
41
+ console . log ( 'Archivo borrado con éxito.' ) ;
42
+ } catch ( error ) {
43
+ console . error ( 'Error:' , error . message ) ;
44
+ }
45
+ }
46
+
47
+ // Función para la gestión de ventas
48
+ async function gestionVentas ( ) {
49
+ let salir = false ;
50
+
51
+ while ( ! salir ) {
52
+ console . log ( '\n--- Gestión de Ventas ---' ) ;
53
+ console . log ( '1. Añadir producto' ) ;
54
+ console . log ( '2. Consultar productos' ) ;
55
+ console . log ( '3. Actualizar producto' ) ;
56
+ console . log ( '4. Eliminar producto' ) ;
57
+ console . log ( '5. Calcular venta total' ) ;
58
+ console . log ( '6. Calcular venta por producto' ) ;
59
+ console . log ( '7. Salir' ) ;
60
+
61
+ const opcion = await pregunta ( 'Seleccione una opción: ' ) ;
62
+
63
+ switch ( opcion ) {
64
+ case '1' :
65
+ await anadirProducto ( ) ;
66
+ break ;
67
+ case '2' :
68
+ await consultarProductos ( ) ;
69
+ break ;
70
+ case '3' :
71
+ await actualizarProducto ( ) ;
72
+ break ;
73
+ case '4' :
74
+ await eliminarProducto ( ) ;
75
+ break ;
76
+ case '5' :
77
+ await calcularVentaTotal ( ) ;
78
+ break ;
79
+ case '6' :
80
+ await calcularVentaPorProducto ( ) ;
81
+ break ;
82
+ case '7' :
83
+ salir = true ;
84
+ await borrarArchivoVentas ( ) ;
85
+ rl . close ( ) ;
86
+ break ;
87
+ default :
88
+ console . log ( 'Opción no válida.' ) ;
89
+ }
90
+ }
91
+ }
92
+
93
+ // Función para añadir un producto
94
+ async function anadirProducto ( ) {
95
+ const nombre = await pregunta ( 'Nombre del producto: ' ) ;
96
+ const cantidad = await pregunta ( 'Cantidad vendida: ' ) ;
97
+ const precio = await pregunta ( 'Precio: ' ) ;
98
+
99
+ const linea = `${ nombre } , ${ cantidad } , ${ precio } \n` ;
100
+ await fs . appendFile ( SALES_FILENAME , linea ) ;
101
+ console . log ( 'Producto añadido con éxito.' ) ;
102
+ }
103
+
104
+ // Función para consultar productos
105
+ async function consultarProductos ( ) {
106
+ try {
107
+ const contenido = await fs . readFile ( SALES_FILENAME , 'utf8' ) ;
108
+ console . log ( '\nProductos:' ) ;
109
+ console . log ( contenido ) ;
110
+ } catch ( error ) {
111
+ if ( error . code === 'ENOENT' ) {
112
+ console . log ( 'No hay productos registrados.' ) ;
113
+ } else {
114
+ console . error ( 'Error al consultar productos:' , error . message ) ;
115
+ }
116
+ }
117
+ }
118
+
119
+ // Función para actualizar un producto
120
+ async function actualizarProducto ( ) {
121
+ const nombreActualizar = await pregunta ( 'Nombre del producto a actualizar: ' ) ;
122
+ const nuevaCantidad = await pregunta ( 'Nueva cantidad vendida: ' ) ;
123
+ const nuevoPrecio = await pregunta ( 'Nuevo precio: ' ) ;
124
+
125
+ try {
126
+ let contenido = await fs . readFile ( SALES_FILENAME , 'utf8' ) ;
127
+ let lineas = contenido . split ( '\n' ) ;
128
+ let encontrado = false ;
129
+
130
+ lineas = lineas . map ( linea => {
131
+ const [ nombre , ...resto ] = linea . split ( ', ' ) ;
132
+ if ( nombre === nombreActualizar ) {
133
+ encontrado = true ;
134
+ return `${ nombre } , ${ nuevaCantidad } , ${ nuevoPrecio } ` ;
135
+ }
136
+ return linea ;
137
+ } ) ;
138
+
139
+ if ( encontrado ) {
140
+ await fs . writeFile ( SALES_FILENAME , lineas . join ( '\n' ) ) ;
141
+ console . log ( 'Producto actualizado con éxito.' ) ;
142
+ } else {
143
+ console . log ( 'Producto no encontrado.' ) ;
144
+ }
145
+ } catch ( error ) {
146
+ console . error ( 'Error al actualizar producto:' , error . message ) ;
147
+ }
148
+ }
149
+
150
+ // Función para eliminar un producto
151
+ async function eliminarProducto ( ) {
152
+ const nombreEliminar = await pregunta ( 'Nombre del producto a eliminar: ' ) ;
153
+
154
+ try {
155
+ let contenido = await fs . readFile ( SALES_FILENAME , 'utf8' ) ;
156
+ let lineas = contenido . split ( '\n' ) ;
157
+ let encontrado = false ;
158
+
159
+ lineas = lineas . filter ( linea => {
160
+ const [ nombre ] = linea . split ( ', ' ) ;
161
+ if ( nombre === nombreEliminar ) {
162
+ encontrado = true ;
163
+ return false ;
164
+ }
165
+ return true ;
166
+ } ) ;
167
+
168
+ if ( encontrado ) {
169
+ await fs . writeFile ( SALES_FILENAME , lineas . join ( '\n' ) ) ;
170
+ console . log ( 'Producto eliminado con éxito.' ) ;
171
+ } else {
172
+ console . log ( 'Producto no encontrado.' ) ;
173
+ }
174
+ } catch ( error ) {
175
+ console . error ( 'Error al eliminar producto:' , error . message ) ;
176
+ }
177
+ }
178
+
179
+ // Función para calcular la venta total
180
+ async function calcularVentaTotal ( ) {
181
+ try {
182
+ const contenido = await fs . readFile ( SALES_FILENAME , 'utf8' ) ;
183
+ const lineas = contenido . split ( '\n' ) ;
184
+ let ventaTotal = 0 ;
185
+
186
+ lineas . forEach ( linea => {
187
+ if ( linea . trim ( ) !== '' ) {
188
+ const [ , cantidad , precio ] = linea . split ( ', ' ) ;
189
+ ventaTotal += parseFloat ( cantidad ) * parseFloat ( precio ) ;
190
+ }
191
+ } ) ;
192
+
193
+ console . log ( `Venta total: ${ ventaTotal . toFixed ( 2 ) } ` ) ;
194
+ } catch ( error ) {
195
+ console . error ( 'Error al calcular venta total:' , error . message ) ;
196
+ }
197
+ }
198
+
199
+ // Función para calcular la venta por producto
200
+ async function calcularVentaPorProducto ( ) {
201
+ const nombreProducto = await pregunta ( 'Nombre del producto: ' ) ;
202
+
203
+ try {
204
+ const contenido = await fs . readFile ( SALES_FILENAME , 'utf8' ) ;
205
+ const lineas = contenido . split ( '\n' ) ;
206
+ let encontrado = false ;
207
+
208
+ lineas . forEach ( linea => {
209
+ if ( linea . trim ( ) !== '' ) {
210
+ const [ nombre , cantidad , precio ] = linea . split ( ', ' ) ;
211
+ if ( nombre === nombreProducto ) {
212
+ const ventaProducto = parseFloat ( cantidad ) * parseFloat ( precio ) ;
213
+ console . log ( `Venta de ${ nombreProducto } : ${ ventaProducto . toFixed ( 2 ) } ` ) ;
214
+ encontrado = true ;
215
+ }
216
+ }
217
+ } ) ;
218
+
219
+ if ( ! encontrado ) {
220
+ console . log ( 'Producto no encontrado.' ) ;
221
+ }
222
+ } catch ( error ) {
223
+ console . error ( 'Error al calcular venta por producto:' , error . message ) ;
224
+ }
225
+ }
226
+
227
+ // Función para borrar el archivo de ventas
228
+ async function borrarArchivoVentas ( ) {
229
+ try {
230
+ await fs . unlink ( SALES_FILENAME ) ;
231
+ console . log ( 'Archivo de ventas borrado con éxito.' ) ;
232
+ } catch ( error ) {
233
+ if ( error . code === 'ENOENT' ) {
234
+ console . log ( 'El archivo de ventas no existe.' ) ;
235
+ } else {
236
+ console . error ( 'Error al borrar el archivo de ventas:' , error . message ) ;
237
+ }
238
+ }
239
+ }
240
+
241
+ // Ejecutar el programa principal
242
+ main ( ) . catch ( console . error ) ;
0 commit comments