2026-07-14 20:16:10 -03:00
|
|
|
package view
|
|
|
|
|
|
2026-07-14 22:11:22 -03:00
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"controle-de-estoque/model"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
2026-07-14 20:16:10 -03:00
|
|
|
|
2026-07-15 00:28:28 -03:00
|
|
|
var reader = bufio.NewReader(os.Stdin)
|
|
|
|
|
|
2026-07-14 22:11:22 -03:00
|
|
|
func ShowMenu() {
|
2026-07-14 20:16:10 -03:00
|
|
|
fmt.Print(`
|
|
|
|
|
============ CONTROLE DE ESTOQUE ==============
|
|
|
|
|
ESCOLHA UMA DAS OPÇÕES:
|
|
|
|
|
1 / adicionar - Adiciona o produto no estoque
|
|
|
|
|
2 / listar - Lista todos os produtos do estoque
|
|
|
|
|
3 / atualizar - Atualiza um produto do estoque
|
|
|
|
|
4 / deletar - Deleta um produto do estoque
|
|
|
|
|
0 / sair - Sair do programa
|
2026-07-14 22:37:11 -03:00
|
|
|
=================================================
|
2026-07-14 20:16:10 -03:00
|
|
|
`)
|
|
|
|
|
}
|
2026-07-14 22:11:22 -03:00
|
|
|
|
|
|
|
|
func ShowError(err error) {
|
|
|
|
|
fmt.Println("Error: ", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 22:37:11 -03:00
|
|
|
func ShowMessage(msg string, args []any) {
|
|
|
|
|
if args == nil {
|
|
|
|
|
fmt.Println(msg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Printf(msg, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 22:11:22 -03:00
|
|
|
func ReadMenuInput() (string, error) {
|
|
|
|
|
input, err := reader.ReadString('\n')
|
|
|
|
|
input = strings.TrimSpace(input)
|
|
|
|
|
|
|
|
|
|
if err != nil || input == "" {
|
|
|
|
|
return input, errors.New("Opção Inválida!")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return input, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 00:28:28 -03:00
|
|
|
func ReadAddProductInput() *model.ProductInput {
|
2026-07-14 22:11:22 -03:00
|
|
|
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{
|
2026-07-14 22:37:11 -03:00
|
|
|
Name: strings.TrimSpace(name),
|
|
|
|
|
Category: strings.TrimSpace(category),
|
2026-07-14 22:11:22 -03:00
|
|
|
Price: price,
|
|
|
|
|
Quantity: quantity,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 00:28:28 -03:00
|
|
|
func ReadEditProductInput() (model.ProductID, string) {
|
|
|
|
|
fmt.Print("Informe o Id do produto que deseja atualizar: ")
|
|
|
|
|
productID, _ := reader.ReadString('\n')
|
|
|
|
|
productIDInt, _ := strconv.Atoi(strings.TrimSpace(productID))
|
|
|
|
|
|
|
|
|
|
fmt.Print("Informe o nome do campo que deseja atualizar (nome, categoria, preco, quantidade): ")
|
|
|
|
|
field, _ := reader.ReadString('\n')
|
|
|
|
|
field = strings.TrimSpace(field)
|
|
|
|
|
|
|
|
|
|
return model.ProductID(productIDInt), field
|
|
|
|
|
}
|
2026-07-15 00:44:32 -03:00
|
|
|
func ReadDeleteProductInput() model.ProductID {
|
|
|
|
|
fmt.Print("Informe o Id do produto que deseja deletar: ")
|
|
|
|
|
productID, _ := reader.ReadString('\n')
|
|
|
|
|
productIDInt, _ := strconv.Atoi(strings.TrimSpace(productID))
|
|
|
|
|
|
|
|
|
|
return model.ProductID(productIDInt)
|
|
|
|
|
}
|
2026-07-15 00:28:28 -03:00
|
|
|
|
|
|
|
|
func ReadNewValueInput(field string) string {
|
|
|
|
|
fmt.Printf("Informe o novo valor para %s: ", field)
|
|
|
|
|
value, _ := reader.ReadString('\n')
|
|
|
|
|
|
|
|
|
|
return strings.TrimSpace(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ShowAllProducts(products map[model.ProductID]model.Product) {
|
2026-07-14 22:37:11 -03:00
|
|
|
for _, p := range products {
|
|
|
|
|
id := p.ID
|
|
|
|
|
name := p.Name
|
|
|
|
|
category := p.Category
|
|
|
|
|
price := p.Price
|
|
|
|
|
quantity := p.Quantity
|
2026-07-14 22:11:22 -03:00
|
|
|
|
2026-07-14 22:37:11 -03:00
|
|
|
fmt.Printf(
|
|
|
|
|
"==============================================="+"\n"+
|
2026-07-15 00:51:18 -03:00
|
|
|
"ID PRODUTO: %d"+"\n"+
|
|
|
|
|
"NOME: %s"+"\n"+
|
|
|
|
|
"CATEGORIA: %s"+"\n"+
|
2026-07-15 00:28:28 -03:00
|
|
|
"PRICE: %.2f"+"\n"+
|
2026-07-15 00:51:18 -03:00
|
|
|
"QUANTIDADE: %d"+"\n"+
|
2026-07-14 22:37:11 -03:00
|
|
|
"==============================================="+"\n",
|
|
|
|
|
id, name, category, price, quantity)
|
|
|
|
|
}
|
2026-07-14 22:11:22 -03:00
|
|
|
}
|