Skip to content

Commit 1c9d7c1

Browse files
committed
#11 - TypeScript
1 parent 7bb5cd1 commit 1c9d7c1

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import * as fs from 'fs';
2+
import * as readline from 'readline';
3+
4+
const githubUser = 'Sac-Corts';
5+
const fileName = `${githubUser}.txt`;
6+
const data = `Name: Isaac\nAge: 22\nProgramming Language: TypeScript`;
7+
8+
fs.writeFileSync(fileName, data, 'utf8');
9+
console.log('File created and data written');
10+
11+
const fileContent = fs.readFileSync(fileName, 'utf8');
12+
console.log('File content:');
13+
console.log(fileContent);
14+
15+
fs.unlinkSync(fileName);
16+
console.log(`File ${fileName} deleted`);
17+
18+
// ** Extra Exercise ** //
19+
const file = 'products.txt';
20+
21+
const readData = (): string[] => {
22+
try {
23+
const data = fs.readFileSync(file, 'utf8');
24+
return data.split('\n').filter(line => line.trim() !== '');
25+
} catch (err) {
26+
return [];
27+
}
28+
};
29+
30+
const writeData = (data: string[]): void => {
31+
fs.writeFileSync(file, data.join('\n'), 'utf8');
32+
};
33+
34+
const addProduct = (name: string, quantity: string, price: string): void => {
35+
const data = readData();
36+
data.push(`${name}, ${quantity}, ${price}`);
37+
writeData(data);
38+
console.log('Product added successfully.');
39+
};
40+
41+
const viewProduct = (name: string): void => {
42+
const data = readData();
43+
const product = data.find(line => line.startsWith(name));
44+
if (product) {
45+
console.log(`Product found: ${product}`);
46+
} else {
47+
console.log('Product not found.');
48+
}
49+
};
50+
51+
const updateProduct = (name: string, quantity: string, price: string): void => {
52+
const data = readData();
53+
const index = data.findIndex(line => line.startsWith(name));
54+
if (index !== -1) {
55+
data[index] = `${name}, ${quantity}, ${price}`;
56+
writeData(data);
57+
console.log('Product updated successfully.');
58+
} else {
59+
console.log('Product not found.');
60+
}
61+
};
62+
63+
const deleteProduct = (name: string): void => {
64+
const data = readData();
65+
const newData = data.filter(line => !line.startsWith(name));
66+
if (data.length !== newData.length) {
67+
writeData(newData);
68+
console.log('Product deleted successfully.');
69+
} else {
70+
console.log('Product not found.');
71+
}
72+
};
73+
74+
const calculateTotalSales = (): void => {
75+
const data = readData();
76+
let total = 0;
77+
data.forEach(line => {
78+
const [, quantity, price] = line.split(', ');
79+
total += parseInt(quantity) * parseFloat(price);
80+
});
81+
console.log(`Total sales: ${total.toFixed(2)}`);
82+
};
83+
84+
const calculateProductSales = (name: string): void => {
85+
const data = readData();
86+
const product = data.find(line => line.startsWith(name));
87+
if (product) {
88+
const [, quantity, price] = product.split(', ');
89+
const sales = parseInt(quantity) * parseFloat(price);
90+
console.log(`Sales for ${name}: ${sales.toFixed(2)}`);
91+
} else {
92+
console.log('Product not found.');
93+
}
94+
};
95+
96+
const clearFile = (): void => {
97+
if (fs.existsSync(file)) {
98+
fs.unlinkSync(file);
99+
console.log('File cleared.');
100+
} else {
101+
console.log('File does not exist.');
102+
}
103+
};
104+
105+
const rl = readline.createInterface({
106+
input: process.stdin,
107+
output: process.stdout
108+
});
109+
110+
const main = (): void => {
111+
rl.question('Choose an option: (add/view/update/delete/total/individual/exit) ', (option) => {
112+
switch (option.toLowerCase()) {
113+
case 'add':
114+
rl.question('Enter product name: ', (name) => {
115+
rl.question('Enter quantity sold: ', (quantity) => {
116+
rl.question('Enter price: ', (price) => {
117+
addProduct(name, quantity, price);
118+
main();
119+
});
120+
});
121+
});
122+
break;
123+
case 'view':
124+
rl.question('Enter product name: ', (name) => {
125+
viewProduct(name);
126+
main();
127+
});
128+
break;
129+
case 'update':
130+
rl.question('Enter product name: ', (name) => {
131+
rl.question('Enter new quantity sold: ', (quantity) => {
132+
rl.question('Enter new price: ', (price) => {
133+
updateProduct(name, quantity, price);
134+
main();
135+
});
136+
});
137+
});
138+
break;
139+
case 'delete':
140+
rl.question('Enter product name: ', (name) => {
141+
deleteProduct(name);
142+
main();
143+
});
144+
break;
145+
case 'total':
146+
calculateTotalSales();
147+
main();
148+
break;
149+
case 'individual':
150+
rl.question('Enter product name: ', (name) => {
151+
calculateProductSales(name);
152+
main();
153+
});
154+
break;
155+
case 'exit':
156+
clearFile();
157+
rl.close();
158+
break;
159+
default:
160+
console.log('Invalid option. Please try again.');
161+
main();
162+
break;
163+
}
164+
});
165+
};
166+
167+
main();

0 commit comments

Comments
 (0)