From 0e296146ecce2c92137165a2388c3006bf888099 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 06:28:32 +0000 Subject: [PATCH 1/2] inventory solution --- go/01-basics/inventory.go | 81 ++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/go/01-basics/inventory.go b/go/01-basics/inventory.go index 209c374..52694e2 100644 --- a/go/01-basics/inventory.go +++ b/go/01-basics/inventory.go @@ -1,40 +1,101 @@ 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 { 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 +// Add a new product to the inventory +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) { - //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) { - //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) { - //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() { + // Initialize inventory 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"}, + {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}, } // Display initial inventory displayInventory(inventory) // Add a new product - addProduct(&inventory, "Phone", 800, 10, "Electronics") + addProduct(&inventory, "Phone", 800, 10, Electronics) // Display updated inventory displayInventory(inventory) From 362bf8202bb89cca5a35d9352e0c68549bd96839 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Mon, 22 Dec 2025 10:40:08 +0000 Subject: [PATCH 2/2] comments for inventory --- go/01-basics/inventory.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/go/01-basics/inventory.go b/go/01-basics/inventory.go index 52694e2..79e4868 100644 --- a/go/01-basics/inventory.go +++ b/go/01-basics/inventory.go @@ -2,14 +2,17 @@ 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 - Groceries - Clothes + 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: @@ -32,7 +35,8 @@ type Product struct { } // Add a new product to the inventory -func addProduct(inventory *[]Product, name string, price float64, quantity int, category Category) { +func addProduct(inventory *[]Product, //pointer to the slice of of Products that shall be modified. In that way it can serve as in input/output parameter + 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 { @@ -51,7 +55,8 @@ func addProduct(inventory *[]Product, name string, price float64, quantity int, } // Remove a product from the inventory -func removeProduct(inventory *[]Product, name string) { +func removeProduct(inventory *[]Product, //pointer to the slice of of Products that shall be modified. In that way it can serve as in input/output parameter + name string) { for i, product := range *inventory { if product.Name == name { *inventory = append((*inventory)[:i], (*inventory)[i+1:]...) @@ -62,8 +67,9 @@ func removeProduct(inventory *[]Product, name string) { fmt.Println("Product not found.") } -// Update the quantity of a product -func updateQuantity(inventory *[]Product, name string, newQuantity int) { +// Update the quantity of a product. +func updateQuantity(inventory *[]Product, //pointer to the slice of of Products that shall be modified. In that way it can serve as in input/output parameter + name string, newQuantity int) { for i, product := range *inventory { if product.Name == name { (*inventory)[i].Quantity = newQuantity @@ -77,7 +83,7 @@ func updateQuantity(inventory *[]Product, name string, newQuantity int) { // Display the inventory func displayInventory(inventory []Product) { fmt.Println("Inventory:") - for _, product := range inventory { + for _, product := range inventory { //iterating over a range returns pairs of index and the content of a container (here: inventory). Since we don't care about the index, we use _ as a placeholder. fmt.Printf("%s - %s (Price: $%.2f, Quantity: %d)\n", product.Name, product.Category, product.Price, product.Quantity) }