J.9. Class Transaction
Class transaction (Fig. J.8) is an abstract base class that represents the notion of an ATM transaction. It contains the common features of derived classes BalanceInquiry, Withdrawal and Deposit. This class expands on the "skeleton" code first developed in Section 11.9. Line 3 declares this class to be abstract. Lines 57 declare the class's private instance variables. Recall from the class diagram of Fig. 11.22 that class transaction contains the property AccountNumber that indicates the account involved in the TRansaction. Line 5 implements the instance variable accountNumber to maintain the AccountNumber property's data. We derive attributes screen (implemented as instance variable userScreen in line 6) and bankDatabase (implemented as instance variable database in line 7) from class transaction's associations, modeled in Fig. 11.21. All transactions require access to the ATM's screen and the bank's database.
Figure J.8. abstract base class TRansaction represents an ATM transaction.
1 // Transaction.cs 2 // Abstract base class Transaction represents an ATM transaction. 3 public abstract class Transaction 4 { 5 private int accountNumber; // account involved in the transaction 6 private Screen userScreen; // reference to ATM's screen 7 private BankDatabase database; // reference to account info database 8 9 // three-parameter constructor invoked by derived classes 10 public Transaction( int userAccount, Screen theScreen, 11 BankDatabase theDatabase ) 12 { 13 accountNumber = userAccount; 14 userScreen = theScreen; 15 database = theDatabase; 16 } // end constructor 17 18 // read-only property that gets the account number 19 public int AccountNumber 20 { 21 get 22 { 23 return accountNumber; 24 } // end get 25 } // end property AccountNumber 26 27 // read-only property that gets the screen reference 28 public Screen UserScreen 29 { 30 get 31 { 32 return userScreen; 33 } // end get 34 } // end property UserScreen 35 36 // read-only property that gets the bank database reference 37 public BankDatabase Database 38 { 39 get 40 { 41 return database; 42 } // end get 43 } // end property Database 44 45 // perform the transaction (overridden by each derived class) 46 public abstract void Execute(); // no implementation here 47 } // end class Transaction |
Class transaction has a constructor (lines 1016) that takes the current user's account number and references to the ATM's screen and the bank's database as arguments. Because transaction is an abstract class (line 3), this constructor is never called directly to instantiate transaction objects. Instead, this constructor is invoked by the constructors of the TRansaction derived classes via constructor initializers.
Class TRansaction has three public read-only propertiesAccountNumber (lines 1925), UserScreen (lines 2834) and Database (lines 3743). Derived classes of transaction inherit these properties and use them to gain access to class transaction's private instance variables. Note that we chose the names of the UserScreen and Database properties for claritywe wanted to avoid property names that are the same as the class names Screen and BankDatabase, which can be confusing.
Class transaction also declares abstract method Execute (line 46). It does not make sense to provide an implementation for this method in class transaction, because a generic transaction cannot be executed. Thus, we declare this method to be abstract, forcing each transaction concrete derived class to provide its own implementation that executes the particular type of transaction.
J 10 Class BalanceInquiry
|