diff --git a/controller/controller.go b/controller/controller.go index cf201ff..485d927 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -28,6 +28,12 @@ func (c *Controller) Execute() { productInput := view.ReadProdutoInput() p := c.inventory.AdicionarProduto(productInput) view.ShowMessage("Produto criado com ID %d: %s\n", []any{p.ID, p.Name}) + case "2", "listar": + products := c.inventory.ListarProdutos() + view.ShowAllProducts(products) + default: + view.ShowMessage("Obrigado por utilizar o programa!", nil) + return } } } diff --git a/model/inventory.go b/model/inventory.go index e38e072..ef923d0 100644 --- a/model/inventory.go +++ b/model/inventory.go @@ -40,7 +40,7 @@ func (e *Inventory) AdicionarProduto(i *ProductInput) *Product { return &product } -func (e *Inventory) ListarProduto() []Product { +func (e *Inventory) ListarProdutos() []Product { return e.products } diff --git a/view/view.go b/view/view.go index 98aa932..59c3590 100644 --- a/view/view.go +++ b/view/view.go @@ -19,6 +19,7 @@ func ShowMenu() { 3 / atualizar - Atualiza um produto do estoque 4 / deletar - Deleta um produto do estoque 0 / sair - Sair do programa + ================================================= `) } @@ -26,6 +27,15 @@ func ShowError(err error) { fmt.Println("Error: ", err) } +func ShowMessage(msg string, args []any) { + if args == nil { + fmt.Println(msg) + return + } + + fmt.Printf(msg, args...) +} + func ReadMenuInput() (string, error) { reader := bufio.NewReader(os.Stdin) input, err := reader.ReadString('\n') @@ -56,18 +66,29 @@ func ReadProdutoInput() *model.ProductInput { quantity, _ := strconv.Atoi(strings.TrimSpace(qtyStr)) return &model.ProductInput{ - Name: name, - Category: category, + Name: strings.TrimSpace(name), + Category: strings.TrimSpace(category), Price: price, Quantity: quantity, } } -func ShowMessage(msg string, args []any) { - if args == nil { - fmt.Println(msg) - return - } +func ShowAllProducts(products []model.Product) { + for _, p := range products { + id := p.ID + name := p.Name + category := p.Category + price := p.Price + quantity := p.Quantity - fmt.Printf(msg, args...) + fmt.Printf( + "==============================================="+"\n"+ + "PRODUTO: %d"+"\n"+ + "NOME: %s"+"\n"+ + "CATEGORIA: %s"+"\n"+ + "PREÇO: %.2f"+"\n"+ + "QUANTIDADE: %d"+"\n"+ + "==============================================="+"\n", + id, name, category, price, quantity) + } }