Skip to content

Commit 6b10d16

Browse files
authored
Merge pull request mouredev#4522 from qwik-zgheib/main
#11-13 - JS/TS
2 parents eaca42e + b5f2895 commit 6b10d16

File tree

6 files changed

+520
-0
lines changed

6 files changed

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