-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made a functional Bitcoin Wallet class and learned
how pointers work in Go in the process!
- Loading branch information
1 parent
907a178
commit 2e8ea39
Showing
6 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package wallet | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds") //var keyword signfies the variable as global to the package | ||
//also errors are values in go so we can encapsulate them into variables like done here! | ||
|
||
type Bitcoin int | ||
|
||
type Stringer interface { //This interface is defined in the fmt package and lets you define how your type is printed when used with the %s format string in prints. | ||
String() string | ||
} | ||
|
||
func (b Bitcoin) String() string { | ||
return fmt.Sprintf("%d BTC", b) | ||
} | ||
|
||
type Wallet struct { | ||
balance Bitcoin | ||
} | ||
|
||
//dereferencing in Go is automatic | ||
|
||
// If you don't make the method receiever a pointer then whenever you call it, it will make a copy of the wallet and not directly affect the variables you want them to | ||
func (w *Wallet) Deposit(amount Bitcoin) { | ||
//fmt.Printf("address of balance in Deposit is %v \n", &w.balance) | ||
w.balance += amount | ||
} | ||
|
||
func (w *Wallet) Withdraw(amount Bitcoin) error { | ||
if amount > w.balance { | ||
return ErrInsufficientFunds // creates a new error with a message of my choosing, which in this case is "oh no" | ||
} | ||
|
||
w.balance -= amount | ||
return nil | ||
} | ||
|
||
func (w *Wallet) Balance() Bitcoin { | ||
return w.balance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package wallet | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestWallet(t *testing.T) { | ||
|
||
assertBalance := func(t testing.TB, wallet Wallet, want Bitcoin) { | ||
t.Helper() | ||
got := wallet.Balance() | ||
|
||
if got != want { | ||
t.Errorf("got %s want %s", got, want) | ||
} | ||
|
||
} | ||
|
||
assertError := func(t testing.TB, got error, want error) { | ||
t.Helper() | ||
if got == nil { | ||
t.Fatal("wanted an error but didn't get one :(") | ||
} | ||
if got != want { | ||
t.Errorf("got %q, want %q", got, want) | ||
} | ||
} | ||
|
||
assertNoError := func(t testing.TB, got error) { | ||
t.Helper() | ||
if got != nil { | ||
t.Fatal("got an error but didn't want one") | ||
} | ||
|
||
} | ||
t.Run("deposit", func(t *testing.T) { | ||
wallet := Wallet{} | ||
|
||
wallet.Deposit(Bitcoin(10)) | ||
|
||
assertBalance(t, wallet, Bitcoin(10)) | ||
}) | ||
|
||
t.Run("withdraw", func(t *testing.T) { | ||
wallet := Wallet{balance: Bitcoin(20)} | ||
|
||
err := wallet.Withdraw(Bitcoin(10)) | ||
|
||
assertNoError(t, err) | ||
assertBalance(t, wallet, Bitcoin(10)) | ||
}) | ||
|
||
t.Run("withdraw insufficient funds", func(t *testing.T) { | ||
startingBalance := Bitcoin(20) | ||
wallet := Wallet{startingBalance} | ||
err := wallet.Withdraw(Bitcoin(100)) | ||
|
||
assertError(t, err, ErrInsufficientFunds) | ||
assertBalance(t, wallet, startingBalance) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.