,

DCIprogram: #PayBills

" Exported from Squeak 7179-basic.226 last modified on 3 November 2014 by: Trygve"

Reader friendly version

projection: Context

Class: PayBillsCtx

Context subclass: #PayBillsCtx
    instanceVariableNames: 'billCollection'
    category: 'PayBills-Context'

" This Context implements the system operation bank: bnk payBills: billCollection.
This Context illustrates that a RoleMethod may call a system operation realized by another Context.


Roles:
    
BILLPAYER Responsible for providing bills and pay them.
    
TELLER Responsible for performing the actual transfer of money.
        This role is played by the
BB5MoneyTransferContext in this version.

For further information, see Data>
BillCollection. "

instanceMethods: operations

payBills: billColl
    billCollection := billColl.
    self triggerInteractionFrom: #BILLPAYER with: #payBills.

instanceMethods: role binding

BILLPAYER
    ^billCollection

TELLER
    ^BB5aMoneyTransferContext new


Interaction: PayBillsCtx

No diagram

roleMethods: BILLPAYER

instanceMethods: role methods

payBills
    " This example illustrates that a RoleMethod "
    " may call a system operation realized by another Context. "
    self billCollection do:
        [:bill |
            TELLER transferTransaction:
                (BB5aTransaction new
                        bank: bill bank
                        amount: bill amount
                        from: bill fromAccountNo
                        to: bill toAccountNo)].



projection: Data

Class: Bill

Object subclass: #Bill
    instanceVariableNames: 'bank fromAccountNo toAccountNo amount'
    category: 'PayBills-Data'

" A bill to be payed.

Instance variables:
    fromAccount (Object) account name
    toAccount (Object) account name
    bank. Both accounts are in the same bank.
    amount (Integer): the amount to be payed.

For further information, see Data>BillCollection. "

instanceMethods: attributes-read

amount
    ^amount

bank
    ^bank

fromAccountNo
    ^fromAccountNo

toAccountNo
    ^toAccountNo

instanceMethods: attributes-write

amount: money
    amount:= money.

bank: bnk
    bank := bnk.

fromAccountNo: accNo
    fromAccountNo := accNo.

toAccountNo: accNo
    toAccountNo := accNo.

Class: BillCollection

Object subclass: #BillCollection
    instanceVariableNames: 'billCollection'
    category: 'PayBills-Data'

" In this example, the BillCollection has a list of bills to be payed. Each bill has a fromAccount, a toAccount and an amount. The solution reuses the BB5aMoneyTransferContext for the actual money transfer. "

instanceMethods: access

addBill: aBill
    billCollection add: aBill.

billCollection
    ^billCollection

instanceMethods: private

initialize
    super initialize.
    billCollection := OrderedCollection new.

projection: Testing

Class: Testing

Object subclass: #Testing
    instanceVariableNames: ''
    category: 'PayBills-Testing'

" See comment in Context >> PayBillsCtx. "

instanceMethods: operations

testPayBills1
    " Testing new testPayBills1. "
    | bank billCollection |
    bank := BB5aBank new.
    (bank addCheckingAccountNo: 1111) increase: 2000.
    bank addSavingsAccountNo: 2222.
    self assert:
            [(bank findAccount: 1111) balance = 2000.
            (bank findAccount: 2222) balance = 0].
    billCollection := BillCollection new.
    billCollection addBill: (Bill new bank: bank; fromAccountNo: 1111; toAccountNo: 2222; amount: 500).
    billCollection addBill: (Bill new bank: bank; fromAccountNo: 2222; toAccountNo: 1111; amount: 200).
    billCollection addBill: (Bill new bank: bank; fromAccountNo: 1111; toAccountNo: 2222; amount: 500).
    PayBillsCtx new payBills: billCollection.
    self assert:
            [(bank findAccount: 1111) balance = 1200 &
            (bank findAccount: 2222) balance = 800].
    self inform: 'Test OK'.

classMethods: instance creation

testPayBills1
    " Testing testPayBills1. "
    | bank billCollection |
    bank := BB5bBank new.
    (bank addCheckingAccountNo: 1111) increase: 2000.
    bank addSavingsAccountNo: 2222.
    self assert:
            [(bank findAccount: 1111) balance = 2000.
            (bank findAccount: 2222) balance = 0].
    billCollection := BillCollection new.
    billCollection addBill: (Bill new bank: bank; fromAccountNo: 1111; toAccountNo: 2222; amount: 500).
    billCollection addBill: (Bill new bank: bank; fromAccountNo: 2222; toAccountNo: 1111; amount: 200).
    billCollection addBill: (Bill new bank: bank; fromAccountNo: 1111; toAccountNo: 2222; amount: 500).
    PayBillsCtx new payBills: billCollection.
    self assert:
            [(bank findAccount: 1111) balance = 1200 &
            (bank findAccount: 2222) balance = 800].
    self inform: 'Test OK'.