-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory_manager.go
127 lines (107 loc) · 3.51 KB
/
inventory_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Error Handling in Go: Inventory Manager
// Welcome to the Inventory Manager project for SpeedyMart, designed to solidify our Go error handling skills. This exercise challenges us to complete a partially built inventory management application for a supermarket chain, focusing on implementing error-handling strategies.
// Our task is to complete the existing codebase by implementing the following features:
// - Adding items to the inventory
// - Removing items from the inventory
// - Updating item quantities
// - Helpful error handling
package main
import (
"errors"
"fmt"
)
type Item struct {
name string
quantity int
}
type Inventory map[string]Item
const MaxInventorySize = 10
func addItem(inv Inventory, name string, quantity int) error {
// TODO: Complete the addItem function
if _, exists := inv[name]; exists {
return fmt.Errorf("item already exists in inventory: %s", name)
}
if len(inv) > MaxInventorySize {
// return fmt.Errorf("cannot add more items to inventory")
return &InventoryFullError{inventorySize: len(inv)}
} else {
inv[name] = Item{name: name, quantity: quantity}
}
return nil
}
func wrapError(err error, message string) error {
// TODO: Complete the wrapError function
return fmt.Errorf("%s: %w", message, err)
}
func printErrorChain(err error) {
// TODO: Complete the printErrorChain function
if err == nil {
return
}
fmt.Printf("Error: %v\n", err)
printErrorChain(errors.Unwrap(err))
}
func removeItem(inv Inventory, name string) error {
// TODO: Complete the removeItem function
if _, itemExists := inv[name]; itemExists {
delete(inv, name)
} else {
return wrapError(errors.New("Item does not exist"), fmt.Sprintf("invalid item '%s'", name))
}
return nil
}
type InventoryFullError struct {
// TODO: Complete the InventoryFullError struct
inventorySize int
}
func (e InventoryFullError) Error() string {
// TODO: Complete the InventoryFullError Error method
return fmt.Sprintf("%s could not be added: inventory full", e.inventorySize)
}
func updateItemQuantity(inv Inventory, name string, quantity int) {
// TODO: Complete the updateItemQuantity function
// fmt.Println("updating item quantity:", name, quantity)
item, exists := inv[name]
if !exists {
panic(fmt.Sprintf("attempted to update non-existent item %s", name))
}
item.quantity = quantity
inv[name] = item
}
func main() {
inventory := make(Inventory)
// TODO: Defer a function to recover from a panic
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}()
if err := addItem(inventory, "apple", 5); err != nil {
fmt.Printf("Error adding apple: %v\n", err)
} else {
fmt.Println("Apple added to inventory")
fmt.Println("Inventory:", inventory)
}
if err := addItem(inventory, "banana", 7); err != nil {
fmt.Printf("Error adding banana: %v\n", err)
} else {
fmt.Println("Banana added to inventory")
fmt.Println("Inventory:", inventory)
}
fmt.Println("\nRemoving non-existent item from inventory: orange")
if err := removeItem(inventory, "orange"); err != nil {
printErrorChain(err)
}
fmt.Println("Updating apple quantity to 10...")
updateItemQuantity(inventory, "apple", 10)
fmt.Println("Inventory:", inventory)
fmt.Println("\nAdding 10 new empty items to inventory...")
for i := range 9 {
if err := addItem(inventory, string(i), 1); err != nil {
fmt.Printf("Error adding new item: %v\n", err)
}
}
fmt.Println("Inventory:", inventory)
fmt.Println("\nAttempting to update non-existent item: orange")
updateItemQuantity(inventory, "orange", 5)
}