Section J.4. Class Keypad


J.4. Class Keypad

Class Keypad (Fig. J.3) represents the keypad of the ATM and is responsible for receiving all user input. Recall that we are simulating this hardware, so we use the computer's keyboard to approximate the keypad. We use method Console.ReadLine to obtain keyboard input from the user. A computer keyboard contains many keys not found on the ATM's keypad. We assume that the user presses only the keys on the computer keyboard that also appear on the keypadthe keys numbered 09 and the Enter key.

Figure J.3. Class Keypad represents the ATM's keypad

 1  ' Keypad.vb 2  ' Represents the keypad of the ATM. 3  Public Class Keypad 4     ' return an integer value entered by user 5     Public Function GetInput() As Integer 6        Return Convert.ToInt32(Console.ReadLine()) 7     End Function ' GetInput 8  End Class ' Keypad 

Method GetInput (lines 57) invokes Convert method ToInt32 to convert the input returned by Console.ReadLine (line 6) to an Integer value. [Note: Method ToInt32 can throw a FormatException if the user enters non-integer input. Because the real ATM's keypad permits only integer input, we simply assume that no exceptions will occur. See Chapter 12, Exception Handling, for information on catching and processing exceptions.] Recall that ReadLine obtains all the input used by the ATM. Class Keypad's GetInput method simply returns the integer input by the user. If a client of class Keypad requires input that satisfies some particular criteria (i.e., a number corresponding to a valid menu option), the client must perform the appropriate error checking.



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