From 2359deb6bb9a557bf248f11f1c505b0b83ce218e Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 15 May 2026 21:42:40 +0700 Subject: [PATCH] sua bai tap demo --- main.js | 138 ++++++++++++++++++++++++++++++++++++++++++++ quan_ly_san_pham.js | 54 +++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 quan_ly_san_pham.js diff --git a/main.js b/main.js index e69de29..3f39b3c 100644 --- a/main.js +++ b/main.js @@ -0,0 +1,138 @@ +/* Khai báo danh sách sản phẩm, có 2 sp mẩu */ +const productList = [ + { + id: -1, + name: "Máy tính 1", + brand: "Asus", + price: 5000, + quantity: 5 + }, + { + id: -2, + name: "Máy tính 2", + brand: "Lenovo", + price: 6000, + quantity: 1 + } +] + +/* Khai báo 1 cái id để tự tăng sau này khi thêm product */ +let genId = 0; + +let choice = 0; + +do { + alert( + "1/ Hiển thị danh sách product\n" + + "2/ Thêm sản phẩm\n" + + "3/ Tìm theo tên\n" + + "4/ Cập nhật thông tin\n" + + "5/ Xóa theo id\n" + + "0/ Thoát chương trình" + ); + + choice = +prompt("Nhập lựa chọn của bạn") + + switch (choice) { + case 1: + let nameList = [] + for (product of productList) { + nameList.push(product.name) + } + alert(nameList) + console.log("productList", productList) + break + case 2: + let newProduct = { + id: genId++, + // name: prompt("Nhập tên sản phẩm"), + // brand: prompt("Nhập thương hiệu"), + // price: +prompt("Nhập giá sản phẩm"), + // quantity: +prompt("Nhập số lượng sản phẩm") + } + + /* validate */ + while (!newProduct.name || newProduct.name.length == 0) { + if (Object.hasOwn(newProduct, "name")) { + alert("Tên không hợp lệ") + } + newProduct.name = prompt("Nhập tên sản phẩm"); + } + + while (!newProduct.brand || newProduct.brand == "") { + if (Object.hasOwn(newProduct, "brand")) { + alert("Thương hiệu không hợp lệ") + } + newProduct.brand = prompt("Nhập thương hiệu sản phẩm"); + } + + while (isNaN(newProduct.price)) { + if (Object.hasOwn(newProduct, "price")) { + alert("Giá không hợp lệ") + } + newProduct.price = +prompt("Nhập giá sản phẩm", 0); + console.log("newProduct.price", newProduct.price) + } + + while (isNaN(newProduct.quantity)) { + if (Object.hasOwn(newProduct, "quantity")) { + alert("Số lượng không hợp lệ") + } + newProduct.quantity = +prompt("Nhập số lượng sản phẩm", 0); + } + + productList.push(newProduct) + + /* Hiển thị */ + let nameList2 = [] + for (product of productList) { + nameList2.push(product.name) + } + alert(nameList2) + + break + case 3: + let findResult = [] + let inputSearch = prompt("Nhập thông tin tìm (theo tên)") + for (product of productList) { + if (product.name.includes(inputSearch)) { + findResult.push(product.name) + } + } + alert(findResult) + break + case 4: + let productEditId = +prompt("Nhập id muốn sửa") + for (product of productList) { + if (product.id == productEditId) { + product.name = prompt("Nhập tên mới", product.name) + product.brand = prompt("Nhập tên brand mới", product.brand) + product.price = +prompt("Nhập tên price mới", product.price) + product.quantity = +prompt("Nhập số lượng mới", product.quantity) + break; + } + } + + /* Hiển thị */ + let nameList3 = [] + for (product of productList) { + nameList3.push(product.name) + } + alert(nameList3) + break + case 5: + let productDeleteId = +prompt("Nhập id muốn xóa") + for(let i = 0; i < productList.length; i++) { + if(productList[i].id == productDeleteId) { + productList.splice(i, 1) + break; + } + } + break + case 0: + break; + default: + alert("Lựa chọn không hợp lệ!") + } + +} while (choice != 0) \ No newline at end of file diff --git a/quan_ly_san_pham.js b/quan_ly_san_pham.js new file mode 100644 index 0000000..2a484f8 --- /dev/null +++ b/quan_ly_san_pham.js @@ -0,0 +1,54 @@ +/* Khai báo danh sách sản phẩm, có 2 sp mẩu */ +const productList = [ + { + id: -1, + name: "Máy tính 1", + brand: "Asus", + price: 5000, + quantity: 5 + }, + { + id: -2, + name: "Máy tính 2", + brand: "Lenovo", + price: 6000, + quantity: 1 + } +] + +/* Khai báo 1 cái id để tự tăng sau này khi thêm product */ +let genId = 0; + +let choice = 0; + +do { + alert( + "1/ Hiển thị danh sách product\n" + + "2/ Thêm sản phẩm\n" + + "3/ Tìm theo tên\n" + + "4/ Cập nhật thông tin\n" + + "5/ Xóa theo id\n" + + "0/ Thoát chương trình" + ); + + choice = +prompt("Nhập lựa chọn của bạn") + + switch (choice) { + case 1: + alert(productList) + break + case 2: + break + case 3: + break + case 4: + break + case 5: + break + case 0: + break; + default: + alert("Lựa chọn không hợp lệ!") + } + +} while (choice != 0) \ No newline at end of file