banking

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bank

type Bank struct {
	// contains filtered or unexported fields
}

Bank represents an institution that offers basic financial accounts to customers. For the sake of simplicity in this demo, a given bank only has a single customer. This source file contains the business logic for managing the account and persisting the balance across sessions. The service.go file contains logic for exposing methods for account management over a network through a basic HTTP API. Implementation note: At present, idempotency keys are only retained for the lifetime of the service; they are not persisted during the current session and therefore values used then are not available for checking in the next session.

func NewBank

func NewBank(name string) *Bank

NewBank returns a Bank instance for the named account. The balance for that account will be the same as in the previous session, or if there was no previous session, it will be zero (in which case you might call the Deposit method to provide initial funding).

func (*Bank) Deposit

func (bank *Bank) Deposit(amount int, idempotencyKey string) (string, error)

Deposit adds the specified amount to the balance. The idempotency key is used to identify duplicate requests. This returns the transaction ID if successful or will return an error if the amount is invalid (zero or negative).

func (*Bank) GetBalance

func (bank *Bank) GetBalance() int

GetBalance returns the current account balance

func (*Bank) GetDataPath

func (bank *Bank) GetDataPath() string

GetDataPath returns the path of the file where account data is persisted

func (*Bank) GetName

func (bank *Bank) GetName() string

GetName returns the name used when creating the instance

func (*Bank) Withdraw

func (bank *Bank) Withdraw(amount int, idempotencyKey string) (string, error)

Withdraw removes the specified amount from the balance. The idempotency key is used to identify duplicate requests. This returns a transaction ID if successful or will return an error if the amount is invalid (either negative or greater than the current balance).

type BankClient

type BankClient struct {
	// contains filtered or unexported fields
}

BankClient allows a caller to invoke operations (such as Withdraw and Deposit) provided by a banking service available via a network.

func NewBankClient

func NewBankClient(host string, port int) *BankClient

NewBankClient creates a BankClient and returns a pointer to it.

func (*BankClient) Deposit

func (client *BankClient) Deposit(amount int, idempotencyKey string) (string, error)

Deposit calls the banking service, requesting that it adds the specified amount to the balance. The idempotency key is used to identify duplicate requests. This returns the transaction ID if successful or an error if it was not.

func (*BankClient) GetBalance

func (client *BankClient) GetBalance() (int, error)

GetBalance returns the current account balance

func (*BankClient) GetName

func (client *BankClient) GetName() (string, error)

GetName returns the name of the bank that the client will access

func (*BankClient) IsServiceRunning

func (client *BankClient) IsServiceRunning() bool

IsServiceRunning returns true if the service is available, false otherwise

func (*BankClient) Withdraw

func (client *BankClient) Withdraw(amount int, idempotencyKey string) (string, error)

Withdraw removes the specified amount from the balance. The idempotency key is used to identify duplicate requests. This returns a transaction ID if successful or will return an error if the amount is invalid (either negative or greater than the current balance).

type BankingService

type BankingService struct {
	// contains filtered or unexported fields
}

BankingService represents account operations that a specific bank allows one to invoke over a network connection.

func NewBankingService

func NewBankingService(bank *Bank, port int) *BankingService

NewBankingService creates a new BankingService and returns a pointer to it.

func (*BankingService) Shutdown

func (svc *BankingService) Shutdown() error

Shutdown stops the BankingService, preventing it from handling client reqests

func (*BankingService) Start

func (svc *BankingService) Start() error

Start starts the BankingService, allowing it to handle client reqests

type InsufficientFundsError

type InsufficientFundsError struct {
	// contains filtered or unexported fields
}

InsufficientFundsError occurs when an account lacks the funds to successfully perform the requested operation.

func (InsufficientFundsError) Error

func (e InsufficientFundsError) Error() string

Jump to

Keyboard shortcuts

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