Created
June 5, 2019 17:49
-
-
Save joaoh82/81830298fce63e6ecc2df608f4d79577 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "errors" | |
| "fmt" | |
| "testing" | |
| "github.com/stretchr/testify/require" | |
| ) | |
| // BankAccount represents a bank account instance | |
| type BankAccount interface { | |
| // Balance returns the current balance of the account | |
| Balance() (float64, error) | |
| // Deposit deposits money onto the account | |
| Deposit(value float64) error | |
| // Withdraw widthdraws money from the account | |
| Withdraw(value float64) error | |
| } | |
| // NewBankAccount creates a new bank account instance | |
| func NewBankAccount(id string) (BankAccount, error) { | |
| return newBankAccount(id) | |
| } | |
| func newBankAccount(id string) (*bankAccount, error) { | |
| if len(id) < 1 { | |
| return nil, errors.New("invalid bank account id") | |
| } | |
| return &bankAccount{ | |
| id: id, | |
| blocked: false, | |
| balance: 0, | |
| }, nil | |
| } | |
| type bankAccount struct { | |
| // Private fields | |
| id string | |
| blocked bool | |
| balance float64 | |
| } | |
| func (ba *bankAccount) checkBlocked() error { | |
| if ba.blocked { | |
| return fmt.Errorf("account %s is blocked", ba.id) | |
| } | |
| return nil | |
| } | |
| func (ba *bankAccount) Balance() (float64, error) { | |
| if err := ba.checkBlocked(); err != nil { | |
| return 0, err | |
| } | |
| return ba.balance, nil | |
| } | |
| func (ba *bankAccount) Deposit(value float64) error { | |
| if err := ba.checkBlocked(); err != nil { | |
| return err | |
| } | |
| if value > 0 { | |
| ba.balance += value | |
| return nil | |
| } | |
| return fmt.Errorf("invalid deposit value: %f", value) | |
| } | |
| func (ba *bankAccount) Withdraw(value float64) error { | |
| if err := ba.checkBlocked(); err != nil { | |
| return err | |
| } | |
| if ba.balance-value < 0 { | |
| return errors.New("insufficient funds") | |
| } | |
| ba.balance -= value | |
| return nil | |
| } | |
| // TestDepositAndWithdrawal tests BankAccount.Deposit and BankAccount.Withdraw | |
| func TestWithdrawal(t *testing.T) { | |
| account, err := NewBankAccount("ABC") | |
| require.NoError(t, err) | |
| require.NotNil(t, account) | |
| b1, err := account.Balance() | |
| require.NoError(t, err) | |
| require.Equal(t, 0.0, b1) | |
| require.NoError(t, account.Deposit(4.5)) | |
| b2, err := account.Balance() | |
| require.NoError(t, err) | |
| require.Equal(t, 4.5, b2) | |
| require.NoError(t, account.Withdraw(2.5)) | |
| b3, err := account.Balance() | |
| require.NoError(t, err) | |
| require.Equal(t, 2.0, b3) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment