-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.go
164 lines (130 loc) · 4.12 KB
/
money.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package money
import "errors"
// Money stores amount and currency value
type Money struct {
amount *Amount
currency *Currency
}
var calc = &calculator{}
const (
greaterThanCheckResult = 1
equalCheckResult = 0
lessThanCheckResult = -1
)
// New creates and returns a new Money instance
func New(amount int64, currency *Currency) *Money {
return &Money{
amount: &Amount{val: amount},
currency: currency,
}
}
// Currency returns the currency used by Money
func (m *Money) Currency() *Currency {
return m.currency
}
// Amount returns the amount value as int64
func (m *Money) Amount() *Amount {
return m.amount
}
// Add returns new Money struct with value representing sum of Self and Other Money
func (m *Money) Add(money *Money) (*Money, error) {
if err := m.assertSameCurrency(money); err != nil {
return nil, err
}
return &Money{amount: calc.add(m.Amount(), money.Amount()), currency: m.currency}, nil
}
// Subtract returns new Money struct with value representing difference of Self and Other Money
func (m *Money) Subtract(money *Money) (*Money, error) {
if err := m.assertSameCurrency(money); err != nil {
return nil, err
}
return &Money{amount: calc.subtract(m.Amount(), money.Amount()), currency: m.currency}, nil
}
// Multiply returns new Money struct with value representing Self multiplied value by multiplier
func (m *Money) Multiply(mul int64) *Money {
return &Money{amount: calc.multiply(m.Amount(), mul), currency: m.currency}
}
// Allocate returns slice of Money structs with split Self value in given ratios.
// It lets split money by given ratios without losing pennies and as Split operations distributes
// leftover pennies amongst the parties with round-robin principle.
func (m *Money) Allocate(rs ...int) ([]*Money, error) {
if len(rs) == 0 {
return nil, errors.New("No ratios specified")
}
// Calculate total of ratios
var total int
for _, r := range rs {
total += r
}
var remainder = m.Amount().Value()
var ms []*Money
for _, r := range rs {
m := &Money{
amount: calc.allocate(m.Amount(), r, total),
currency: m.currency,
}
ms = append(ms, m)
remainder -= m.Amount().Value()
}
for i := 0; i < int(remainder); i++ {
ms[i] = &Money{
amount: calc.add(ms[i].Amount(), &Amount{1}),
currency: ms[i].Currency(),
}
}
return ms, nil
}
// Equals checkes equality between two Money instances
func (m *Money) Equals(money *Money) (bool, error) {
if err := m.assertSameCurrency(money); err != nil {
return false, err
}
return m.compare(money) == 0, nil
}
// GreaterThan checks whether the value of Money is greater than the other
func (m *Money) GreaterThan(money *Money) (bool, error) {
if err := m.assertSameCurrency(money); err != nil {
return false, err
}
return m.compare(money) == greaterThanCheckResult, nil
}
// GreaterThanOrEqual checks whether the value of Money is greater or equal than the other
func (m *Money) GreaterThanOrEqual(money *Money) (bool, error) {
if err := m.assertSameCurrency(money); err != nil {
return false, err
}
return m.compare(money) >= equalCheckResult, nil
}
// LessThan checks whether the value of Money is less than the other
func (m *Money) LessThan(money *Money) (bool, error) {
if err := m.assertSameCurrency(money); err != nil {
return false, err
}
return m.compare(money) == lessThanCheckResult, nil
}
// LessThanOrEqual checks whether the value of Money is less or equal than the other
func (m *Money) LessThanOrEqual(money *Money) (bool, error) {
if err := m.assertSameCurrency(money); err != nil {
return false, err
}
return m.compare(money) <= equalCheckResult, nil
}
// Display returns a formatted amount string for the current currency
func (m *Money) Display() string {
return m.currency.Format(m.Amount().Value())
}
func (m *Money) assertSameCurrency(money *Money) error {
if !m.currency.equals(money.currency) {
return errors.New("Currency don't match")
}
return nil
}
func (m *Money) compare(money *Money) int {
if m.Amount().Value() > money.Amount().Value() {
return greaterThanCheckResult
}
if m.Amount().Value() < money.Amount().Value() {
return lessThanCheckResult
}
return equalCheckResult
}