forked from steger/pr3-sose2026
comments for inventory
parent
0e296146ec
commit
362bf8202b
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue