-
Notifications
You must be signed in to change notification settings - Fork 2
/
box.go
56 lines (46 loc) · 933 Bytes
/
box.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
package packme
import (
"bytes"
"fmt"
)
type Box struct {
desc string
dims Dimensions
items Items
}
func NewBox(desc string, dims Dimensions) *Box {
return &Box{
desc: desc,
dims: dims,
items: make(Items, 0),
}
}
func (bx *Box) Desc() string {
return bx.desc
}
func (bx *Box) Volume() float32 {
return bx.dims.Length() * bx.dims.Width() * bx.dims.Height()
}
func (bx *Box) Dimensions() Dimensions {
return bx.dims
}
func (bx *Box) Items() Items {
return bx.items[:]
}
func (bx *Box) String() string {
buf := &bytes.Buffer{}
buf.WriteString(fmt.Sprintf("%s items(%v) %s vol(%v)",
bx.Desc(), len(bx.items), bx.Dimensions(), bx.Volume()))
buf.WriteRune('\n')
buf.WriteString(bx.items.String())
return buf.String()
}
type Boxes []*Box
func (bxs Boxes) String() string {
buf := &bytes.Buffer{}
for _, bx := range bxs {
buf.WriteString(bx.String())
buf.WriteRune('\n')
}
return buf.String()
}