feat: listando os produtos

This commit is contained in:
2026-07-14 22:37:11 -03:00
parent d0b5884958
commit 0315a20b2a
3 changed files with 36 additions and 9 deletions
+6
View File
@@ -28,6 +28,12 @@ func (c *Controller) Execute() {
productInput := view.ReadProdutoInput() productInput := view.ReadProdutoInput()
p := c.inventory.AdicionarProduto(productInput) p := c.inventory.AdicionarProduto(productInput)
view.ShowMessage("Produto criado com ID %d: %s\n", []any{p.ID, p.Name}) 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
} }
} }
} }
+1 -1
View File
@@ -40,7 +40,7 @@ func (e *Inventory) AdicionarProduto(i *ProductInput) *Product {
return &product return &product
} }
func (e *Inventory) ListarProduto() []Product { func (e *Inventory) ListarProdutos() []Product {
return e.products return e.products
} }
+29 -8
View File
@@ -19,6 +19,7 @@ func ShowMenu() {
3 / atualizar - Atualiza um produto do estoque 3 / atualizar - Atualiza um produto do estoque
4 / deletar - Deleta um produto do estoque 4 / deletar - Deleta um produto do estoque
0 / sair - Sair do programa 0 / sair - Sair do programa
=================================================
`) `)
} }
@@ -26,6 +27,15 @@ func ShowError(err error) {
fmt.Println("Error: ", err) 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) { func ReadMenuInput() (string, error) {
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n') input, err := reader.ReadString('\n')
@@ -56,18 +66,29 @@ func ReadProdutoInput() *model.ProductInput {
quantity, _ := strconv.Atoi(strings.TrimSpace(qtyStr)) quantity, _ := strconv.Atoi(strings.TrimSpace(qtyStr))
return &model.ProductInput{ return &model.ProductInput{
Name: name, Name: strings.TrimSpace(name),
Category: category, Category: strings.TrimSpace(category),
Price: price, Price: price,
Quantity: quantity, Quantity: quantity,
} }
} }
func ShowMessage(msg string, args []any) { func ShowAllProducts(products []model.Product) {
if args == nil { for _, p := range products {
fmt.Println(msg) id := p.ID
return 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)
}
} }