J.10. Class BalanceInquiry
Class BalanceInquiry (Fig. J.9) inherits from TRansaction and represents an ATM balance inquiry transaction (line 3). BalanceInquiry does not have any attributes of its own, but it inherits transaction attributes accountNumber, screen and bankDatabase, which are accessible through transaction's public read-only properties. The BalanceInquiry constructor (lines 68) takes arguments corresponding to these attributes and forwards them to TRansaction's constructor by invoking the constructor initializer with keyword base (line 8). The body of the constructor is empty.
Figure J.9. Class BalanceInquiry represents a balance inquiry ATM transaction.
1 // BalanceInquiry.cs 2 // Represents a balance inquiry ATM transaction 3 public class BalanceInquiry : Transaction 4 { 5 // five-parameter constructor initializes base class variables 6 public BalanceInquiry( int userAccountNumber, 7 Screen atmScreen, BankDatabase atmBankDatabase ) 8 : base( userAccountNumber, atmScreen, atmBankDatabase ) {} 9 10 // performs transaction; overrides Transaction's abstract method 11 public override void Execute() 12 { 13 // get the available balance for the current user's Account 14 decimal availableBalance = 15 Database.GetAvailableBalance( AccountNumber ); 16 17 // get the total balance for the current user's Account 18 decimal totalBalance = Database.GetTotalBalance( AccountNumber ); 19 20 // display the balance information on the screen 21 UserScreen.DisplayMessageLine( " Balance Information:" ); 22 UserScreen.DisplayMessage( " - Available balance: " ); 23 UserScreen.DisplayDollarAmount( availableBalance ); 24 UserScreen.DisplayMessage( " - Total balance: " ); 25 UserScreen.DisplayDollarAmount( totalBalance ); 26 UserScreen.DisplayMessageLine( "" ); 27 } // end method Execute 28 } // end class BalanceInquiry |
Class BalanceInquiry overrides transaction's abstract method Execute to provide a concrete implementation (lines 1127) that performs the steps involved in a balance inquiry. Lines 1415 obtain the specified Account's available balance by invoking the GetAvailableBalance method of the inherited property Database. Note that line 15 uses the inherited property AccountNumber to get the account number of the current user. Line 18 retrieves the specified Account's total balance. Lines 2126 display the balance information on the ATM's screen using the inherited property UserScreen. Recall that DisplayDollarAmount takes a decimal argument and outputs it to the screen formatted as a dollar amount with a dollar sign. For example, if a user's available balance is 1000.50M, line 23 outputs $1,000.50. Note that line 26 inserts a blank line of output to separate the balance information from subsequent output (i.e., the main menu repeated by class ATM after executing the BalanceInquiry).