-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
86 lines (72 loc) · 1.63 KB
/
db.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
package cryptohedge
// before we work with a db, we are going to store everything in a struct
import (
"fmt"
)
type Cryptofolio struct {
FiatArray []*Coin
CryptoArray []*Coin
}
type Coin struct {
Name string `json:"name"`
Amount float64 `json:"amount"`
Rate float64
Percentage float64
Value float64
}
type Cryptohedge struct {
Index float64
Total float64
ShareArray []*Share
}
type Share struct {
Name string `json:"name"`
Shares float64 `json:"shares"`
Value float64
}
func (c *Coin) computeValue() (value float64) {
value = c.Amount * c.Rate
c.Value = value
return
}
func (c *Coin) computePercentage(value float64) (p float64) {
p = percentage(c.Value, value)
c.Percentage = p
return
}
func (crypto *Cryptofolio) Value() (value float64) {
for _, c := range crypto.CryptoArray {
value += c.computeValue()
}
return
}
func (crypto *Cryptofolio) Percentage() {
value := crypto.Value()
for _, c := range crypto.CryptoArray {
c.computePercentage(value)
}
}
func (crypto *Cryptofolio) Print() {
for _, c := range crypto.CryptoArray {
fmt.Println(c.Name, " ", c.Amount, " ", c.Value, " ", c.Percentage, "%")
}
}
// compute percentage
func percentage(part float64, total float64) (p float64) {
p = 100 * part / total
return
}
func (hedge *Cryptohedge) ComputeValues(value float64) {
for _, s := range hedge.ShareArray {
hedge.Total += s.Shares
}
hedge.Index = value / hedge.Total
for _, s := range hedge.ShareArray {
s.Value = hedge.Index * s.Shares
}
}
func (hedge *Cryptohedge) Print() {
for _, s := range hedge.ShareArray {
fmt.Println(s.Name, " ", s.Shares, " ", s.Value)
}
}