DCIprogram: #BBLoanSyndicate

" Exported from Squeak Squeak4.5-13680 last modified on 24 February 2019 by: sumim"

Printer friendly version

projection: Data

Class: BBFacility

Object subclass: #BBFacility
    instanceVariableNames: 'limit loan shares'
    category: 'BBLoanSyndicate-Data'

instanceMethods: initialization

initialize
    shares := Dictionary new

instanceMethods: accessing

limit
    "Answer the value of limit"

    ^ limit

limit: anObject
    "Set the value of limit"

    limit := anObject

loan
    "Answer the value of loan"

    ^ loan

loan: anObject
    "Set the value of loan"

    loan := anObject

shares
    "Answer the value of shares"

    ^ shares

shares: anObject
    "Set the value of shares"

    shares := anObject

Class: BBLoan

Object subclass: #BBLoan
    instanceVariableNames: 'shares'
    category: 'BBLoanSyndicate-Data'

instanceMethods: initialization

initialize
    shares := Dictionary new

instanceMethods: accessing

shares
    "Answer the value of shares"

    ^ shares

shares: anObject
    "Set the value of shares"

    shares := anObject

projection: Context

Class: BBLoanSyndicateCtx

BB1Context subclass: #BBLoanSyndicateCtx
    instanceVariableNames: 'facility'
    category: 'BBLoanSyndicate-Context'

instanceMethods: accessing

facility
    "Answer the value of facility"

    ^ facility

facility: anObject
    "Set the value of facility"

    facility := anObject

instanceMethods: role binding

AmountPie
    ^facility loan

Lender
    ^facility

PercentagePie
    ^facility

instanceMethods: initialization

buildFacility
    facility := BBFacility new.
    facility loan: BBLoan new

joinFacility: company percentage: int
    facility shares at: company put: int.
    facility loan shares at: company put: 0

instanceMethods: triggers

draw: amount
    self triggerInteractionFrom: #Lender with: #draw: andArgs: {amount}

pay: amount
    self triggerInteractionFrom: #Lender with: #pay: andArgs: {amount}


Interaction: BBLoanSyndicateCtx

No diagram

roleMethods: Lender

instanceMethods: role methods

draw: amount
    AmountPie increase: amount.
    self limit: self limit - amount

pay: amount
    AmountPie decrease: amount.
    self limit: self limit + amount

roleMethods: AmountPie

instanceMethods: role methods

decrease: amount
    | shares total |
    shares := self shares.
    total := shares sum asFloat.
    shares keysDo: [:company |
        | current |
        current := shares at: company.
        shares at: company put: current - (amount * (current / total))]

increase: amount
    PercentagePie shares associationsDo: [:assoc |
        | company percentage |
        company := assoc key.
        percentage := assoc value / 100 asFloat.
        self shares
            at: company
            put: (self shares at: company) + (amount * percentage)]



projection: Testing

Class: BBLoanSyndicateTesting

Object subclass: #BBLoanSyndicateTesting
    instanceVariableNames: ''
    category: 'BBLoanSyndicate-Testing'

classMethods: examples

example
    "BBLoanSyndicateTesting example"
    | syndicate |
    syndicate := BBLoanSyndicateCtx new buildFacility.
    syndicate joinFacility: #A percentage: 50.
    syndicate joinFacility: #B percentage: 30.
    syndicate joinFacility: #C percentage: 20.
    syndicate facility limit: 100000.

    syndicate draw: 10000.
    syndicate pay: 5000.

    World findATranscript: nil.
    syndicate facility loan shares associations sort
        do: [:assoc | Transcript cr; show: assoc]



Top
Data
BBFacility
BBLoan
Context
BBLoanSyndicateCtx
Testing
BBLoanSyndicateTesting