diff --git a/README.md b/README.md index 07fd982..5b0555d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,5 @@ -

Material referente as entregas dos desafios no curso de Back-End na Coderhouse Brasil

- +

Descrição:


- -

Aulas com entrega:

- - -Acesse as aulas pelas branches \ No newline at end of file +
+

05/12/2024 Desafio ( Classes com ECMAScript e ECMAScript avançado ) da Aula 2 ( Princípios básicos de Javascript e novos recursos ECMAScript ) do curso de Back-end na CoderHouse; Turma #63515 de 2024/25.

+
diff --git a/package.json b/package.json new file mode 100644 index 0000000..5425b6b --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "cursocoderhousebackend", + "version": "1.0.0", + "description": "aula 1 de back end", + "main": "productManager.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Gustavo Rezende", + "license": "ISC" +} diff --git a/src/assets/productManager.js b/src/assets/productManager.js new file mode 100644 index 0000000..a9e0cb6 --- /dev/null +++ b/src/assets/productManager.js @@ -0,0 +1,81 @@ +class ProductManager { + constructor() { + this.products = []; + this.currentId = 1; + } + + addProduct({ title, description, price, thumbnail, code, stock }) { + // Verification + if (!title || !description || !price || !thumbnail || !code || !stock) { + console.error("Error: All field are required."); + return; + } + + // Verification + if (this.products.some((product) => product.code === code)) { + console.error(`Error: The code "${code}" already in use.`); + return; + } + + // Create + const newProduct = { + id: this.currentId++, + title, + description, + price, + thumbnail, + code, + stock, + }; + + // Add List + this.products.push(newProduct); + console.log(`Product "${title}" added successfully.`); + } + + getProductById(id) { + const product = this.products.find((product) => product.id === id); + if (!product) { + console.error("Error: Product not found."); + return; + } + return product; + } + } + + // Test + const manager = new ProductManager(); + + // Add product + manager.addProduct({ + title: "Product 1", + description: "Product description 1", + price: 100, + thumbnail: "product1.jpg", + code: "P001", + stock: 10, + }); + + manager.addProduct({ + title: "Product 2", + description: "Product description 2", + price: 150, + thumbnail: "product2.jpg", + code: "P002", + stock: 5, + }); + + // Try to add product with repeated code + manager.addProduct({ + title: "Product 3", + description: "Product description 3", + price: 200, + thumbnail: "product3.jpg", + code: "P001", + stock: 8, + }); + + // Search product by ID + console.log(manager.getProductById(1)); // Should return Product 1 + console.log(manager.getProductById(3)); // Should return "Error: Product not found" + \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..03576ed --- /dev/null +++ b/src/index.js @@ -0,0 +1 @@ +// 404 \ No newline at end of file