go-by-test/wallet/wallet.go

35 lines
521 B
Go
Raw Normal View History

package wallet
2024-09-11 09:26:39 +00:00
import (
"errors"
"fmt"
)
2024-09-11 09:17:23 +00:00
2024-09-11 09:15:24 +00:00
type Bitcoin int
var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds")
2024-09-11 09:17:23 +00:00
func (b Bitcoin) String() string {
return fmt.Sprintf("%d BTC", b)
}
type Wallet struct {
2024-09-11 09:15:24 +00:00
balance Bitcoin
}
2024-09-11 09:15:24 +00:00
func (w *Wallet) Deposit(amount Bitcoin) {
w.balance += amount
}
2024-09-11 09:26:39 +00:00
func (w *Wallet) Withdraw(amount Bitcoin) error {
if w.balance < amount {
return ErrInsufficientFunds
2024-09-11 09:26:39 +00:00
}
2024-09-11 09:22:04 +00:00
w.balance -= amount
2024-09-11 09:26:39 +00:00
return nil
2024-09-11 09:22:04 +00:00
}
2024-09-11 09:15:24 +00:00
func (w *Wallet) Balance() Bitcoin {
return w.balance
}