www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 1306 (continued)]

G.10. Class BalanceInquiry

Class BalanceInquiry (Figs. G.17G.18) derives from abstract base class TRansaction and represents a balance-inquiry ATM transaction. BalanceInquiry does not have any data members of its own, but it inherits TRansaction data members accountNumber, screen and bankDatabase, which are accessible through transaction's public get functions. Note that line 6 #includes the definition of base class transaction. The BalanceInquiry constructor (declared in line 11 of Fig. G.17 and defined in lines 813 of Fig. G.18) takes arguments corresponding to the transaction data members and simply forwards them to TRansaction's constructor, using base-class initializer syntax (line 10 of Fig. G.18). Line 12 of Fig. G.17 contains the function prototype for member function execute, which is required to indicate the intention to override the base class's pure virtual function of the same name.

Figure G.17. BalanceInquiry class definition.
(This item is displayed on page 1307 in the print version)

 1  // BalanceInquiry.h
 2  // BalanceInquiry class definition. Represents a balance inquiry.
 3  #ifndef BALANCE_INQUIRY_H
 4  #define BALANCE_INQUIRY_H
 5
 6  #include "Transaction.h" // Transaction class definition
 7
 8  class BalanceInquiry : public Transaction
 9  {
10  public:
11     BalanceInquiry( int, Screen &, BankDatabase & ); // constructor
12     virtual void execute(); // perform the transaction
13  }; // end class BalanceInquiry
14
15  #endif // BALANCE_INQUIRY_H

Figure G.18. BalanceInquiry class member-function definitions.
(This item is displayed on pages 1307 - 1308 in the print version)

 1  // BalanceInquiry.cpp
 2  // Member-function definitions for class BalanceInquiry.
 3  #include "BalanceInquiry.h" // BalanceInquiry class definition
 4  #include "Screen.h" // Screen class definition
 5  #include "BankDatabase.h" // BankDatabase class definition
 6
 7  // BalanceInquiry constructor initializes base-class data members
 8  BalanceInquiry:: BalanceInquiry( int userAccountNumber, Screen &atmScreen,
 9     BankDatabase &atmBankDatabase )
10     : Transaction( userAccountNumber, atmScreen, atmBankDatabase )
11  {
12     // empty body
13  } // end BalanceInquiry constructor
14
15  // performs transaction; overrides Transaction's pure virtual function
16  void BalanceInquiry::execute()
17  {
18     // get references to bank database and screen
19     BankDatabase &bankDatabase = getBankDatabase();
20     Screen &screen = getScreen();
21
22     // get the available balance for the current user's Account
23     double availableBalance =
24        bankDatabase.getAvailableBalance( getAccountNumber() );
25
26     // get the total balance for the current user's Account
27     double totalBalance =
28        bankDatabase.getTotalBalance( getAccountNumber() );
29
30     // display the balance information on the screen
31     screen.displayMessageLine( "\nBalance Information:" );
32     screen.displayMessage( " - Available balance: " );
33     screen.displayDollarAmount( availableBalance );
34     screen.displayMessage( "\n - Total balance:     " );
35     screen.displayDollarAmount( totalBalance );
36     screen.displayMessageLine( "" );
37  } // end function execute

Class BalanceInquiry overrides TRansaction's pure virtual function execute to provide a concrete implementation (lines 1637 of Fig. G.18) that performs the steps involved in a balance inquiry. Lines 1920 get references to the bank database and the ATM's screen by invoking member functions inherited from base class transaction. Lines 2324 retrieve the available balance of the account involved by invoking member function getAvailableBalance of bankDatabase. Note that line 24 uses inherited member function getAccountNumber to get the account number of the current user, which it then passes to getAvailableBalance. Lines 2728 retrieve the total balance of the current user's account. Lines 3136 display the balance information on the ATM's screen. Recall that displayDollarAmount takes a double argument and outputs it to the screen formatted as a dollar amount. For example, if a user's availableBalance is 700.5, line 33 outputs $700.50. Note that line 36 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).


[Page 1308]

Previous Page
Next Page