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