G.7. Class Account
Class Account (Figs. G.11G.12) represents a bank account. Lines 915 in the class definition (Fig. G.11) contain function prototypes for the class's constructor and six member functions, which we discuss shortly. Each Account has four attributes (modeled in Fig. 13.29)accountNumber, pin, availableBalance and totalBalance. Lines 1720 implement these attributes as private data members. Data member availableBalance represents the amount of funds available for withdrawal. Data member totalBalance represents the amount of funds available, plus the amount of deposited funds still pending confirmation or clearance.
Figure G.11. Account class definition.
1 // Account.h 2 // Account class definition. Represents a bank account. 3 #ifndef ACCOUNT_H 4 #define ACCOUNT_H 5 6 class Account 7 { 8 public: 9 Account( int, int, double, double ); // constructor sets attributes 10 bool validatePIN( int ) const; // is user-specified PIN correct? 11 double getAvailableBalance() const; // returns available balance 12 double getTotalBalance() const; // returns total balance 13 void credit( double ); // adds an amount to the Account balance 14 void debit( double ); // subtracts an amount from the Account balance 15 int getAccountNumber() const; // returns account number 16 private: 17 int accountNumber; // account number 18 int pin; // PIN for authentication 19 double availableBalance; // funds available for withdrawal 20 double totalBalance; // funds available + funds waiting to clear 21 }; // end class Account 22 23 #endif // ACCOUNT_H |
Figure G.12. Account class member-function definitions.
(This item is displayed on pages 1299 - 1300 in the print version)
1 // Account.cpp 2 // Member-function definitions for class Account. 3 #include "Account.h" // Account class definition 4 5 // Account constructor initializes attributes 6 Account::Account( int theAccountNumber, int thePIN, 7 double theAvailableBalance, double theTotalBalance ) 8 : accountNumber( theAccountNumber ), 9 pin( thePIN ), 10 availableBalance( theAvailableBalance ), 11 totalBalance( theTotalBalance ) 12 { 13 // empty body 14 } // end Account constructor 15 16 // determines whether a user-specified PIN matches PIN in Account 17 bool Account::validatePIN( int userPIN ) const 18 { 19 if ( userPIN == pin ) 20 return true; 21 else 22 return false; 23 } // end function validatePIN 24 25 // returns available balance 26 double Account::getAvailableBalance() const 27 { 28 return availableBalance; 29 } // end function getAvailableBalance 30 31 // returns the total balance 32 double Account::getTotalBalance() const 33 { 34 return totalBalance; 35 } // end function getTotalBalance 36 37 // credits an amount to the account 38 void Account::credit( double amount ) 39 { 40 totalBalance += amount; // add to total balance 41 } // end function credit 42 43 // debits an amount from the account 44 void Account::debit( double amount ) 45 { 46 availableBalance -= amount; // subtract from available balance 47 totalBalance -= amount; // subtract from total balance 48 } // end function debit 49 50 // returns account number 51 int Account::getAccountNumber() const 52 { 53 return accountNumber; 54 } // end function getAccountNumber |
Account Class Member-Function Definitions
Figure G.12 presents the definitions of class Account's member functions. The class's constructor (lines 614) takes an account number, the PIN established for the account, the initial available balance and the initial total balance as arguments. Lines 811 assign these values to the class's data members using member initializers.
Member function validatePIN (lines 1723) determines whether a user-specified PIN (i.e., parameter userPIN) matches the PIN associated with the account (i.e., data member pin). Recall that we modeled this member function's parameter userPIN in the UML class diagram of Fig. 6.37. If the two PINs match, the member function returns TRue (line 20); otherwise, it returns false (line 22).
Member functions getAvailableBalance (lines 2629) and getTotalBalance (lines 3235) are get functions that return the values of double data members availableBalance and totalBalance, respectively.
Member function credit (lines 3841) adds an amount of money (i.e., parameter amount) to an Account as part of a deposit transaction. Note that this member function adds the amount only to data member totalBalance (line 40). The money credited to an account during a deposit does not become available immediately, so we modify only the total balance. We assume that the bank updates the available balance appropriately at a later time. Our implementation of class Account includes only member functions required for carrying out ATM transactions. Therefore, we omit the member functions that some other bank system would invoke to add to data member availableBalance (to confirm a deposit) or subtract from data member totalBalance (to reject a deposit).
Member function debit (lines 4448) subtracts an amount of money (i.e., parameter amount) from an Account as part of a withdrawal transaction. This member function subtracts the amount from both data member availableBalance (line 46) and data member totalBalance (line 47), because a withdrawal affects both measures of an account balance.
Member function getAccountNumber (lines 5154) provides access to an Account's accountNumber. We include this member function in our implementation so that a client of the class (i.e., BankDatabase) can identify a particular Account. For example, BankDatabase contains many Account objects, and it can invoke this member function on each of its Account objects to locate the one with a specific account number.