Class BalanceInquiry
(Figs. 23.30–23.31) derives from abstract base class Transaction
and represents a balanceinquiry 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. Line 6 #include
s the definition of base class Transaction
. The BalanceInquiry
constructor (declared in line 11 of Fig. 23.30 and defined in lines 8–13 of Fig. 23.31) 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. 23.31). Line 12 of Fig. 23.30 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.
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
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( "
Balance Information:" );
32 screen.displayMessage( " - Available balance: " );
33 screen.displayDollarAmount( availableBalance );
34 screen.displayMessage( "
- 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 16–37 of Fig. 23.31) that performs the steps involved in a balance inquiry. Lines 19–20 get references to the bank database and the ATM’s screen by invoking member functions inherited from base class Transaction
. Lines 23–24 retrieve the available balance of the account involved by invoking member function getAvailableBalance
of bankDatabase
. Line 24 uses inherited member function getAccountNumber
to get the account number of the current user, which it then passes to getAvailableBalance
. Lines 27–28 retrieve the total balance of the current user’s account. Lines 31–36 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
. 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
).