33 lines
459 B
Go
33 lines
459 B
Go
package wallet
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
type Bitcoin int
|
|
|
|
func (b Bitcoin) String() string {
|
|
return fmt.Sprintf("%d BTC", b)
|
|
}
|
|
|
|
type Wallet struct {
|
|
balance Bitcoin
|
|
}
|
|
|
|
func (w *Wallet) Deposit(amount Bitcoin) {
|
|
w.balance += amount
|
|
}
|
|
|
|
func (w *Wallet) Withdraw(amount Bitcoin) error {
|
|
if w.balance < amount {
|
|
return errors.New("not enough to withdraw")
|
|
}
|
|
w.balance -= amount
|
|
return nil
|
|
}
|
|
|
|
func (w *Wallet) Balance() Bitcoin {
|
|
return w.balance
|
|
}
|