Section J.2. Class ATM


J.2. Class ATM

Class ATM (Fig. J.1) represents the ATM as a whole. Lines 410 implement the class's attributes. We determine all but one of these attributes from the UML class diagrams of Fig. 11.19 and Fig. 11.20. Line 4 declares the Boolean attribute userAuthenticated from Fig. 11.21. Line 5 declares an attribute not found in our UML designan Integer attribute currentAccountNumber that keeps track of the account number of the current authenticated user. We will soon see how the class uses this attribute. Lines 610 declare reference-type attributes corresponding to the ATM class's associations modeled in the class diagram of Fig. 11.19. These attributes allow the ATM to access its parts (i.e., its Screen, Keypad, CashDispenser and DepositSlot) and interact with the bank's account information database (i.e., a BankDatabase object).

Figure J.1. Class ATM represents the ATM.

  1  ' ATM.vb  2  ' Represents an automated teller machine.  3  Public Class ATM  4     Private userAuthenticated As Boolean ' whether user is authenticated  5     Private currentAccountNumber As Integer ' user's account number  6     Private screenHandle As Screen ' ATM's screen  7     Private keypadHandle As Keypad ' ATM's keypad  8     Private cashDispenserHandle As CashDispenser ' ATM's cash dispenser  9     Private depositSlotHandle As DepositSlot ' ATM's deposit slot 10     Private bankDatabaseHandle As BankDatabase ' account database 11 12     ' enumeration constants represent main menu options 13     Private Enum MenuOption 14        BALANCE_INQUIRY = 1 15        WITHDRAWAL 16        DEPOSIT 17        EXIT_ATM 18     End Enum ' MenuOption 19 20     ' parameterless constructor initializes instance variables 21     Public Sub New() 22        userAuthenticated = False ' user is not authenticated to start 23        currentAccountNumber = 0 ' no current account number to start 24        screenHandle = New Screen() ' create screen 25        keypadHandle = New Keypad() ' create keypad 26        cashDispenserHandle = New CashDispenser() ' create cash dispenser 27        depositSlotHandle = New DepositSlot() ' create deposit slot 28        bankDatabaseHandle = New BankDatabase() ' create database 29     End Sub ' New 30 31     ' start ATM 32     Public Sub Run() 33        ' welcome and authenticate users; perform transactions 34        While (True) ' infinite loop 35           ' loop while user is not yet authenticated 36           While (Not userAuthenticated) 37              screenHandle.DisplayMessageLine(vbCrLf & "Welcome!") 38              AuthenticateUser() ' authenticate user 39           End While 40 41           PerformTransactions() ' for authenticated user 42           userAuthenticated = False ' reset before next ATM session 43           currentAccountNumber = 0 ' reset before next ATM session 44           screenHandle.DisplayMessageLine(vbCrLf & "Thank you! Goodbye!") 45        End While 46     End Sub ' Run 47 48     ' attempt to authenticate user against database 49     Private Sub AuthenticateUser() 50        screenHandle.DisplayMessage(vbCrLf & _ 51           "Please enter your account number: ") 52        Dim accountNumber As Integer = keypadHandle.GetInput() 53        screenHandle.DisplayMessage(vbCrLf & "Enter your PIN: ") ' prompt 54        Dim pin As Integer = keypadHandle.GetInput() ' get PIN 55 56        ' set userAuthenticated to Boolean value returned by database 57        userAuthenticated = _ 58           bankDatabaseHandle.AuthenticateUser(accountNumber, pin) 59 60        ' check whether authentication succeeded 61        If userAuthenticated Then 62           currentAccountNumber = accountNumber ' save user's account # 63        Else 64           screenHandle.DisplayMessageLine( _ 65              "Invalid account number or PIN. Please try again.") 66        End If 67     End Sub ' AuthenticateUser 68 69     ' display the main menu and perform transactions 70     Private Sub PerformTransactions() 71        Dim currentTransaction As Transaction ' transaction being processed 72        Dim userExited As Boolean = False ' user has not chosen to exit 73 74        ' loop while user has not chosen exit option 75        While (Not userExited) 76           ' show main menu and get user selection 77           Dim mainMenuSelection As Integer = DisplayMainMenu() 78 79           ' decide how to proceed based on user's menu selection 80           Select Case (mainMenuSelection) 81              ' user chooses to perform one of three transaction types 82              Case MenuOption.BALANCE_INQUIRY, MenuOption.WITHDRAWAL, _ 83                 MenuOption.DEPOSIT 84                 ' initialize as new object of chosen type 85                 currentTransaction = CreateTransaction(mainMenuSelection) 86                 currentTransaction.Execute() ' execute transaction 87              Case MenuOption.EXIT_ATM ' user chose to terminate session 88                 screenHandle.DisplayMessageLine( _ 89                    vbCrLf & "Exiting the system...") 90                 userExited = True ' this ATM session should end 91              Case Else ' user did not enter an integer from 1-4 92                 screenHandle.DisplayMessageLine(vbCrLf & _ 93                    "You did not enter a valid selection. Try again.") 94           End Select 95        End While 96     End Sub ' PerformTransactions 97 98     ' display the main menu and return an input selection 99     Private Function DisplayMainMenu() As Integer 100       screenHandle.DisplayMessageLine(vbCrLf & "Main menu:") 101       screenHandle.DisplayMessageLine("1 - View my balance") 102       screenHandle.DisplayMessageLine("2 - Withdraw cash") 103       screenHandle.DisplayMessageLine("3 - Deposit funds") 104       screenHandle.DisplayMessageLine("4 - Exit" & vbCrLf) 105       screenHandle.DisplayMessage("Enter a choice: ") 106       Return keypadHandle.GetInput() ' return user's selection 107    End Function ' DisplayMainMenu 108 109    ' return object of specified Transaction derived class 110    Private Function CreateTransaction(ByVal type As Integer) _ 111       As Transaction 112       Dim temp As Transaction = Nothing ' temporary Transaction object 113 114       ' determine which type of Transaction to create 115       Select Case (type) 116          ' create new BalanceInquiry transaction 117          Case MenuOption.BALANCE_INQUIRY 118             temp = New BalanceInquiry( _ 119                currentAccountNumber, screenHandle, bankDatabaseHandle) 120          Case MenuOption.WITHDRAWAL ' create new Withdrawal transaction 121             temp = New Withdrawal(currentAccountNumber, screenHandle, _ 122                bankDatabaseHandle, keypadHandle, cashDispenserHandle) 123          Case MenuOption.DEPOSIT ' create new Deposit transaction 124             temp = New Deposit(currentAccountNumber, screenHandle, _ 125                bankDatabaseHandle, keypadHandle, depositSlotHandle) 126       End Select 127 128       Return temp 129    End Function ' CreateTransaction 130 End Class ' ATM 

Lines 1318 declare an enumeration that corresponds to the four options in the ATM's main menu (i.e., balance inquiry, withdrawal, deposit and exit). Lines 2129 declare class ATM's constructor, which initializes the class's attributes. When an ATM object is first created, no user is authenticated, so line 22 initializes userAuthenticated to False. Line 23 initializes currentAccountNumber to 0 because there is no current user yet. Lines 2427 instantiate new objects to represent the parts of the ATM. Recall that class ATM has composition relationships with classes Screen, Keypad, CashDispenser and DepositSlot, so class ATM is responsible for their creation. Line 28 creates a new BankDatabase. As you will soon see, the BankDatabase creates two Account objects that can be used to test the ATM. [Note: If this were a real ATM system, the ATM class would receive a reference to an existing database object created by the bank. However, in this implementation we are only simulating the bank's database, so class ATM creates the BankDatabase object with which it interacts.]

Implementing the Operation

The class diagram of Fig. 11.20 does not list any operations for class ATM. We now implement one operation (i.e., Public method) in class ATM that allows an external client of the class (i.e., module ATMCaseStudy; Section J.13) to tell the ATM to run. ATM method Run (lines 3246) uses an infinite loop (lines 3445) to repeatedly welcome a user, attempt to authenticate the user and, if authentication succeeds, allow the user to perform transactions. After an authenticated user performs the desired transactions and chooses to exit, the ATM resets itself, displays a goodbye message to the user and restarts the process. We use an infinite loop here to simulate the fact that an ATM appears to run continuously until the bank turns it off (an action beyond the user's control). An ATM user has the option to exit the system, but does not have the ability to turn off the ATM completely.

Inside method Run's infinite loop, lines 3639 cause the ATM to repeatedly welcome and attempt to authenticate the user as long as the user has not been authenticated (i.e., the condition Not userAuthenticated is TRue). Line 37 invokes method DisplayMessageLine of the ATM's screen to display a welcome message. Like Screen method DisplayMessage designed in the case study, method DisplayMessageLine (declared in lines 1012 of Fig. J.2) displays a message to the user, but this method also outputs a newline after displaying the message. We add this method during implementation to give class Screen's clients more control over the placement of displayed messages. Line 38 invokes class ATM's Private utility method AuthenticateUser (declared in lines 4967) to attempt to authenticate the user.

Figure J.2. Class Screen represents the screen of the ATM.

  1  ' Screen.vb  2  ' Represents the screen of the ATM  3  Public Class Screen  4     ' displays a message without a terminating carriage return  5     Public Sub DisplayMessage(ByVal message As String)  6        Console.Write(message)  7     End Sub ' DisplayMessage  8  9     ' display a message with a terminating carriage return 10     Public Sub DisplayMessageLine(ByVal message As String) 11        Console.WriteLine(message) 12     End Sub ' DisplayMessageLine 13 14     ' display a dollar amount 15     Public Sub DisplayDollarAmount(ByVal amount As Decimal) 16        Console.Write("{0:C}", amount) 17     End Sub ' DisplayDollarAmount 18  End Class ' Screen 

Authenticating the User

We refer to the requirements document to determine the steps necessary to authenticate the user before allowing transactions to occur. Lines 5051 of method AuthenticateUser invoke method DisplayMessage of the ATM's screen to prompt the user to enter an account number. Line 52 invokes method GetInput of the ATM's keypad to obtain the user's input, then stores the integer value entered by the user in local variable accountNumber. Method AuthenticateUser next prompts the user to enter a PIN (line 53), and stores the PIN input by the user in local variable pin (line 54). Next, lines 5758 attempt to authenticate the user by passing the accountNumber and pin entered by the user to the bank database's AuthenticateUser method. Class ATM sets its userAuthenticated attribute to the Boolean value returned by this methoduserAuthenticated becomes true if authentication succeeds (i.e., accountNumber and pin match those of an existing Account in the bank database) and remains False otherwise. If userAuthenticated is true, line 62 saves the account number entered by the user (i.e., accountNumber) in the ATM attribute currentAccountNumber. The other methods of class ATM use this variable whenever an ATM session requires access to the user's account number. If userAuthenticated is False, lines 6465 use the screenHandle's DisplayMessageLine method to indicate that an invalid account number and/or PIN was entered, so the user must try again. Note that we set currentAccountNumber only after authenticating the user's account number and the associated PINif the database could not authenticate the user, currentAccountNumber remains 0.

After method Run attempts to authenticate the user (line 38), if userAuthenticated is still False (line 36), the While loop body (lines 3738) executes again. If userAuthenticated is now TRue, the loop terminates and control continues with line 41, which calls class ATM's Private utility method PerformTransactions.

Performing Transactions

Method PerformTransactions (lines 7096) carries out an ATM session for an authenticated user. Line 71 declares local variable TRansaction to which we assign a BalanceInquiry, Withdrawal or Deposit object representing the ATM transaction currently being processed. Note that we use a transaction variable here to allow us to take advantage of polymorphism. Also note that we name this variable after the role name included in the class diagram of Fig. 4.19currentTransaction. Line 72 declares another local variablea Boolean called userExited that keeps track of whether the user has chosen to exit. This variable controls a While loop (lines 7595) that allows the user to execute an unlimited number of transactions before choosing to exit. Within this loop, line 77 displays the main menu and obtains the user's menu selection by calling ATM utility method DisplayMainMenu (declared in lines 99107). This method displays the main menu by invoking methods of the ATM's screen and returns a menu selection obtained from the user through the ATM's keypad. Line 77 stores the user's selection returned by DisplayMainMenu in local variable mainMenuSelection.

After obtaining a main menu selection, method PerformTransactions uses a Select Case statement (lines 8094) to respond to the selection appropriately. If mainMenuSelection is equal to any of the three integer constants representing transaction types (i.e., if the user chose to perform a transaction), line 85 calls utility method CreateTransaction (declared in lines 110129) to return a newly instantiated object of the type that corresponds to the selected transaction. Variable currentTransaction is assigned the reference returned by method CreateTransaction, then line 86 invokes method Execute of this transaction to execute it. We will discuss transaction method Execute and the three transaction derived classes shortly. Note that we assign to the TRansaction variable currentTransaction an object of one of the three transaction derived classes so that we can execute transactions polymorphically. For example, if the user chooses to perform a balance inquiry, mainMenuSelection equals MenuOption.BALANCE_INQUIRY, and CreateTransaction returns a BalanceInquiry object (line 85). Thus, currentTransaction refers to a BalanceInquiry and invoking currentTransaction.Execute() (line 86) results in BalanceInquiry's version of Execute being called.

Creating Transactions

Method CreateTransaction (lines 110129) uses a Select Case statement (lines 115126) to instantiate a new transaction derived class object of the type indicated by the parameter type. Recall that method PerformTransactions passes mainMenuSelection to method CreateTransaction only when mainMenuSelection contains a value corresponding to one of the three transaction types. So parameter type (line 110) receives one of the values MenuOption.BALANCE_INQUIRY, MenuOption.WITHDRAWAL or MenuOption.DEPOSIT. Each Case in the Select Case statement instantiates a new object by calling the appropriate transaction derived class constructor. Note that each constructor has a unique parameter list, based on the specific data required to initialize the derived class object. A BalanceInquiry (lines 118119) requires only the account number of the current user and the ATM's screenHandle and bankDatabaseHandle. In addition to these parameters, a Withdrawal (lines 121122) requires the ATM's keypadHandle and cashDispenserHandle, and a Deposit (lines 124125) requires the ATM's keypadHandle and depositSlotHandle. We discuss the transaction classes in more detail in Sections J.9J.12.

After executing a transaction (line 86 in method PerformTransactions), userExited remains False and the While loop in lines 7595 repeats, returning the user to the main menu. However, if a user does not perform a transaction and instead selects the main menu option to exit, line 90 sets userExited to true causing the condition in line 75 of the While loop (Not userExited) to become False. This While is the final statement of method PerformTransactions, so control returns to line 42 of the calling method Run. If the user enters an invalid main menu selection (i.e., not an integer in the range 14), lines 9293 display an appropriate error message, userExited (as set in line 72) remains False and the user returns to the main menu to try again.

When method PerformTransactions returns control to method Run, the user has chosen to exit the system, so lines 4243 reset the ATM's attributes userAuthenticated and currentAccountNumber to False and 0, respectively, to prepare for the next ATM user. Line 44 displays a goodbye message to the current user before the ATM welcomes the next user.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net