/
goods.go
72 lines (54 loc) · 1.27 KB
/
goods.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
package main
import (
"fmt"
"math/rand"
)
type Trade struct {
Name string
Quantity int
}
type Sellable struct {
Trade
Price float32
}
type Trades []Trade
func (t *Trade) Print() {
fmt.Printf("%s : (%d)\n", t.Name, t.Quantity)
}
func (t Trades) Print() {
position := 0
for _, trade := range t {
position++
fmt.Printf(" %d. ", position)
trade.Print()
}
}
type Goods []Sellable
func (s *Sellable) Print() {
fmt.Printf("%s : %.2f (%d)\n", s.Name, s.Price, s.Quantity)
}
func (g Goods) Add(name string, quantity int) (payment float32) {
for i, item := range g {
if item.Name == name {
g[i].Quantity += quantity
payment = float32(quantity) * item.Price
}
}
return payment
}
func GenerateGoods() Goods {
return Goods{
{Trade: Trade{Name: "Light Saber", Quantity: rand.Intn(5)}, Price: (rand.Float32() * 20) + 20},
{Trade: Trade{Name: "Phaser Gun", Quantity: rand.Intn(5)}, Price: (rand.Float32() * 15) + 15},
{Trade: Trade{Name: "Food Packet", Quantity: rand.Intn(5)}, Price: (rand.Float32() * 5) + 5},
{Trade: Trade{Name: "Thremal Detonator", Quantity: rand.Intn(5)}, Price: (rand.Float32() * 50) + 50},
}
}
func (g Goods) Print() {
position := 0
for _, trade := range g {
position++
fmt.Printf(" %d. ", position)
trade.Print()
}
}