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 Category } // 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 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 removeProduct(inventory map[string]Product, name string) { //no checks required. Does nothing if the product doesn't exist. delete(inventory, name) } 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() { 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 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 fmt.Println("\nUpdating quantity for Apples: New Quantity = 30") updateQuantity(inventory, "Apples", 30) // Remove a product fmt.Println("\nRemoving product: T-shirt") removeProduct(inventory, "T-shirt") // Display final inventory fmt.Println("\nFinal Inventory:") displayInventory(inventory) }