This project is read-only.
Bank account

Category:
Application
Description: Bank account with threadsafe deposit and withdraw feature. This implementation generates the object according to its internal state.
Code:
type IAccount = 
abstract Withdraw : int -> unit
abstract Deposit : int -> unit
type AccountInfo = {
Balance: int
}

type BankAccount (initialBalance) =
let mutable accountInfo = { Balance = initialBalance }
member this.AccountInfo
with get() = accountInfo
member this.GetCommand() =
if accountInfo.Balance >= 0 then
{ new IAccount with
member this.Withdraw(amount) =
if (amount > accountInfo.Balance) then printfn "cannot withdraw"
else accountInfo <- { accountInfo with Balance = accountInfo.Balance - amount }
member this.Deposit(amount) =
accountInfo <- { accountInfo with Balance = accountInfo.Balance + amount } }
else
{ new IAccount with
member this.Withdraw(amount) =
printfn "not enough fund"
member this.Deposit(amount) =
accountInfo <- { accountInfo with Balance = accountInfo.Balance + amount } }

let Application1() =
let account = BankAccount(-1)
account.GetCommand().Withdraw(1)
account.GetCommand().Deposit(100)
account.GetCommand().Withdraw(5)

Execution Result:
not enough fund

Last edited Sep 14, 2011 at 4:30 AM by ttliu2000, version 1

Comments

No comments yet.