From d0b5884958f81c0a2722923ad37ff576e3815a18 Mon Sep 17 00:00:00 2001 From: jimdmm Date: Tue, 14 Jul 2026 22:11:22 -0300 Subject: [PATCH] feat: adicionando o cadastro de produto --- controller/controller.go | 20 ++++++++++--- main.go | 6 +--- model/inventory.go | 4 +-- view/view.go | 62 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 79 insertions(+), 13 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index cbe8127..cf201ff 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -1,7 +1,6 @@ package controller import ( - "bufio" "controle-de-estoque/model" "controle-de-estoque/view" ) @@ -14,8 +13,21 @@ func New(inventory *model.Inventory) *Controller { return &Controller{inventory: inventory} } -func (c *Controller) CapturarComando(scanner *bufio.Scanner) { - view.ExibirMenu() +func (c *Controller) Execute() { + for { + view.ShowMenu() + input, err := view.ReadMenuInput() - // scanner.Scan() + if err != nil { + view.ShowError(err) + break + } + + 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}) + } + } } diff --git a/main.go b/main.go index b041b64..65a9d6a 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,12 @@ package main import ( - "bufio" "controle-de-estoque/controller" "controle-de-estoque/model" - "os" ) func main() { estoque := model.NewInventory() controller := controller.New(estoque) - scanner := bufio.NewScanner(os.Stdin) - - controller.CapturarComando(scanner) + controller.Execute() } diff --git a/model/inventory.go b/model/inventory.go index e4732bf..e38e072 100644 --- a/model/inventory.go +++ b/model/inventory.go @@ -24,7 +24,7 @@ func NewInventory() *Inventory { return &Inventory{} } -func (e *Inventory) AdicionarProduto(i *ProductInput) Product { +func (e *Inventory) AdicionarProduto(i *ProductInput) *Product { e.nextID += 1 product := Product{ @@ -37,7 +37,7 @@ func (e *Inventory) AdicionarProduto(i *ProductInput) Product { e.products = append(e.products, product) - return product + return &product } func (e *Inventory) ListarProduto() []Product { diff --git a/view/view.go b/view/view.go index f78e6b6..98aa932 100644 --- a/view/view.go +++ b/view/view.go @@ -1,8 +1,16 @@ package view -import "fmt" +import ( + "bufio" + "controle-de-estoque/model" + "errors" + "fmt" + "os" + "strconv" + "strings" +) -func ExibirMenu() { +func ShowMenu() { fmt.Print(` ============ CONTROLE DE ESTOQUE ============== ESCOLHA UMA DAS OPÇÕES: @@ -13,3 +21,53 @@ func ExibirMenu() { 0 / sair - Sair do programa `) } + +func ShowError(err error) { + fmt.Println("Error: ", err) +} + +func ReadMenuInput() (string, error) { + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + input = strings.TrimSpace(input) + + if err != nil || input == "" { + return input, errors.New("Opção Inválida!") + } + + return input, nil +} + +func ReadProdutoInput() *model.ProductInput { + reader := bufio.NewReader(os.Stdin) + + fmt.Print("Nome do produto: ") + name, _ := reader.ReadString('\n') + + fmt.Print("Categoria do produto: ") + category, _ := reader.ReadString('\n') + + fmt.Print("Preço: ") + precoStr, _ := reader.ReadString('\n') + price, _ := strconv.ParseFloat(strings.TrimSpace(precoStr), 64) + + fmt.Print("Quantidade: ") + qtyStr, _ := reader.ReadString('\n') + quantity, _ := strconv.Atoi(strings.TrimSpace(qtyStr)) + + return &model.ProductInput{ + Name: name, + Category: category, + Price: price, + Quantity: quantity, + } +} + +func ShowMessage(msg string, args []any) { + if args == nil { + fmt.Println(msg) + return + } + + fmt.Printf(msg, args...) +}