atm

package module
v0.0.0-...-c81b65b Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

atm

An ATM in the console

Usage

Start the ./atm binary and enter one of the following commands:

authorize <account_id> <pin>
withdraw <amount>
deposit <amount>
balance
history
logout
end

Development

To compile the code execute execute the following command in the project root directory:

go build -o atm cmd/atm/main.go

A file called ./atm will appear in your current working directory to run this file simply execute it:

./atm

Testing

To run all tests in the suite execute the following in the project root directory:

go test ./...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthorizationUnsuccessful = errors.New("Authorization failed.")
	ErrAuthorizationRequired     = errors.New("Authorization required.")
)

authorize errors

View Source
var (
	ErrWithdrawATMInsufficientFunds = errors.New("Unable to dispense full amount requested at this time.")
	ErrWithdrawATMNoFunds           = errors.New("Unable to process your withdrawal at this time.")
	ErrWithdrawAccountOverdrawn     = errors.New("Your account is overdrawn! You may not make withdrawals at this time.")
	ErrWithdrawAmountNoMultipleOf20 = errors.New("Unable to process since amount is not a multiple of 20.")
)

withdraw errors

View Source
var (
	ErrAccountNotFound        = errors.New("Account could not be found in database.")
	ErrAccountIDNotInteger    = errors.New("Account ID is not an integer")
	ErrAccountBalanceNotFloat = errors.New("Balance is not a float")
)

account db error

View Source
var (
	ErrTransactionAccountIDNotInteger = errors.New("Account ID is not an integer")
	ErrTransactionDateTimeNotInteger  = errors.New("Datetime is not an integer")
	ErrTransactionAmounteNotFloat     = errors.New("Amount is not a float")
	ErrTransactionBalanceNotFloat     = errors.New("Balance is not a float")
)

transaction db error

View Source
var (
	ErrSessionNoActiveSession  = errors.New("No active session found. Authorization required.")
	ErrSessionInvalidAccountID = errors.New("Invalid account ID for session.")
	ErrSessionTimedOut         = errors.New("Session has timed out.")
)

session error

View Source
var (
	ErrConsoleInvalidCommand      = errors.New("Invalid command. e.g. ")
	ErrConsoleAuthorizationFailed = errors.New("Authorization failed.")
	ErrConsoleInvalidAmount       = errors.New("Amount is not a valid")
)

console error

View Source
var (
	ErrLogoutNoActiveSession = errors.New("No account is currently authorized.")
)

logout errors

Functions

func RunCommand

func RunCommand(atm *ATM, command string) error

RunCommand will execute the command given the command string If an invalid command is used then the help message is displayed

Types

type ATM

type ATM struct {
	AccountDB     IAccountDB
	TransactionDB ITransactionDB
	ATMBalance    float64
	Session       *Session
}

func (*ATM) Authorize

func (atm *ATM) Authorize(accountID int, accountPIN string) bool

Authorize authorizes the accountID with the accountPIM If pin and accountID is correct then a session is created that should expire in 2 mins

func (*ATM) Balance

func (atm *ATM) Balance(accountID int) (float64, error)

Balance returns the current balance An error is returned if no active session or account balance could not be found in DB

func (*ATM) Deposit

func (atm *ATM) Deposit(accountID int, amount float64) error

Deposit deposits a specific amount to the account and updates the AccountDB and TransactionDB An error is returned if no actives session or failure to interface with DBs

func (*ATM) History

func (atm *ATM) History(accountID int) []Transaction

History returns the history of a specific account If error occurs an empty list is returned

func (*ATM) Logout

func (atm *ATM) Logout() error

Logout logouts of the current session An error is returned if no active session could be closed

func (*ATM) Withdraw

func (atm *ATM) Withdraw(accountID int, amount int) (bool, error)

Withdraw withraws a specific amount from an account and updates value in AccountsDB and TransactionDB Withdrawl will fail if session not active or session timed out, atm balance is 0, withdrawl amount is more than atm balance, account current balance is negative, If withdrawl amount is more than account's balance then overdrawn boolean is true

type Account

type Account struct {
	AccountID int
	PIN       string
	Balance   float64
}

type AccountDB

type AccountDB struct {
	DBFile string
}

func (AccountDB) Get

func (a AccountDB) Get(accountID int) (Account, error)

Get returns a specific account from the CSV file if account cannot be found then an error is returned

func (AccountDB) Set

func (a AccountDB) Set(account Account) error

Set returns an error if the account was not updated in the CSV file set will override the CSV row that represents this account

type Command

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

func DefaultCommands

func DefaultCommands() []Command

DefaultCommands returns all of the commands valid in the console

func (Command) Name

func (c Command) Name() string

Name shows the name of the command

func (Command) Run

func (c Command) Run(atm *ATM, args []string) error

Run executes the command with the provided arguments An error is returned when the command failed to run

type CommandRun

type CommandRun func(*ATM, []string) error

type IATM

type IATM interface {
	Authorize(int, string) bool
	Withdraw(Account, int) (bool, error)
	Deposit(Account, float64) error
	Balance(Account)
	History(Account) []Transaction
	Logout(bool) error
	// contains filtered or unexported methods
}

type IAccountDB

type IAccountDB interface {
	// Return error if account cannot be found
	Get(int) (Account, error)
	// Return err if account could not be updated
	Set(Account) error
}

type ICommand

type ICommand interface {
	Name() string
	Run(*ATM, []string) error
}

type ISession

type ISession interface {
	Authorize(int)
	TimedOut() bool
	Refresh()
	Logout() bool
	Valid(int) error
}

type ITransactionDB

type ITransactionDB interface {
	// return all transactions for a given account
	Get(int) ([]Transaction, error)
	// add a transaction return error if failure to add transaction
	Set(Transaction) error
}

type Session

type Session struct {
	// When the session started in epoch
	LastActivity int64

	AccountID int
}

func (*Session) Authorize

func (s *Session) Authorize(accountID int)

Authorize will set the LastActivity time to now and the AccountID of the session

func (*Session) LogOut

func (s *Session) LogOut() error

LogOut will logout of the session

func (*Session) Refresh

func (s *Session) Refresh()

Refresh updates the LastActivity to now

func (*Session) TimedOut

func (s *Session) TimedOut() bool

TimedOut checks if the session has timed out after 2 mins

func (*Session) Valid

func (s *Session) Valid(accountID int) error

Valid will validate the session exists and not timed out and will refresh the LastActivity

type Transaction

type Transaction struct {
	AccountID int
	DateTime  int64
	Amount    float64
	Balance   float64
}

type TransactionDB

type TransactionDB struct {
	DBFile string
}

func (TransactionDB) Get

func (t TransactionDB) Get(accountID int) ([]Transaction, error)

Get retrieves a list of transactions for a given accountID from a CSV file An error is returned on failure to read the CSV file

func (TransactionDB) Set

func (t TransactionDB) Set(transaction Transaction) error

Set appends a transaction to the transactions CSV file An error is returned on failure to read or write the CSV file

Directories

Path Synopsis
cmd
atm command

Jump to

Keyboard shortcuts

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