J.8. Class BankDatabase
Class BankDatabase (Fig. J.7) models the bank database with which the ATM interacts to access and modify a user's account information. We determine one reference-type attribute for class BankDatabase based on its composition relationship with class Account. Recall from Fig. 11.21 that a BankDatabase is composed of zero or more objects of class Account. Line 5 declares attribute accountsan array that will store Account objectsto implement this composition relationship. Class BankDatabase has a parameterless constructor (lines 815) that initializes accounts with new Account objects (lines 1314). Note that the Account constructor (Fig. J.6, lines 1118) has four parametersthe account number, the PIN assigned to the account, the initial available balance and the initial total balance.
Recall that class BankDatabase serves as an intermediary between class ATM and the actual Account objects that contain users' account information. Thus, methods of class BankDatabase invoke the corresponding methods and properties of the Account object belonging to the current ATM user.
private Utility Method GetAccount
We include private utility method GetAccount (lines 1829) to allow the BankDatabase to obtain a reference to a particular Account within the accounts array. To locate the user's Account, the BankDatabase compares the value returned by property AccountNumber for each element of accounts to a specified account number until it finds a match. Lines 2125 traverse the accounts array. If currentAccount's account number equals the value of parameter accountNumber, the method returns currentAccount. If no account has the given account number, then line 28 returns null.
public Methods
Method AuthenticateUser (lines 3343) proves or disproves the identity of an ATM user. This method takes a user-specified account number and a user-specified PIN as arguments and indicates whether they match the account number and PIN of an Account in the database. Line 36 calls method GetAccount, which returns either an Account with userAccountNumber as its account number or null to indicate that userAccountNumber is invalid. If GetAccount returns an Account object, line 40 returns the bool value returned by that object's ValidatePIN method. Note that BankDatabase's AuthenticateUser method does not perform the PIN comparison itselfrather, it forwards userPIN to the Account object's ValidatePIN method to do so. The value returned by Account method ValidatePIN (line 40) indicates whether the user-specified PIN matches the PIN of the user's Account, so method AuthenticateUser simply returns this value (line 40) to the client of the class (i.e., ATM).
The BankDatabase trusts the ATM to invoke method AuthenticateUser and receive a return value of TRue before allowing the user to perform transactions. BankDatabase also trusts that each TRansaction object created by the ATM contains the valid account number of the current authenticated user and that this account number is passed to the remaining BankDatabase methods as argument userAccountNumber. Methods GetAvailableBalance (lines 4650), GetTotalBalance (lines 5357), Credit (lines 6064) and Debit (lines 6771) therefore simply retrieve the user's Account object with utility method GetAccount, then invoke the appropriate Account method on that object. We know that the calls to GetAccount within these methods will never return null, because userAccountNumber must refer to an existing Account. Note that GetAvailableBalance and GetTotalBalance return the values returned by the corresponding Account properties. Also, note that methods Credit and Debit simply redirect parameter amount to the Account methods they invoke.