-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallet.go
43 lines (35 loc) · 958 Bytes
/
wallet.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
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
type Wallet struct {
ID int
Wallet_balance decimal.Decimal
}
type WStorage interface {
Update(wallet Wallet) error
Get(id int) (*Wallet, error)
}
func (w *Wallet) Balance() decimal.Decimal {
w.Wallet_balance = decimal.NewFromFloat(w.Wallet_balance.InexactFloat64())
return w.Wallet_balance
}
func (w *Wallet) Debit(amount decimal.Decimal) error {
if amount.IsNegative() {
x := fmt.Errorf("debit amount can not be negative")
return x
}
w.Wallet_balance = decimal.Sum(w.Wallet_balance, amount)
return nil
}
func (w *Wallet) Credit(amount decimal.Decimal) error {
if amount.GreaterThan(w.Wallet_balance) {
return fmt.Errorf("credit amount can not be higher than balance")
} else if amount.IsNegative() {
return fmt.Errorf("credit amount can not be negative")
}
amount = amount.Neg()
w.Wallet_balance = decimal.Sum(w.Wallet_balance, amount)
return nil
}