feat: adicionando o cadastro de produto

This commit is contained in:
2026-07-14 22:11:22 -03:00
parent 70f433ce2b
commit d0b5884958
4 changed files with 79 additions and 13 deletions
+16 -4
View File
@@ -1,7 +1,6 @@
package controller package controller
import ( import (
"bufio"
"controle-de-estoque/model" "controle-de-estoque/model"
"controle-de-estoque/view" "controle-de-estoque/view"
) )
@@ -14,8 +13,21 @@ func New(inventory *model.Inventory) *Controller {
return &Controller{inventory: inventory} return &Controller{inventory: inventory}
} }
func (c *Controller) CapturarComando(scanner *bufio.Scanner) { func (c *Controller) Execute() {
view.ExibirMenu() 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})
}
}
} }
+1 -5
View File
@@ -1,16 +1,12 @@
package main package main
import ( import (
"bufio"
"controle-de-estoque/controller" "controle-de-estoque/controller"
"controle-de-estoque/model" "controle-de-estoque/model"
"os"
) )
func main() { func main() {
estoque := model.NewInventory() estoque := model.NewInventory()
controller := controller.New(estoque) controller := controller.New(estoque)
scanner := bufio.NewScanner(os.Stdin) controller.Execute()
controller.CapturarComando(scanner)
} }
+2 -2
View File
@@ -24,7 +24,7 @@ func NewInventory() *Inventory {
return &Inventory{} return &Inventory{}
} }
func (e *Inventory) AdicionarProduto(i *ProductInput) Product { func (e *Inventory) AdicionarProduto(i *ProductInput) *Product {
e.nextID += 1 e.nextID += 1
product := Product{ product := Product{
@@ -37,7 +37,7 @@ func (e *Inventory) AdicionarProduto(i *ProductInput) Product {
e.products = append(e.products, product) e.products = append(e.products, product)
return product return &product
} }
func (e *Inventory) ListarProduto() []Product { func (e *Inventory) ListarProduto() []Product {
+60 -2
View File
@@ -1,8 +1,16 @@
package view package view
import "fmt" import (
"bufio"
"controle-de-estoque/model"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
func ExibirMenu() { func ShowMenu() {
fmt.Print(` fmt.Print(`
============ CONTROLE DE ESTOQUE ============== ============ CONTROLE DE ESTOQUE ==============
ESCOLHA UMA DAS OPÇÕES: ESCOLHA UMA DAS OPÇÕES:
@@ -13,3 +21,53 @@ func ExibirMenu() {
0 / sair - Sair do programa 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...)
}