Skip to content

Commit b60636e

Browse files
committed
#11 - javascript
1 parent 4074169 commit b60636e

File tree

1 file changed

+252
-12
lines changed

1 file changed

+252
-12
lines changed

Roadmap/11 - MANEJO DE FICHEROS/javascript/DAVstudy.js

+252-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
// // Importar modulo
2626

2727
const fs = require("fs");
28-
28+
const readline = require("readline");
29+
const read = readline.createInterface({
30+
input: process.stdin,
31+
output: process.stdout,
32+
});
2933
// // Crear archivo
3034
// fs.writeFile("DAVstudy.txt", "", (err) => {
3135
// if (err) throw err;
@@ -70,18 +74,44 @@ const myEcommerce = {
7074
currentQuantitySold: 0,
7175
currentPrice: 0,
7276
lastLineAdd: "",
73-
app: function (params) {},
7477
createFile: function (nameFile) {
7578
this.nameFile = `${nameFile}.txt`;
76-
fs.writeFileSync(
77-
this.nameFile,
78-
"nombre_producto,cantidad_vendida,precio\n"
79-
);
79+
fs.writeFileSync(this.nameFile, "nombre_producto,cantidad_vendida,precio");
80+
return "Archivo Creado";
8081
},
81-
saveNewProduct: function (newLine) {
82-
fs.appendFileSync(this.nameFile, newLine);
82+
addProduct: function (newLine) {
83+
fs.appendFileSync(this.nameFile, "\n" + newLine);
84+
return "Nuevo producto agregado";
85+
},
86+
updateProduct: function (
87+
product,
88+
newProduct = "",
89+
quantitySold = "",
90+
price = ""
91+
) {
92+
const dataUpdate = [];
93+
const data = fs.readFileSync(this.nameFile, "utf8");
94+
const lines = data.split("\n");
95+
for (let i = 0; i < lines.length; i++) {
96+
const line = lines[i].split(",");
97+
if (line[0] === product) {
98+
if (newProduct !== "") {
99+
line[0] = newProduct;
100+
}
101+
if (quantitySold !== "") {
102+
line[1] = quantitySold;
103+
}
104+
if (price !== "") {
105+
line[2] = price;
106+
}
107+
dataUpdate.push(line.join(","));
108+
} else {
109+
dataUpdate.push(lines[i]);
110+
}
111+
}
112+
fs.writeFileSync(this.nameFile, dataUpdate.join("\n"));
113+
return "Archivo actualizado";
83114
},
84-
updateProduct: function (product) {},
85115
findProduct: function (product) {
86116
const data = fs.readFileSync(this.nameFile, "utf8");
87117
const lines = data.split("\n");
@@ -92,13 +122,223 @@ const myEcommerce = {
92122
this.currentQuantitySold = line[1];
93123
this.currentPrice = line[2];
94124
this.indexCurrentProduct = i;
125+
return "Producto encontrado";
95126
}
96127
}
128+
return "El producto no existe";
129+
},
130+
deleteProduct: function (product) {
131+
const dataUpdate = [];
132+
const data = fs.readFileSync(this.nameFile, "utf8");
133+
const lines = data.split("\n");
134+
for (let i = 0; i < lines.length; i++) {
135+
const line = lines[i].split(",");
136+
if (line[0] === product) {
137+
continue;
138+
} else {
139+
dataUpdate.push(lines[i]);
140+
}
141+
}
142+
fs.writeFileSync(this.nameFile, dataUpdate.join("\n"));
143+
return "Producto eliminado";
144+
},
145+
calculateSells: function (product) {
146+
this.findProduct(product);
147+
return Number(this.currentQuantitySold) * Number(this.currentPrice);
97148
},
98149
};
99150

151+
// console.log(myEcommerce.createFile("product-solds-price"));
152+
// console.log(myEcommerce.addProduct("\nToalla,2000,100"));
153+
// console.log(myEcommerce.addProduct("\nCartas,400,100"));
154+
// console.log(myEcommerce.findProduct("toalla"));
155+
// console.log(myEcommerce.currentProduct);
156+
// console.log(myEcommerce.updateProduct("Toalla", "Toallas", "10", "100"));
157+
// console.log(myEcommerce.deleteProduct("Cartas"));
158+
// console.log(myEcommerce.calculateSells("Toallas"));
159+
100160

161+
myEcommerce.createFile('data');
162+
app = () => {
163+
164+
read.question(
165+
`Ecommerce:
166+
1. Agregar producto
167+
2. Actualizar producto
168+
3. Buscar producto
169+
4. Cantidad de ventas de un producto
170+
5. Eliminar producto
171+
0. Cerrar (elimina el archivo)
172+
Ingrese su opcion: `,
173+
(option = (option) => {
174+
switch (option) {
175+
case "1":
176+
read.question(`Ingrese el nombre del producto: `, (name) => {
177+
read.question(`Ingrese la cantidad de ventas: `, (quantitySold) => {
178+
read.question(`Ingrese el precio: `, (price) => {
179+
const line = [name, quantitySold, price]
180+
console.log(myEcommerce.addProduct(line.join(',')))
181+
app();
182+
});
183+
});
184+
});
185+
break;
186+
case "2":
187+
read.question(
188+
`Ingrese el nombre del producto a modificar`,
189+
(name) => {
190+
read.question(`Desea cambiar el nombre? `, (answer) => {
191+
answer.toLowerCase() === "si"
192+
? read.question(`Ingrese el nuevo nombre: `, (newName) =>
193+
read.question(
194+
`Desea cambiar la cantidad de ventas? `,
195+
(answer) => {
196+
answer.toLowerCase() === "si"
197+
? read.question(
198+
`Ingrese la cantidad de ventas: `,
199+
(newQuantity) =>
200+
read.question(
201+
`Desea cambiar el precio? `,
202+
(answer) => {
203+
answer.toLowerCase() === "si"
204+
? read.question(
205+
`Ingrese el precio: `,
206+
(newPrice) => {
207+
console.log(
208+
myEcommerce.updateProduct(
209+
name,
210+
newName,
211+
newQuantity,
212+
newPrice
213+
)
214+
)
215+
})
216+
: console.log(
217+
myEcommerce.updateProduct(
218+
name,
219+
newName,
220+
newQuantity
221+
)
222+
);
223+
}
224+
)
225+
)
226+
: read.question(
227+
`Desea cambiar el precio? `,
228+
(answer) => {
229+
answer.toLowerCase() === "si"
230+
? read.question(
231+
`Ingrese el precio: `,
232+
(newPrice) =>
233+
console.log(
234+
myEcommerce.updateProduct(
235+
name,
236+
newName,
237+
"",
238+
newPrice
239+
)
240+
)
241+
)
242+
: console.log(
243+
myEcommerce.updateProduct(
244+
name,
245+
newName,
246+
"",
247+
""
248+
)
249+
);
250+
}
251+
);
252+
}
253+
)
254+
)
255+
: read.question(
256+
`Desea cambiar la cantidad de ventas? `,
257+
(answer) => {
258+
answer.toLowerCase() === "si"
259+
? read.question(
260+
`Ingrese la cantidad de ventas: `,
261+
(newQuantity) =>
262+
read.question(
263+
`Desea cambiar el precio? `,
264+
(answer) => {
265+
answer.toLowerCase() === "si"
266+
? read.question(
267+
`Ingrese el precio: `,
268+
(newPrice) =>
269+
console.log(
270+
myEcommerce.updateProduct(
271+
name,
272+
"",
273+
newQuantity,
274+
newPrice
275+
)
276+
)
277+
)
278+
: console.log(
279+
myEcommerce.updateProduct(
280+
name,
281+
"",
282+
newQuantity
283+
)
284+
);
285+
}
286+
)
287+
)
288+
: read.question(
289+
`Desea cambiar el precio? `,
290+
(answer) => {
291+
answer.toLowerCase() === "si"
292+
? read.question(
293+
`Ingrese el precio: `,
294+
(newPrice) =>
295+
console.log(
296+
myEcommerce.updateProduct(
297+
name,
298+
"",
299+
"",
300+
newPrice
301+
)
302+
)
303+
)
304+
: console.log("No hubo ningun cambio");
305+
}
306+
);
307+
}
308+
);
309+
});
310+
}
311+
);
312+
app();
313+
break;
314+
case '3':
315+
read.question(`Ingrese el nombre del producto: `, (name) => {
316+
console.log(myEcommerce.findProduct(name))
317+
app();
318+
});
319+
break;
320+
case '4':
321+
read.question(`Ingrese el nombre del producto: `, (name) => {
322+
console.log(myEcommerce.calculateSells(name))
323+
app();
324+
});
325+
break;
326+
case '5':
327+
read.question(`Ingrese el nombre del producto: `, (name) => {
328+
console.log(myEcommerce.deleteProduct(name))
329+
app();
330+
});
331+
break;
332+
case '0':
333+
fs.unlinkSync(myEcommerce.nameFile)
334+
read.close()
335+
break;
336+
default:
337+
app();
338+
break;
339+
}
340+
})
341+
);
342+
};
101343

102-
console.log(myEcommerce.createFile('product-solds-price'))
103-
console.log(myEcommerce.saveNewProduct('toalla,2000,100\n'))
104-
console.log(myEcommerce.saveNewProduct('cartas,400,100\n'))
344+
app();

0 commit comments

Comments
 (0)