forked from steger/pr3-ws202526
inventory solution
parent
60bc9586d1
commit
0e296146ec
|
|
@ -1,40 +1,101 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Category int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Electronics Category = iota
|
||||||
|
Groceries
|
||||||
|
Clothes
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c Category) String() string {
|
||||||
|
switch c {
|
||||||
|
case Electronics:
|
||||||
|
return "Electronics"
|
||||||
|
case Groceries:
|
||||||
|
return "Groceries"
|
||||||
|
case Clothes:
|
||||||
|
return "Clothes"
|
||||||
|
default:
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Struct for product details
|
||||||
type Product struct {
|
type Product struct {
|
||||||
Name string
|
Name string
|
||||||
Price float64
|
Price float64
|
||||||
Quantity int
|
Quantity int
|
||||||
Category string //TODO: use enum instead
|
Category Category
|
||||||
}
|
}
|
||||||
|
|
||||||
func addProduct(inventory *[]Product, name string, price float64, quantity int, category string) {
|
// Add a new product to the inventory
|
||||||
//TODO: implement
|
func addProduct(inventory *[]Product, name string, price float64, quantity int, category Category) {
|
||||||
|
// Check if the product already exists in the inventory
|
||||||
|
for _, product := range *inventory {
|
||||||
|
if product.Name == name {
|
||||||
|
fmt.Println("Product already exists.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Add the new product
|
||||||
|
*inventory = append(*inventory, Product{
|
||||||
|
Name: name,
|
||||||
|
Price: price,
|
||||||
|
Quantity: quantity,
|
||||||
|
Category: category,
|
||||||
|
})
|
||||||
|
fmt.Printf("Added product: %s\n", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove a product from the inventory
|
||||||
func removeProduct(inventory *[]Product, name string) {
|
func removeProduct(inventory *[]Product, name string) {
|
||||||
//TODO: implement
|
for i, product := range *inventory {
|
||||||
|
if product.Name == name {
|
||||||
|
*inventory = append((*inventory)[:i], (*inventory)[i+1:]...)
|
||||||
|
fmt.Printf("Removed product: %s\n", name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("Product not found.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the quantity of a product
|
||||||
func updateQuantity(inventory *[]Product, name string, newQuantity int) {
|
func updateQuantity(inventory *[]Product, name string, newQuantity int) {
|
||||||
//TODO: implement
|
for i, product := range *inventory {
|
||||||
|
if product.Name == name {
|
||||||
|
(*inventory)[i].Quantity = newQuantity
|
||||||
|
fmt.Printf("Updated quantity for %s: New Quantity = %d\n", name, newQuantity)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("Product not found.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display the inventory
|
||||||
func displayInventory(inventory []Product) {
|
func displayInventory(inventory []Product) {
|
||||||
//TODO: implement
|
fmt.Println("Inventory:")
|
||||||
|
for _, product := range inventory {
|
||||||
|
fmt.Printf("%s - %s (Price: $%.2f, Quantity: %d)\n",
|
||||||
|
product.Name, product.Category, product.Price, product.Quantity)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Initialize inventory
|
||||||
inventory := []Product{
|
inventory := []Product{
|
||||||
{Name: "Laptop", Price: 1000, Quantity: 5, Category: "Electronics"},
|
{Name: "Laptop", Price: 1000, Quantity: 5, Category: Electronics},
|
||||||
{Name: "Apples", Price: 2, Quantity: 50, Category: "Groceries"},
|
{Name: "Apples", Price: 2, Quantity: 50, Category: Groceries},
|
||||||
{Name: "T-shirt", Price: 10, Quantity: 20, Category: "Clothes"},
|
{Name: "T-shirt", Price: 10, Quantity: 20, Category: Clothes},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display initial inventory
|
// Display initial inventory
|
||||||
displayInventory(inventory)
|
displayInventory(inventory)
|
||||||
|
|
||||||
// Add a new product
|
// Add a new product
|
||||||
addProduct(&inventory, "Phone", 800, 10, "Electronics")
|
addProduct(&inventory, "Phone", 800, 10, Electronics)
|
||||||
|
|
||||||
// Display updated inventory
|
// Display updated inventory
|
||||||
displayInventory(inventory)
|
displayInventory(inventory)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue