1
0
Fork 0
pr3-sose2026-fork/go/exercises/inventory/inventory.go

82 lines
1.8 KiB
Go

package main
import "fmt"
type prod_category int
const ( //Subtask 1
ELECTRONICS prod_category = iota
GROCERIES prod_category = iota
CLOTHES prod_category = iota
)
type Product struct {
category prod_category
name string
quantitiy float32
price float32
}
var initial_inventory = [1]Product{{ELECTRONICS, "Laptop", 10, 100.0}} //Subtask 2 => Did you mean "product(s)" instead of "product names"
var inventory = initial_inventory[0:] //Subtask 3
var product_categories = map[prod_category]string{
0: "Electronics",
1: "Groceries",
2: "Clothes",
} //Subtask 4
// From here on Subtask 5:
func AddProduct(category prod_category, name string, quantity float32, price float32) {
inventory = append(inventory, Product{category, name, quantity, price})
}
func RemoveProduct(product_name string) {
var new_inventory = []Product{}
for i := 0; i < len(inventory); i++ {
if inventory[i].name != product_name {
new_inventory = append(new_inventory, inventory[i])
}
}
inventory = new_inventory
}
func DisplayInventory() {
fmt.Println("Hier ist ihre Produktuebersicht:")
fmt.Println("Kategorie: |Name: |Preis: |Menge: |")
for i := 0; i < len(inventory); i++ {
product := inventory[i]
fmt.Printf("%s | %s | %.2f | %.2f |\n", product_categories[product.category], product.name, product.price, product.quantitiy)
}
fmt.Println()
}
func UpdateQuantitiy(product_name string, new_quantity float32) {
for i := 0; i < len(inventory); i++ {
if inventory[i].name == product_name {
inventory[i].quantitiy = new_quantity
}
}
}
func main() {
DisplayInventory()
AddProduct(CLOTHES, "Jeans", 10, 50)
AddProduct(GROCERIES, "Flour", 5, 3)
DisplayInventory()
UpdateQuantitiy("Flour", 100)
DisplayInventory()
RemoveProduct("Laptop")
DisplayInventory()
}