feat: adicionando o cadastro de produto
This commit is contained in:
@@ -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})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
+2
-2
@@ -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 {
|
||||
|
||||
+60
-2
@@ -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...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user