1+ const fs = require ( 'fs' ) ;
2+
3+ // let nombre = 'jeronimocardu';
4+ // let archivo = `${nombre}.txt`;
5+
6+ // let datos = [
7+ // "nombre: Jeronimo Cardu",
8+ // "edad: 20 años",
9+ // "lenguaje: JavaScript"
10+ // ].join('\n');
11+
12+ // // Añado el contenido
13+ // fs.writeFileSync(archivo, datos);
14+
15+ // // Imprimo el contenido
16+ // console.log(fs.readFileSync(archivo, 'utf-8'));
17+
18+ // // Borro el archivo
19+ // fs.unlinkSync(archivo);
20+ // console.log(`\n${archivo} ha sido eliminado con exito`);
21+
22+ //////////////////////////////////////////////////////////////
23+ // Extra
24+ let salesFile = 'ventas.txt' ;
25+ let products = [ ] ;
26+
27+ let readline = require ( 'readline' ) . createInterface ( {
28+ input : process . stdin ,
29+ output : process . stdout ,
30+ } ) ;
31+
32+ function addProduct ( ) {
33+ readline . question ( '\n¿Qué producto desea agregar? ' , nameProduct => {
34+ for ( let i of products ) {
35+ if ( i . name === nameProduct ) {
36+ console . log ( 'Este producto ya existe' ) ;
37+ saveProductsToFile ( ) ;
38+ return ;
39+ }
40+ }
41+ readline . question ( `\n¿Cuál es la cantidad de ${ nameProduct } que se vendieron? ` , soldProducts => {
42+ readline . question ( `\n¿Cuál es el precio unitario de cada ${ nameProduct } ? ` , price => {
43+ products . unshift ( { name : nameProduct , sold : parseInt ( soldProducts ) , unitPrice : parseInt ( price ) } ) ;
44+ console . log ( `\nLos datos de '${ nameProduct } ' se han agregado correctamente.` ) ;
45+ saveProductsToFile ( ) ;
46+ } )
47+ } )
48+ } )
49+ }
50+ function consult ( ) {
51+ if ( products . length > 0 ) {
52+ readline . question ( '\nIngrese el nombre del producto que desea ver los datos: ' , nameProduct => {
53+ for ( let i of products ) {
54+ if ( i . name === nameProduct ) {
55+ console . log ( `\nName: ${ nameProduct } \nQuantity Sold: ${ i . sold } \nUnit Price: $${ i . unitPrice } ` ) ;
56+ saveProductsToFile ( ) ;
57+ return ;
58+ }
59+ }
60+ console . log ( '\nProducto no encontrado' ) ;
61+ saveProductsToFile ( ) ;
62+ } )
63+ } else console . log ( '\nNo hay productos registrados todavia.' ) ;
64+ saveProductsToFile ( ) ;
65+ }
66+ function update ( ) {
67+ let = found = false ;
68+ if ( products . length > 0 ) {
69+ readline . question ( '\n¿Qué producto quiere modificar? ' , nameProduct => {
70+ for ( let i of products ) {
71+ if ( i . name === nameProduct ) {
72+ found = true ;
73+ readline . question ( '\nIngrese el nombre nuevo del producto (si no lo quiere cambiar presione ENTER): ' , newNameProduct => {
74+ for ( let i of products ) {
75+ if ( i . name === newNameProduct ) {
76+ console . log ( '\nNombre no disponible' ) ;
77+ break ;
78+ }
79+ }
80+ if ( newNameProduct !== '' ) i . name = newNameProduct ;
81+ readline . question ( `\n¿Qué cantidad de ${ i . name } se vendieron? (si no lo quiere cambiar presione ENTER) ` , soldProducts => {
82+ if ( soldProducts !== '' ) i . sold = parseInt ( soldProducts ) ;
83+ readline . question ( `\n¿Cuál es el precio unitario de cada ${ i . name } ? (si no lo quiere cambiar presione ENTER) ` , price => {
84+ if ( price !== '' ) i . unitPrice = parseInt ( price ) ;
85+ console . log ( '\nProducto actualizado con exito!' ) ;
86+ saveProductsToFile ( ) ;
87+ return ;
88+ } )
89+ } )
90+ } )
91+ }
92+ break ;
93+ }
94+ if ( ! found ) console . log ( '\nProducto no encontrado' ) ;
95+ saveProductsToFile ( ) ;
96+ } )
97+ } else console . log ( '\nNo hay productos registrados todavia.' ) ;
98+ saveProductsToFile ( ) ;
99+ }
100+ function removeProduct ( ) {
101+ if ( products . length > 0 ) {
102+ readline . question ( '\n¿Cuál es el nombre del producto que quiere eliminar? ' , nameProduct => {
103+ for ( let i of products ) {
104+ if ( i . name === nameProduct ) {
105+ found = true ;
106+ products . splice ( products . indexOf ( i ) , 1 ) ;
107+ console . log ( '\nProducto eliminado con exito!' ) ;
108+ saveProductsToFile ( ) ;
109+ return ;
110+ }
111+ }
112+ console . log ( '\nProducto no encontrado' ) ;
113+ saveProductsToFile ( ) ;
114+ } )
115+ } else {
116+ console . log ( '\nNo hay productos registrados todavia.' ) ;
117+ saveProductsToFile ( ) ;
118+ }
119+ }
120+ function showProducts ( ) {
121+ if ( products . length > 0 ) {
122+ const content = products . map ( i => `Name: ${ i . name } | Quantity Sold: ${ i . sold } | Unit Price: $${ i . unitPrice } ` ) . join ( '\n' ) ;
123+ console . log ( content ) ;
124+ } else {
125+ console . log ( '\nNo hay productos registrados todavia.' ) ;
126+ }
127+ saveProductsToFile ( ) ;
128+ }
129+ function totalSales ( ) {
130+ let suma = 0 ;
131+ if ( products . length > 0 ) {
132+ for ( let i of products ) {
133+ suma += i . sold * i . unitPrice ;
134+ }
135+ console . log ( '\nHay una venta total de $' , suma ) ;
136+ } else console . log ( '\nNo hay productos todavia' ) ;
137+ saveProductsToFile ( ) ;
138+ }
139+ function productsSales ( ) {
140+ if ( products . length > 0 ) {
141+ readline . question ( '\n¿De que producto deseas ver la venta? ' , nameProduct => {
142+ let suma = 0 ;
143+ for ( let i of products ) {
144+ if ( i . name === nameProduct ) {
145+ suma = i . sold * i . unitPrice ;
146+ console . log ( `${ nameProduct } recaudó en total $${ suma } ` ) ;
147+ saveProductsToFile ( ) ;
148+ return ;
149+ }
150+ }
151+ console . log ( '\nProducto no encontrado' ) ;
152+ } )
153+ } else console . log ( '\nNo hay productos todavia' ) ;
154+ saveProductsToFile ( ) ;
155+ }
156+ function saveProductsToFile ( ) {
157+ const content = products . map ( i => `Name: ${ i . name } | Quantity Sold: ${ i . sold } | Unit Price: $${ i . unitPrice } ` ) . join ( '\n' ) ;
158+ fs . writeFileSync ( salesFile , content ) ;
159+ readline . question ( '\nPrecione ENTER para continuar...' , tecla => {
160+ if ( tecla === '' ) management ( ) ;
161+ else saveProductsToFile ( ) ;
162+ } )
163+ }
164+ function management ( ) {
165+ console . clear ( ) ;
166+ console . log ( `
167+ \t\t/// INGRESE UNA OPCIÓN ///\n
168+ 1. Añadir producto.
169+ 2. Consultar datos de productos.
170+ 3. Actualizar datos de productos.
171+ 4. Eliminar productos.
172+ 5. Ver productos.
173+ 6. Ver total.
174+ 7. Ver total de X producto.
175+ 8. Salir ( Elimina el archivo )` ) ;
176+ readline . question ( '\nIngrese una opción: ' , op => {
177+ switch ( op ) {
178+ case '1' :
179+ addProduct ( ) ;
180+ break ;
181+ case '2' :
182+ consult ( ) ;
183+ break ;
184+ case '3' :
185+ update ( ) ;
186+ break ;
187+ case '4' :
188+ removeProduct ( ) ;
189+ break ;
190+ case '5' :
191+ showProducts ( ) ;
192+ break ;
193+ case '6' :
194+ totalSales ( ) ;
195+ break ;
196+ case '7' :
197+ productsSales ( ) ;
198+ break ;
199+ case '8' :
200+ fs . unlinkSync ( salesFile ) ;
201+ readline . close ( ) ;
202+ return ;
203+ default :
204+ console . log ( '\nOpción no valida' ) ;
205+ saveProductsToFile ( ) ;
206+ }
207+ } )
208+ }
209+ management ( ) ;
0 commit comments