forked from steger/pr3-sose2026
Compare commits
3 Commits
90244f2511
...
b83f1365e4
| Author | SHA1 | Date |
|---|---|---|
|
|
b83f1365e4 | |
|
|
72c8f435e2 | |
|
|
9a92c34d5e |
|
|
@ -0,0 +1,81 @@
|
|||
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()
|
||||
}
|
||||
Loading…
Reference in New Issue