From 08c83157b1f0f672fd9e6535ce2bdbef250468bf Mon Sep 17 00:00:00 2001 From: jimdmm Date: Wed, 15 Jul 2026 00:28:28 -0300 Subject: [PATCH] feat: atualizando os produtos --- controller/controller.go | 37 +++++++++++++++++++---- model/inventory.go | 64 ++++++++++++++++++++++++++++++++-------- view/view.go | 38 +++++++++++++++++------- 3 files changed, 111 insertions(+), 28 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index 485d927..c28343b 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -25,15 +25,40 @@ func (c *Controller) Execute() { switch input { case "1", "adicionar": - productInput := view.ReadProdutoInput() - p := c.inventory.AdicionarProduto(productInput) - view.ShowMessage("Produto criado com ID %d: %s\n", []any{p.ID, p.Name}) + c.AddProduct() case "2", "listar": - products := c.inventory.ListarProdutos() - view.ShowAllProducts(products) + c.listAllProducts() + case "3", "atualizar": + c.updateProduct() default: - view.ShowMessage("Obrigado por utilizar o programa!", nil) + view.ShowMessage("Sistema encerrado!", nil) return } } } + +func (c *Controller) AddProduct() { + productInput := view.ReadAddProductInput() + p := c.inventory.AddProduct(productInput) + view.ShowMessage("Produto criado com ID %d: %s\n", []any{p.ID, p.Name}) +} + +func (c *Controller) listAllProducts() { + products := c.inventory.FetchProducts() + view.ShowAllProducts(products) +} + +func (c *Controller) updateProduct() { + productID, field := view.ReadEditProductInput() + + value := view.ReadNewValueInput(field) + + updated, err := c.inventory.UpdateProduct(productID, field, value) + + if err != nil { + view.ShowError(err) + return + } + + view.ShowMessage("Produto %d atualizado com sucesso!\n", []any{updated.ID}) +} diff --git a/model/inventory.go b/model/inventory.go index ef923d0..07da279 100644 --- a/model/inventory.go +++ b/model/inventory.go @@ -1,7 +1,17 @@ package model +import ( + "errors" + "strconv" + "strings" +) + +type ProductID int + +var productID ProductID = 1 + type Product struct { - ID int + ID ProductID Name string Category string Price float64 @@ -16,34 +26,64 @@ type ProductInput struct { } type Inventory struct { - products []Product - nextID int + products map[ProductID]Product } func NewInventory() *Inventory { - return &Inventory{} + return &Inventory{products: make(map[ProductID]Product)} } -func (e *Inventory) AdicionarProduto(i *ProductInput) *Product { - e.nextID += 1 - +func (e *Inventory) AddProduct(i *ProductInput) *Product { product := Product{ - ID: e.nextID, + ID: productID, Name: i.Name, Category: i.Category, Price: i.Price, Quantity: i.Quantity, } - e.products = append(e.products, product) + e.products[productID] = product + + productID += 1 return &product } -func (e *Inventory) ListarProdutos() []Product { +func (e *Inventory) FetchProducts() map[ProductID]Product { return e.products } -func (e *Inventory) AtualizarProduto() {} +func (e *Inventory) UpdateProduct(id ProductID, field, value string) (*Product, error) { + product, ok := e.products[id] -func (e *Inventory) DeletarProduto() {} + if !ok { + return nil, errors.New("Produto não encontrado") + } + + switch strings.ToLower(field) { + case "nome", "name": + product.Name = value + case "categoria", "category": + product.Category = value + case "preco", "preço", "price": + price, err := strconv.ParseFloat(value, 64) + if err != nil { + return nil, errors.New("Preço inválido") + } + product.Price = price + case "quantidade", "quantity": + quantity, err := strconv.Atoi(value) + if err != nil { + return nil, errors.New("Quantidade inválida") + } + product.Quantity = quantity + default: + return nil, errors.New("Campo inválido") + } + + e.products[id] = product + + return &product, nil +} + +func (e *Inventory) DeleteProduct() {} diff --git a/view/view.go b/view/view.go index 59c3590..8609afb 100644 --- a/view/view.go +++ b/view/view.go @@ -10,6 +10,8 @@ import ( "strings" ) +var reader = bufio.NewReader(os.Stdin) + func ShowMenu() { fmt.Print(` ============ CONTROLE DE ESTOQUE ============== @@ -37,7 +39,6 @@ func ShowMessage(msg string, args []any) { } func ReadMenuInput() (string, error) { - reader := bufio.NewReader(os.Stdin) input, err := reader.ReadString('\n') input = strings.TrimSpace(input) @@ -48,9 +49,7 @@ func ReadMenuInput() (string, error) { return input, nil } -func ReadProdutoInput() *model.ProductInput { - reader := bufio.NewReader(os.Stdin) - +func ReadAddProductInput() *model.ProductInput { fmt.Print("Nome do produto: ") name, _ := reader.ReadString('\n') @@ -73,7 +72,26 @@ func ReadProdutoInput() *model.ProductInput { } } -func ShowAllProducts(products []model.Product) { +func ReadEditProductInput() (model.ProductID, string) { + fmt.Print("Informe o Id do produto que deseja atualizar: ") + productID, _ := reader.ReadString('\n') + productIDInt, _ := strconv.Atoi(strings.TrimSpace(productID)) + + fmt.Print("Informe o nome do campo que deseja atualizar (nome, categoria, preco, quantidade): ") + field, _ := reader.ReadString('\n') + field = strings.TrimSpace(field) + + return model.ProductID(productIDInt), field +} + +func ReadNewValueInput(field string) string { + fmt.Printf("Informe o novo valor para %s: ", field) + value, _ := reader.ReadString('\n') + + return strings.TrimSpace(value) +} + +func ShowAllProducts(products map[model.ProductID]model.Product) { for _, p := range products { id := p.ID name := p.Name @@ -83,11 +101,11 @@ func ShowAllProducts(products []model.Product) { fmt.Printf( "==============================================="+"\n"+ - "PRODUTO: %d"+"\n"+ - "NOME: %s"+"\n"+ - "CATEGORIA: %s"+"\n"+ - "PREÇO: %.2f"+"\n"+ - "QUANTIDADE: %d"+"\n"+ + "PRODUCT ID: %d"+"\n"+ + "NAME: %s"+"\n"+ + "CATEGORY: %s"+"\n"+ + "PRICE: %.2f"+"\n"+ + "QUANTITY: %d"+"\n"+ "==============================================="+"\n", id, name, category, price, quantity) }