// Inventory System -- package inventory (starter). // // Finish the TODOs so that main.go prints what the project page shows. As it // is, the file compiles and the methods do nothing useful. Add "errors" to the // import list when you need it. package inventory import "fmt" // Item is one collectable thing. Make one with NewItem. type Item struct { name string weight float64 value int } // NewItem returns an item. The name must not be empty, and the weight and the // value must not be below 0. func NewItem(name string, weight float64, value int) (*Item, error) { // TODO: return nil and an error for each of the three bad cases. return &Item{name: name, weight: weight, value: value}, nil } // Name returns the item's name. func (it *Item) Name() string { return it.name } // Describe returns a one-line summary such as "sword (3.5kg, 50g)". func (it *Item) Describe() string { // TODO: use fmt.Sprintf. Replace this placeholder line: return "TODO: replace this line" } // Inventory is a container of items. Make one with New. type Inventory struct { items []*Item } // New returns an empty inventory. func New() *Inventory { return &Inventory{} } // Add puts an item in the inventory. func (inv *Inventory) Add(item *Item) { // TODO: append item to inv.items. } // Remove takes out the first item with this name and returns it. It returns // nil if no item has this name. func (inv *Inventory) Remove(name string) *Item { // TODO: walk inv.items with range. If an item has this name, remove it // from the slice as in Chapter 22, and return it. return nil } // TotalWeight returns the sum of the weights. func (inv *Inventory) TotalWeight() float64 { // TODO: add up the weights. Replace this placeholder value: return 0 } // TotalValue returns the sum of the values. func (inv *Inventory) TotalValue() int { // TODO: add up the values. Replace this placeholder value: return 0 } // Counts returns how many items there are with each name. func (inv *Inventory) Counts() map[string]int { // TODO: make a map and count the names. Replace this placeholder value: return map[string]int{} } // List prints each item on its own line, then a line of totals. func (inv *Inventory) List() { // TODO: print "Inventory (N items):", then " - " and Describe for each // item, then " Totals: 8.70kg, 85g" with the real totals. fmt.Println("TODO: replace this line") }