Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "OTExample ATM/ATM"

(New page: <css> .source-java { border:#2F6FAB 1px dashed; background-color:#F9F9F9; padding:4px; } .source-otj { border:#2F6FAB 1px dashed; background-color:#F9F9F9; padding:4px; } </css> ===ATM wit...)
 
 
Line 3: Line 3:
 
.source-otj { border:#2F6FAB 1px dashed; background-color:#F9F9F9; padding:4px; }
 
.source-otj { border:#2F6FAB 1px dashed; background-color:#F9F9F9; padding:4px; }
 
</css>
 
</css>
===ATM with different treatment of accounts===
+
==ATM with different treatment of accounts==
  
 
<source lang="otj" line="1">
 
<source lang="otj" line="1">
Line 110: Line 110:
 
</source>
 
</source>
  
====Explanation====
+
===Explanation===
 
* Lines 1-44 apply straight-forward Java programming for implementing the basic behaviour of the ATM.
 
* Lines 1-44 apply straight-forward Java programming for implementing the basic behaviour of the ATM.
 
* Role '''ForeignAccount''' (l.55ff) implements the following adaptions for its base class ''Account'':
 
* Role '''ForeignAccount''' (l.55ff) implements the following adaptions for its base class ''Account'':
Line 117: Line 117:
 
* The above conditions are, however, not applied to all accounts but only to those belonging to a different Bank than the ATM's Bank.
 
* The above conditions are, however, not applied to all accounts but only to those belonging to a different Bank than the ATM's Bank.
 
** Different Accounts are distinguished by the '''guard predicate''' in the role's header (line 56) ({{otjld|5|.4}}).
 
** Different Accounts are distinguished by the '''guard predicate''' in the role's header (line 56) ({{otjld|5|.4}}).
** This guard is evaluated whenever a callin binding of this role is about to fire. For accounts of the local bank all callins are simply blocked.
+
** This guard is evaluated whenever any callin binding of this role is about to fire. Thus for accounts of the local bank all callins are simply blocked.
 
* A frequent question regards line 64: this '''base call''' ({{otjld|4|.3}}) invokes the original behaviour that has been intercepted by the current callin method. Note, that a base call ''always'' uses the name and signature of the enclosing method. Only by the callin method binding (line 76) the callin method and its base call are both bound to the method ''debit(int)'' of ''Account''. This indirection is needed so that the same callin method can be bound to several base methods. The base call will always invoke the base method on which a callin binding fired.
 
* A frequent question regards line 64: this '''base call''' ({{otjld|4|.3}}) invokes the original behaviour that has been intercepted by the current callin method. Note, that a base call ''always'' uses the name and signature of the enclosing method. Only by the callin method binding (line 76) the callin method and its base call are both bound to the method ''debit(int)'' of ''Account''. This indirection is needed so that the same callin method can be bound to several base methods. The base call will always invoke the base method on which a callin binding fired.

Latest revision as of 08:41, 24 February 2010

ATM with different treatment of accounts

  1. /**
  2.  * This team realizes the ATM.
  3.  * An ATM belongs to a certain bank and only for withdrawal from a foreign account
  4.  * an additional fee is debited. Furthermore it is not possible to query the balance
  5.  * of a foreign account.
  6.  */
  7. public team class ATM {
  8.     private Bank myBank;
  9.     private Account feeAccount;
  10.  
  11.     public ATM(Bank bank) {
  12.         myBank = bank;
  13.         feeAccount = new Account(bank);
  14.     }
  15.  
  16.     /**
  17.      * Returns the balance of the fee account.
  18.      */
  19.     int getFeeAccountBalance() {
  20.         return feeAccount.getBalance();
  21.     }
  22.  
  23.     /**
  24.      * Pays the given amount of cash from the given account, if it contains enough money.
  25.      */
  26.     public int payCash(Account account, int amount) {
  27.         boolean ok = account.debit(amount);
  28.         if (ok)
  29.             return amount;
  30.         else
  31.             return 0;
  32.     }
  33.  
  34.     /**
  35.      * Returns the balance of the given Account object. If an exception is thrown while 
  36.      * accessing the balance of the account this method throws an AccessDeniedException.
  37.      */
  38.     public int getBalance(Account account) throws AccessDeniedException {
  39.         try {
  40.             return account.getBalance();
  41.         } catch (Exception e) {
  42.             throw new AccessDeniedException();
  43.         }
  44.     }
  45.  
  46.     /**
  47.      * This role is responsible for the different handling of accounts belonging to a 
  48.      * different bank than the ATM does (foreign accounts).
  49.      *
  50.      * The guard predicate attached to the ForeignAccount role ensures that this role 
  51.      * and therefore all its callin bindings are only effective for foreign accounts. 
  52.      * It checks, if the bank of the base object to be lifted is different from the bank
  53.      * of the ATM.
  54.      */
  55.     public class ForeignAccount playedBy Account
  56.         base when (!(ATM.this.myBank.equals(base.getBank())))
  57.     {
  58.         /**
  59.          * This callin method calls its base method with the given amount plus an 
  60.          * additional fee.
  61.          */
  62.         callin boolean debitWithFee(int amount) {
  63.             int fee = calculateFee(amount);
  64.             if (base.debitWithFee(fee+amount)) {
  65.                 System.out.println("Debiting from a foreign account: " + 
  66.                                    "Additional fee of "+fee+" Euro will be debited!");
  67.                 feeAccount.credit(fee);
  68.                 return true;
  69.             }
  70.             return false;
  71.         }
  72.         /**
  73.          * Binds the role method debitWithFee to the base method debit of the
  74.          * base class Account.
  75.          */
  76.         debitWithFee <- replace debit;
  77.  
  78.         /**
  79.          * Restricting the query of balance, is realized as another callin method
  80.          * denying the call to getBalance for foreign accounts. Because of the role
  81.          * predicate, this callin method is not called for own accounts.
  82.          */
  83.         callin int checkedGetBalance() {
  84.             throw new RuntimeException("Access to balance of foreign account not allowed!");
  85.         }
  86.  
  87.         /**
  88.          * Binds the role method checkedGetBalance to the base method getBalance of
  89.          * the base class Account.
  90.          */
  91.         checkedGetBalance <- replace getBalance;
  92.  
  93.         /**
  94.          * Returns the fee for debiting the given amount from a foreign account.
  95.          * Here this is a fixed fee of 5%.
  96.          */
  97.         public int calculateFee(int amount) {
  98.             int feePercent = 5;
  99.             return (amount/100)*feePercent;
  100.         }
  101.     }
  102. }

Explanation

  • Lines 1-44 apply straight-forward Java programming for implementing the basic behaviour of the ATM.
  • Role ForeignAccount (l.55ff) implements the following adaptions for its base class Account:
    • When method debit(int) is invoked the amount is increased by a variable fee (see calculateFee(int)), and the account is debited with the resulting total. The fee is then credited to a fixed feeAccount.
    • Any attempt to query the balance of the account will be prohibited by throwing a RuntimeException.
  • The above conditions are, however, not applied to all accounts but only to those belonging to a different Bank than the ATM's Bank.
    • Different Accounts are distinguished by the guard predicate in the role's header (line 56) (OTJLD §5.4).
    • This guard is evaluated whenever any callin binding of this role is about to fire. Thus for accounts of the local bank all callins are simply blocked.
  • A frequent question regards line 64: this base call (OTJLD §4.3) invokes the original behaviour that has been intercepted by the current callin method. Note, that a base call always uses the name and signature of the enclosing method. Only by the callin method binding (line 76) the callin method and its base call are both bound to the method debit(int) of Account. This indirection is needed so that the same callin method can be bound to several base methods. The base call will always invoke the base method on which a callin binding fired.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.