Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<h2>Material referente as entregas dos desafios no curso de Back-End na Coderhouse Brasil</h2>

<h1>Descrição:</h1>
<hr>

<h3> Aulas com entrega:</h3>
<ul>
<li>Aula 2 ( Classes com ECMAScript e ECMAScript avançado )</li>

</ul>

<span>Acesse as aulas pelas branches</span>
<div>
<p>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.</p>
</div>
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
81 changes: 81 additions & 0 deletions src/assets/productManager.js
Original file line number Diff line number Diff line change
@@ -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"

1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// 404