J.7. Class Account Class Account (Fig. J.6) represents a bank account. Each Account has four attributes (modeled in Fig. 11.20)accountNumber, pin, availableBalance and totalBalance. Lines 47 implement these attributes as Private instance variables. Note that when we provide a property to access an instance variable, we create the instance variable name by appending Value to the end of the attribute name that was listed in the model. We provide a property with the same name as the attribute name (but starting with a capital letter) to access the instance variable. For example, property AccountNumber corresponds to the accountNumber attribute modeled in Fig. 11.20. Since clients of this class do not need to modify the accountNumberValue instance variable, AccountNumber is a ReadOnly property (i.e., it provides only a Get accessory). Figure J.6. Class Account represents a bank account. 1 ' Account.vb 2 ' Represents a bank account. 3 Public Class Account 4 Private accountNumberValue As Integer ' account number 5 Private pin As Integer ' PIN for authentication 6 Private availableBalanceValue As Decimal ' available withdrawal amount 7 Private totalBalanceValue As Decimal ' funds available+pending deposit 8 9 ' constructor initializes attributes 10 Public Sub New (ByVal theAccountNumber As Integer, _ 11 ByVal thePIN As Integer, ByVal theAvailableBalance As Decimal, _ 12 ByVal theTotalBalance As Decimal) 13 accountNumberValue = theAccountNumber 14 pin = thePIN 15 availableBalanceValue = theAvailableBalance 16 totalBalanceValue = theTotalBalance 17 End Sub ' New 18 19 ' property AccountNumber 20 Public ReadOnly Property AccountNumber() As Integer 21 Get 22 Return accountNumberValue 23 End Get 24 End Property ' AccountNumber 25 26 ' property AvailableBalance 27 Public ReadOnly Property AvailableBalance() As Decimal 28 Get 29 Return availableBalanceValue 30 End Get 31 End Property ' AvailableBalance 32 33 ' property TotalBalance 34 Public ReadOnly Property TotalBalance() As Decimal 35 Get 36 Return totalBalanceValue 37 End Get 38 End Property ' TotalBalance 39 40 ' determines whether a user-specified PIN matches PIN in Account 41 Public Function ValidatePIN(ByVal userPIN As Integer) As Boolean 42 If userPIN = pin Then 43 Return True 44 Else 45 Return False 46 End If 47 End Function ' ValidatePIN 48 49 ' credits the account (funds have not yet cleared) 50 Public Sub Credit(ByVal amount As Decimal) 51 totalBalanceValue += amount ' add to total balance 52 End Sub ' Credit 53 54 ' debits the account 55 Public Sub Debit(ByVal amount As Decimal) 56 availableBalanceValue -= amount ' subtract from available balance 57 totalBalanceValue -= amount ' subtract from total balance 58 End Sub ' Debit 59 End Class ' Account | Class Account has a constructor (lines 1017) that takes an account number, the PIN established for the account, the initial available balance and the initial total balance as arguments. Lines 1316 assign these values to the class's attributes (i.e., instance variables). Note that Account objects would normally be created externally to the ATM system. However, in this simulation, the Account objects are created in the BankDatabase class (Fig. J.7). Figure J.7. Class BankDatabase represents the bank's account information database. 1 ' BankDatabase.vb 2 ' Represents the bank account information database 3 Public Class BankDatabase 4 Private accounts As Account() ' array of the bank's Accounts 5 6 ' parameterless BankDatabase constructor initializes accounts 7 Public Sub New() 8 ' create two Account objects for testing and 9 ' place them in the accounts array 10 accounts = New Account(0 To 1) {} ' create accounts array 11 accounts(0) = New Account(12345, 54321, 1000, 1200) 12 accounts(1) = New Account(98765, 56789, 200, 200) 13 End Sub ' New 14 15 ' retrieve Account object containing specified account number 16 Private Function GetAccount(ByVal accountNumber As Integer) As Account 17 ' loop through accounts searching for matching account number 18 For Each currentAccount As Account In accounts 19 If currentAccount.AccountNumber = accountNumber Then 20 Return currentAccount 21 End If 22 Next 23 24 Return Nothing 25 End Function ' GetAccount 26 27 ' determine whether user-specified account number and PIN match 28 ' those of an account in the database 29 Public Function AuthenticateUser(ByVal userAccountNumber As Integer, _ 30 ByVal userPIN As Integer) As Boolean 31 ' attempt to retrieve the account with the account number 32 Dim userAccount As Account = GetAccount(userAccountNumber) 33 34 ' if account exists, return result of Account function ValidatePIN 35 If (userAccount IsNot Nothing) Then 36 Return userAccount.ValidatePIN(userPIN) 37 Else 38 Return False ' account number not found, so return false 39 End If 40 End Function ' AuthenticateUser 41 42 ' return available balance of Account with specified account number 43 Public Function GetAvailableBalance( _ 44 ByVal userAccountNumber As Integer) As Decimal 45 Dim userAccount As Account = GetAccount(userAccountNumber) 46 Return userAccount.AvailableBalance 47 End Function ' GetAvailableBalance 48 49 ' return total balance of Account with specified account number 50 Public Function GetTotalBalance( _ 51 ByVal userAccountNumber As Integer) As Decimal 52 Dim userAccount As Account = GetAccount(userAccountNumber) 53 Return userAccount.TotalBalance 54 End Function ' GetTotalBalance 55 56 ' credit the Account with specified account number 57 Public Sub Credit(ByVal userAccountNumber As Integer, _ 58 ByVal amount As Decimal) 59 Dim userAccount As Account = GetAccount(userAccountNumber) 60 userAccount.Credit(amount) 61 End Sub ' Credit 62 63 ' debit the Account with specified account number 64 Public Sub Debit(ByVal userAccountNumber As Integer, _ 65 ByVal amount As Decimal) 66 Dim userAccount As Account = GetAccount(userAccountNumber) 67 userAccount.Debit(amount) 68 End Sub ' Debit 69 End Class ' BankDatabase | Public ReadOnly Properties of Class Account ReadOnly property AccountNumber (lines 2024) provides access to an Account's accountNumberValue. We include this property in our implementation so that a client of the class (e.g., BankDatabase) can identify a particular Account. For example, BankDatabase contains many Account objects, and it can access this property on each of its Account objects to locate the one with a specific account number. ReadOnly properties AvailableBalance (lines 2731) and TotalBalance (lines 3438) allow clients to retrieve the values of Private Decimal instance variables availableBalanceValue and totalBalanceValue, respectively. Property AvailableBalance represents the amount of funds available for withdrawal. Property TotalBalance represents the amount of funds available, plus the amount of deposited funds still pending confirmation (of cash in deposit envelopes) or clearance (of checks in deposit envelopes). Public Methods of Class Account Method ValidatePIN (lines 4147) determines whether a user-specified PIN (i.e., parameter userPIN) matches the PIN associated with the account (i.e., attribute pin). Recall that we modeled this method's parameter userPIN in the UML class diagram of Fig. 7.26. If the two PINs match, the method returns TRue (line 43); otherwise, it returns False (line 45). Method Credit (lines 5052) adds an amount of money (i.e., parameter amount) to an Account as part of a deposit transaction. Note that this method adds the amount only to instance variable totalBalanceValue (line 45). 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 when the amount of cash in the deposit envelope has been verified and when the checks in the deposit envelope have cleared. Our implementation of class Account includes only methods required for carrying out ATM transactions. Therefore, we omit the methods that some other bank system would invoke to add to instance variable availableBalanceValue (to confirm a deposit) or subtract from attribute totalBalanceValue (to reject a deposit). Method Debit (lines 5558) subtracts an amount of money (i.e., parameter amount) from an Account as part of a withdrawal transaction. This method subtracts the amount from both instance variable availableBalanceValue (line 56) and instance variable totalBalanceValue (line 57), because a withdrawal affects both measures of an account balance. |