Skip to content

Commit 7fe2c6e

Browse files
author
qwik zgheib mossad bashit
committed
feat: #11 - Typescript
1 parent 75bdfd5 commit 7fe2c6e

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import readline from "readline-sync";
2+
import fs from "fs";
3+
import path from "path";
4+
5+
const createFile = (fileName: string, content: string): void => {
6+
fs.writeFile(fileName, content, (err) => {
7+
if (err) {
8+
console.error("Error writing file:", err);
9+
return;
10+
}
11+
console.log("File created successfully!");
12+
});
13+
};
14+
15+
const readFileContent = (fileName: string): void => {
16+
fs.readFile(fileName, "utf8", (err, data) => {
17+
if (err) {
18+
console.error("Error reading file:", err);
19+
return;
20+
}
21+
console.log("File content:", data);
22+
});
23+
};
24+
25+
const deleteFile = (fileName: string): void => {
26+
fs.unlink(fileName, (err) => {
27+
if (err) {
28+
console.error("Error deleting file:", err);
29+
return;
30+
}
31+
console.log("File deleted successfully!");
32+
});
33+
};
34+
35+
const githubUsername: string = "qwik-zgheib";
36+
37+
const content: string = `Name: Qwik Zgheib\nAge: 22\nFavorite Programming Language: JavaScript`;
38+
39+
const fileName: string = `${githubUsername}.txt`;
40+
41+
createFile(fileName, content);
42+
readFileContent(fileName);
43+
deleteFile(fileName);
44+
45+
/* -- extra challenge */
46+
const filePath: string = path.join(process.cwd(), "sales.txt");
47+
48+
interface SalesManagerType {
49+
addProduct: () => void;
50+
viewProducts: () => void;
51+
updateProduct: () => void;
52+
deleteProduct: () => void;
53+
calculateTotalSales: () => void;
54+
calculateSalesByProduct: () => void;
55+
exit: () => void;
56+
}
57+
58+
const SalesManager: SalesManagerType = {
59+
addProduct: (): void => {
60+
const name: string = readline.question("Enter product name: ");
61+
const quantity: number = readline.questionInt("Enter quantity sold: ");
62+
const price: number = readline.questionFloat("Enter price: ");
63+
64+
const productLine: string = `${name}, ${quantity}, ${price}\n`;
65+
fs.appendFileSync(filePath, productLine);
66+
console.log("Product added.");
67+
},
68+
69+
viewProducts: (): void => {
70+
if (!fs.existsSync(filePath)) {
71+
console.log("No products found.");
72+
return;
73+
}
74+
const data: string = fs.readFileSync(filePath, "utf8");
75+
console.log("Current Products:\n" + data);
76+
},
77+
78+
updateProduct: (): void => {
79+
if (!fs.existsSync(filePath)) {
80+
console.log("No products to update.");
81+
return;
82+
}
83+
const data: string = fs.readFileSync(filePath, "utf8");
84+
const products: string[] = data.split("\n").filter((line) => line.trim() !== "");
85+
const name: string = readline.question("Enter product name to update: ");
86+
87+
const productIndex: number = products.findIndex((line) => line.split(", ")[0] === name);
88+
if (productIndex !== -1) {
89+
const quantity: number = readline.questionInt("Enter new quantity sold: ");
90+
const price: number = readline.questionFloat("Enter new price: ");
91+
products[productIndex] = `${name}, ${quantity}, ${price}`;
92+
fs.writeFileSync(filePath, products.join("\n") + "\n");
93+
console.log("Product updated.");
94+
} else {
95+
console.log("Product not found.");
96+
}
97+
},
98+
99+
deleteProduct: (): void => {
100+
if (!fs.existsSync(filePath)) {
101+
console.log("No products to delete.");
102+
return;
103+
}
104+
const data: string = fs.readFileSync(filePath, "utf8");
105+
const products: string[] = data.split("\n").filter((line) => line.trim() !== "");
106+
const name: string = readline.question("Enter product name to delete: ");
107+
108+
const newProducts: string[] = products.filter((line) => line.split(", ")[0] !== name);
109+
fs.writeFileSync(filePath, newProducts.join("\n") + "\n");
110+
console.log("Product deleted.");
111+
},
112+
113+
calculateTotalSales: (): void => {
114+
if (!fs.existsSync(filePath)) {
115+
console.log("No sales data found.");
116+
return;
117+
}
118+
const data: string = fs.readFileSync(filePath, "utf8");
119+
const products: string[] = data.split("\n").filter((line) => line.trim() !== "");
120+
121+
let totalSales: number = 0;
122+
products.forEach((line) => {
123+
const [, quantity, price] = line.split(", ");
124+
totalSales += parseInt(quantity) * parseFloat(price);
125+
});
126+
console.log("Total Sales: $" + totalSales.toFixed(2));
127+
},
128+
129+
calculateSalesByProduct: (): void => {
130+
if (!fs.existsSync(filePath)) {
131+
console.log("No sales data found.");
132+
return;
133+
}
134+
const data: string = fs.readFileSync(filePath, "utf8");
135+
const products: string[] = data.split("\n").filter((line) => line.trim() !== "");
136+
137+
const salesByProduct: Record<string, number> = {};
138+
products.forEach((line) => {
139+
const [name, quantity, price] = line.split(", ");
140+
if (!salesByProduct[name]) {
141+
salesByProduct[name] = 0;
142+
}
143+
salesByProduct[name] += parseInt(quantity) * parseFloat(price);
144+
});
145+
146+
for (const [name, total] of Object.entries(salesByProduct)) {
147+
console.log(`${name}: $${total.toFixed(2)}`);
148+
}
149+
},
150+
151+
exit: (): void => {
152+
if (fs.existsSync(filePath)) {
153+
fs.unlinkSync(filePath);
154+
}
155+
console.log("Exiting and deleting sales file.");
156+
process.exit();
157+
},
158+
};
159+
160+
const mainMenu = (): void => {
161+
while (true) {
162+
console.log(
163+
"\n1. Add Product\n2. View Products\n3. Update Product\n4. Delete Product\n5. Calculate Total Sales\n6. Calculate Sales by Product\n7. Exit"
164+
);
165+
166+
let choice: number = readline.questionInt("Enter your choice: ");
167+
168+
while (choice < 1 || choice > 7) {
169+
console.log("Invalid choice. Please try again.");
170+
choice = readline.questionInt("Enter your choice: ");
171+
}
172+
if (choice === 1) SalesManager.addProduct();
173+
if (choice === 2) SalesManager.viewProducts();
174+
if (choice === 3) SalesManager.updateProduct();
175+
if (choice === 4) SalesManager.deleteProduct();
176+
if (choice === 5) SalesManager.calculateTotalSales();
177+
if (choice === 6) SalesManager.calculateSalesByProduct();
178+
if (choice === 7) SalesManager.exit();
179+
}
180+
};
181+
182+
mainMenu();

0 commit comments

Comments
 (0)