wallet: add withdraw method

This commit is contained in:
vinchent 2024-09-11 11:22:04 +02:00
parent 36ef78fb92
commit 8ed0235074
2 changed files with 20 additions and 8 deletions

View File

@ -16,6 +16,10 @@ func (w *Wallet) Deposit(amount Bitcoin) {
w.balance += amount w.balance += amount
} }
func (w *Wallet) Withdraw(amount Bitcoin) {
w.balance -= amount
}
func (w *Wallet) Balance() Bitcoin { func (w *Wallet) Balance() Bitcoin {
return w.balance return w.balance
} }

View File

@ -3,14 +3,22 @@ package wallet
import "testing" import "testing"
func TestWallet(t *testing.T) { func TestWallet(t *testing.T) {
wallet := Wallet{} assertBalance := func(t testing.TB, wallet Wallet, want Bitcoin) {
t.Helper()
wallet.Deposit(10) got := wallet.Balance()
if got != want {
got := wallet.Balance() t.Errorf("got %q want %q", got, want)
want := Bitcoin(10) }
if got != want {
t.Errorf("got %q want %q", got, want)
} }
t.Run("deposit", func(t *testing.T) {
wallet := Wallet{}
wallet.Deposit(10)
assertBalance(t, wallet, Bitcoin(10))
})
t.Run("withdraw", func(t *testing.T) {
wallet := Wallet{balance: Bitcoin(20)}
wallet.Withdraw(10)
assertBalance(t, wallet, Bitcoin(10))
})
} }