This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinventory.go
67 lines (55 loc) · 1.52 KB
/
inventory.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
// Create an application which manages an inventory of products. Create a
// product class which has a price, id, and quantity on hand. Then create an
// *inventory* class which keeps track of various products and can sum up
// the inventory value.
package main
import (
"fmt"
"os"
"text/tabwriter"
)
// Product "class" that contains a price, id, and quantity
type Product struct {
price float64
id int
quantity int
}
// Inventory "class" that contains a list of Products
type Inventory struct {
products []*Product
}
// Inventory "class" method that prints a formatted inventory list
func (inv *Inventory) printInventory() {
w := new(tabwriter.Writer)
// Format in tab-separated columns with right alignment
w.Init(os.Stdout, 6, 0, 4, ' ', tabwriter.AlignRight)
fmt.Fprintln(w, "ID\tPRICE\tQUANTITY\t")
for _, p := range inv.products {
fmt.Fprintf(w, "%d\t$%.2f\t%d\t\n", p.id, p.price, p.quantity)
}
fmt.Fprintln(w)
w.Flush()
}
// Inventory "class" method that adds a product to the inventory
func (inv *Inventory) add(p *Product) {
inv.products = append(inv.products, p)
}
func main() {
// Create some Products
p1 := Product{0.99, 1, 16}
p2 := Product{19.99, 2, 5}
p3 := Product{4.99, 3, 8}
// Create an Inventory and add the products to it
var inv Inventory
inv.add(&p1)
inv.add(&p2)
inv.add(&p3)
fmt.Println("Inventory List Before Changes")
inv.printInventory()
// Make a few changes
p2.quantity += 4
p1.price += 2
p3.quantity -= 5
fmt.Println("Inventory List After Changes")
inv.printInventory()
}