go-by-test/wallet/wallet.go

35 lines
521 B
Go

package wallet
import (
"errors"
"fmt"
)
type Bitcoin int
var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds")
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 ErrInsufficientFunds
}
w.balance -= amount
return nil
}
func (w *Wallet) Balance() Bitcoin {
return w.balance
}