Skip to content

Commit

Permalink
Made a functional Bitcoin Wallet class and learned
Browse files Browse the repository at this point in the history
how pointers work in Go in the process!
  • Loading branch information
vedicpanda committed May 30, 2023
1 parent 907a178 commit 2e8ea39
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ArraysSlices/sum.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// More important information on slices here: https://go.dev/blog/slices-intro
package main

func Sum(numbers []int) int {
Expand Down Expand Up @@ -39,4 +40,5 @@ func SumAllTails(numbersToSum ...[]int) []int {
}
return sums
}

//Testing things
1 change: 1 addition & 0 deletions ArraysSlices/sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestSum(t *testing.T) {
})

t.Run("collection of any size", func(t *testing.T) {
//This is a slice
numbers := []int{1, 2, 3}
got := Sum(numbers)
want := 6
Expand Down
44 changes: 44 additions & 0 deletions PointersErrors/wallet.go
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
}
61 changes: 61 additions & 0 deletions PointersErrors/wallet_test.go
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)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Rectangle struct {
}

//This is a method specific to the rectangle struct
//A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver's base type.
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
Expand Down
File renamed without changes.

0 comments on commit 2e8ea39

Please sign in to comment.