Skip to content

Commit 50325af

Browse files
authored
#11 - Javascript
1 parent f95c63e commit 50325af

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// En package.json se debe agregar "type": "module"
2+
3+
import { open, unlink, access, constants } from 'node:fs/promises';
4+
import * as readline from 'node:readline/promises';
5+
import { stdin as input, stdout as output } from 'node:process';
6+
7+
async function manageFile() {
8+
try {
9+
console.log('Creando archivo de texto edalmava.txt...');
10+
const filehandle = await open('edalmava.txt', 'a');
11+
await filehandle.appendFile("Edalmava\n");
12+
await filehandle.appendFile("30\n");
13+
await filehandle.appendFile("JavaScript\n");
14+
await filehandle.close();
15+
16+
console.log('\nImprimiendo el contenido del archivo edalmava.txt:');
17+
const file = await open('edalmava.txt');
18+
for await (const line of file.readLines()) {
19+
console.log(line);
20+
}
21+
await file.close();
22+
23+
console.log('\nBorrando archivo de texto edalmava.txt...')
24+
await unlink('edalmava.txt');
25+
console.log('Borrado exitoso del archivo edalmava.txt');
26+
27+
await retoExtra();
28+
} catch (error) {
29+
console.error('Ha ocurrido un error:', error.message);
30+
}
31+
}
32+
33+
async function guardarVenta(rl2) {
34+
try {
35+
console.log('Añadir Venta');
36+
37+
const nombreProducto = await rl2.question('Nombre Producto: ');
38+
const cantidadVendida = await rl2.question('Cantidad Vendida: ');
39+
const precio = await rl2.question('Precio Producto: ');
40+
41+
const producto = [nombreProducto, cantidadVendida, precio, precio * cantidadVendida].join(',')
42+
43+
const filehandle = await open('ventas.txt', 'a');
44+
await filehandle.appendFile(`${producto}\n`);
45+
await filehandle.close();
46+
} catch(error) {
47+
console.error('Ha ocurrido un error:', error.message);
48+
}
49+
}
50+
51+
async function mostrarProductos() {
52+
try {
53+
await access('ventas.txt', constants.F_OK);
54+
console.log('\nVentas:\n');
55+
const file = await open('ventas.txt');
56+
for await (const line of file.readLines()) {
57+
console.log(line);
58+
}
59+
await file.close();
60+
} catch(error) {
61+
if (error.code === 'ENOENT') {
62+
console.error('Todavía no se han añadido productos al archivo de ventas');
63+
} else {
64+
console.error('Ha ocurrido un error:', error.message);
65+
}
66+
}
67+
}
68+
69+
async function retoExtra() {
70+
const rl = readline.createInterface({ input, output, terminal: false });
71+
try {
72+
73+
let opcion = '';
74+
75+
while (opcion !== '3') {
76+
console.log('\nMenú Principal\n');
77+
opcion = await rl.question('1. Añadir venta\n2. Consultar Ventas\n3. Salir\n\nEscoja su opción: ');
78+
switch (opcion) {
79+
case '1':
80+
await guardarVenta(rl);
81+
break;
82+
case '2':
83+
await mostrarProductos();
84+
break;
85+
case '3':
86+
console.log('Saliendo...')
87+
try {
88+
await access('ventas.txt', constants.F_OK);
89+
await unlink('ventas.txt');
90+
console.log('Archivo de ventas borrado');
91+
} catch(error) {
92+
if (error.code !== 'ENOENT') {
93+
console.error('Error al verificar o borrar el archivo de ventas:', error.message);
94+
}
95+
}
96+
97+
break;
98+
default:
99+
console.log('Opción no válida\n')
100+
}
101+
}
102+
103+
rl.close();
104+
} catch (error) {
105+
console.error('Ha ocurrido un error:', error.message);
106+
rl.close();
107+
}
108+
}
109+
110+
manageFile();

0 commit comments

Comments
 (0)