-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwallet.go
43 lines (34 loc) · 805 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 (
"errors"
)
// Wallet struct
type Wallet struct {
Account string `storm:"id"`
Pin string `storm:"index"`
Balance int `storm:"index"`
}
// GetBalance function
func (w *Wallet) GetBalance() int {
return w.Balance
}
// AddBalance function
func (w *Wallet) AddBalance(amount int) {
w.Balance = w.Balance + amount
}
// RemoveBalance function
func (w *Wallet) RemoveBalance(amount int) {
w.Balance = w.Balance - amount
}
// SendBalance function
func (w *Wallet) SendBalance(receiver *Wallet, amount int) error {
if w.GetBalance()-amount < 0 {
return errors.New("Insufficient Sender Balance")
}
if amount < 0 {
return errors.New("You cannot send a negative amount")
}
w.Balance = w.Balance - amount
receiver.Balance = receiver.Balance + amount
return nil
}