bank

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

README

bank

Bank package for the Master Go course

Documentation

Overview

Package bank was made for the Master Go course at appliedgo.com.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Balance

func Balance(a *Account) int

Balance returns the current balance of account a.

func Deposit

func Deposit(a *Account, m int) (int, error)

Deposit adds amount m to account a's balance. The amount must be positive.

func History

func History(a *Account) func() (amt, bal int, more bool)

History returns a closure that returns one account transaction at a time. On each call, the closure returns the amount of the transaction, the resulting balance, and a boolean that is true as long as there are more history elements to read. The closure returns the history items from oldest to newest. The closure panics if it is called again after its third return value has turned "false".

func ListAccounts

func ListAccounts() string

ListAccounts returns a formatted string that lists each account and its current balance.

func Load

func Load() error

Load restores the accounts map from disk.

func Name

func Name(a *Account) string

Name returns the name of account a.

func Save

func Save() (err error)

Save stores the accounts map on disk.

func Transfer

func Transfer(a, b *Account, m int) (int, int, error)

Transfer transfers amount m from account a to account b. The amount must be positive. The sending account must have at least as much money as the amount to be transferred.

func Withdraw

func Withdraw(a *Account, m int) (int, error)

Withdraw removes amount m from account a's balance. The amount must be positive.

Types

type Account

type Account struct {
	Name string
	Bal  int
	Hist []history
}

func GetAccount

func GetAccount(name string) (*Account, error)

GetAccount receives a name and returns the account of that name, if it exists. GetAccount panics if the bank has no accounts.

func NewAccount

func NewAccount(s string) *Account

NewAccount creates a new account with a name. Initial balance is 0. The new account is added to the bank's map of accounts.

Example
a := NewAccount("Test")
fmt.Println(Name(a))
Output:

Test

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL