From 8ed02350748ff4e0f503eb27481f05d8dfe204ff Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 11 Sep 2024 11:22:04 +0200 Subject: [PATCH] wallet: add withdraw method --- wallet/wallet.go | 4 ++++ wallet/wallet_test.go | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/wallet/wallet.go b/wallet/wallet.go index f2fb957..fca806e 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -16,6 +16,10 @@ func (w *Wallet) Deposit(amount Bitcoin) { w.balance += amount } +func (w *Wallet) Withdraw(amount Bitcoin) { + w.balance -= amount +} + func (w *Wallet) Balance() Bitcoin { return w.balance } diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index c575ec9..65e764c 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -3,14 +3,22 @@ package wallet import "testing" func TestWallet(t *testing.T) { - wallet := Wallet{} + assertBalance := func(t testing.TB, wallet Wallet, want Bitcoin) { + t.Helper() - wallet.Deposit(10) - - got := wallet.Balance() - want := Bitcoin(10) - - if got != want { - t.Errorf("got %q want %q", got, want) + got := wallet.Balance() + 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)) + }) }