1
0
Fork 0
Sebastian Steger 2026-10-16 09:53:06 +00:00
commit 36b0db6473
1 changed files with 86 additions and 16 deletions

View File

@ -1,50 +1,120 @@
package main
import (
"fmt"
)
// a new type named Category that can hold integer values
type Category int
// The go equivalent of an enum that enumerates different named Categories
const (
Electronics Category = iota //0
Groceries //1
Clothes //2
)
// implementation of the fmt.Stringer interface. In that way, Categories can be printed using the fmt.Print* functions
func (c Category) String() string {
switch c {
case Electronics:
return "Electronics"
case Groceries:
return "Groceries"
case Clothes:
return "Clothes"
default:
return "Unknown"
}
}
type Product struct {
Name string
Price float64
Quantity int
Category string //TODO: use enum instead
Category Category
}
func addProduct(inventory *[]Product, name string, price float64, quantity int, category string) {
//TODO: implement
// implementation of the fmt.Stringer interface for printing products
func (p Product) String() string {
return fmt.Sprintf("%s (%s, Price: $%.2f, Quantity: %d)", p.Name, p.Category, p.Price, p.Quantity)
}
func removeProduct(inventory *[]Product, name string) {
//TODO: implement
func addProduct(inventory map[string]Product, name string, price float64, quantity int, category Category) {
//do not create a new product in case a product with the name already exists
if _, exists := inventory[name]; exists {
fmt.Printf("Unable to add new Product %s: Product already exists.\n", name)
return
}
//create a new product...
p := Product{name, price, quantity, category}
inventory[name] = p // ...and insert it into the map
}
func updateQuantity(inventory *[]Product, name string, newQuantity int) {
//TODO: implement
func removeProduct(inventory map[string]Product, name string) {
//no checks required. Does nothing if the product doesn't exist.
delete(inventory, name)
}
func displayInventory(inventory []Product) {
//TODO: implement
func updateQuantity(inventory map[string]Product, name string, newQuantity int) {
p := inventory[name] //read from the map
p.Quantity = newQuantity //update value
inventory[name] = p //store back into the map
}
func displayInventory(inventory map[string]Product) {
//Iterate over the inventory
i := 1
for _, p := range inventory { //don't care about the key (_), only the value (p) is relevant
fmt.Printf("%d. %s\n", i, p)
i++
}
}
func main() {
inventory := []Product{
{Name: "Laptop", Price: 1000, Quantity: 5, Category: "Electronics"},
{Name: "Apples", Price: 2, Quantity: 50, Category: "Groceries"},
{Name: "T-shirt", Price: 10, Quantity: 20, Category: "Clothes"},
fmt.Println("Welcome to the Inventory Manager!")
// Store a fixed list of initial product names in an array.
initialProductNames := [...]string{"Laptop"}
// Convert the array into a slice so the inventory can grow or shrink dynamically.
productNames := initialProductNames[:]
productNames = append(productNames, "Apples", "T-shirt")
// Use a map to associate product names with details like quantity, price, and category.
prices := []float64{1000.0, 2.0, 10.0}
quantities := []int{5, 50, 20}
categories := []Category{Electronics, Groceries, Clothes}
inventory := make(map[string]Product)
for i, name := range productNames {
inventory[name] = Product{name, prices[i], quantities[i], categories[i]}
}
// Display initial inventory
fmt.Println("\nInitial Inventory:")
displayInventory(inventory)
// Add a new product
addProduct(&inventory, "Phone", 800, 10, "Electronics")
fmt.Println("\nAdding a new product: Phone (Electronics, Price: $800, Quantity: 10)")
addProduct(inventory, "Phone", 800, 10, Electronics)
// Display updated inventory
fmt.Println("\nUpdated Inventory:")
displayInventory(inventory)
// Update the quantity of an existing product
updateQuantity(&inventory, "Apples", 30)
fmt.Println("\nUpdating quantity for Apples: New Quantity = 30")
updateQuantity(inventory, "Apples", 30)
// Remove a product
removeProduct(&inventory, "T-shirt")
fmt.Println("\nRemoving product: T-shirt")
removeProduct(inventory, "T-shirt")
// Display final inventory
fmt.Println("\nFinal Inventory:")
displayInventory(inventory)
}