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