Class Withdrawal (Fig. J.10) extends TRansaction and represents an ATM withdrawal transaction. This class expands on the "skeleton" code for this class developed in Fig. 11.24. Recall from the class diagram of Fig. 11.22 that class Withdrawal has one attribute, amount, which line 5 declares as a decimal instance variable. Fig. 11.21 models associations between class Withdrawal and classes Keypad and CashDispenser, for which lines 67 implement reference attributes keypad and cashDispenser, respectively. Line 10 declares a constant corresponding to the cancel menu option.
Figure J.10. Class Withdrawal represents an ATM withdrawal transaction.
1 // Withdrawal.cs 2 // Class Withdrawal represents an ATM withdrawal transaction. 3 public class Withdrawal : Transaction 4 { 5 private decimal amount; // amount to withdraw 6 private Keypad keypad; // reference to Keypad 7 private CashDispenser cashDispenser; // reference to cash dispenser 8 9 // constant that corresponds to menu option to cancel 10 private const int CANCELED = 6;11 12 // five-parameter constructor 13 public Withdrawal( int userAccountNumber, Screen atmScreen, 14 BankDatabase atmBankDatabase, Keypad atmKeypad, 15 CashDispenser atmCashDispenser ) 16 : base( userAccountNumber, atmScreen, atmBankDatabase ) 17 { 18 // initialize references to keypad and cash dispenser 19 keypad = atmKeypad; 20 cashDispenser = atmCashDispenser; 21 } // end constructor 22 23 // perform transaction, overrides Transaction's abstract method 24 public override void Execute() 25 { 26 bool cashDispensed = false; // cash was not dispensed yet 27 28 // transaction was not canceled yet 29 bool transactionCanceled = false; 30 31 // loop until cash is dispensed or the user cancels 32 do 33 { 34 // obtain the chosen withdrawal amount from the user 35 int selection = DisplayMenuOfAmounts(); 36 37 // check whether user chose a withdrawal amount or canceled 38 if ( selection != CANCELED ) 39 { 40 // set amount to the selected dollar amount 41 amount = selection; 42 43 // get available balance of account involved 44 decimal availableBalance = 45 Database.GetAvailableBalance( AccountNumber ); 46 47 // check whether the user has enough money in the account 48 if ( amount <= availableBalance ) 49 { 50 // check whether the cash dispenser has enough money 51 if ( cashDispenser.IsSufficientCashAvailable( amount ) ) 52 { 53 // debit the account to reflect the withdrawal 54 Database.Debit( AccountNumber, amount ); 55 56 cashDispenser.DispenseCash( amount ); // dispense cash 57 cashDispensed = true; // cash was dispensed 58 59 // instruct user to take cash 60 UserScreen.DisplayMessageLine( 61 " Please take your cash from the cash dispenser." ); 62 } // end innermost if 63 else // cash dispenser does not have enough cash 64 UserScreen.DisplayMessageLine( 65 " Insufficient cash available in the ATM." + 66 " Please choose a smaller amount." ); 67 } // end middle if 68 else // not enough money available in user's account 69 UserScreen.DisplayMessageLine( 70 " Insufficient cash available in your account." + 71 " Please choose a smaller amount." ); 72 } // end outermost if 73 else 74 { 75 UserScreen.DisplayMessageLine( " Canceling transaction..." ); 76 transactionCanceled = true; // user canceled the transaction 77 } // end else 78 } while ( ( !cashDispensed ) && ( !transactionCanceled ) ); 79 } // end method Execute 80 81 // display a menu of withdrawal amounts and the option to cancel; 82 // return the chosen amount or 6 if the user chooses to cancel 83 private int DisplayMenuOfAmounts() 84 { 85 int userChoice = 0; // variable to store return value 86 87 // array of amounts to correspond to menu numbers 88 int[] amounts = { 0, 20, 40, 60, 100, 200 }; 89 90 // loop while no valid choice has been made 91 while ( userChoice == 0 ) 92 { 93 // display the menu 94 UserScreen.DisplayMessageLine( " Withdrawal options:" ); 95 UserScreen.DisplayMessageLine( "1 - $20" ); 96 UserScreen.DisplayMessageLine( "2 - $40" ); 97 UserScreen.DisplayMessageLine( "3 - $60" ); 98 UserScreen.DisplayMessageLine( "4 - $100" ); 99 UserScreen.DisplayMessageLine( "5 - $200" ); 100 UserScreen.DisplayMessageLine( "6 - Cancel transaction" ); 101 UserScreen.DisplayMessage( 102 " Choose a withdrawal option (1-6): " ); 103 104 // get user input through keypad 105 int input = keypad.GetInput(); 106 107 // determine how to proceed based on the input value 108 switch ( input ) 109 { 110 // if the user chose a withdrawal amount (i.e., option 111 // 1, 2, 3, 4, or 5), return the corresponding amount 112 // from the amounts array 113 case 1: case 2: case 3: case 4: case 5: 114 userChoice = amounts[ input ]; // save user's choice 115 break; 116 case CANCELED: // the user chose to cancel 117 userChoice = CANCELED; // save user's choice 118 break; 119 default: 120 UserScreen.DisplayMessageLine( 121 " Invalid selection. Try again." ); 122 break; 123 } // end switch 124 } // end while 125 126 return userChoice; 127 } // end method DisplayMenuOfAmounts 128 } // end class Withdrawal |
Class Withdrawal's constructor (lines 1321) has five parameters. It uses the constructor initializer to pass parameters userAccountNumber, atmScreen and atmBankDatabase to base class transaction's constructor to set the attributes that Withdrawal inherits from transaction. The constructor also takes references atmKeypad and atmCashDispenser as parameters and assigns them to reference-type attributes keypad and cashDispenser, respectively.
Overriding abstract Method Execute
Class Withdrawal overrides transaction's abstract method Execute with a concrete implementation (lines 2479) that performs the steps involved in a withdrawal. Line 26 declares and initializes a local bool variable cashDispensed. This variable indicates whether cash has been dispensed (i.e., whether the transaction has completed successfully) and is initially false. Line 29 declares and initializes to false a bool variable TRansactionCanceled to indicate that the transaction has not yet been canceled by the user.
Lines 3278 contain a do...while statement that executes its body until cash is dispensed (i.e., until cashDispensed becomes TRue) or until the user chooses to cancel (i.e., until TRansactionCanceled becomes true). We use this loop to continuously return the user to the start of the transaction if an error occurs (i.e., the requested withdrawal amount is greater than the user's available balance or greater than the amount of cash in the cash dispenser). Line 35 displays a menu of withdrawal amounts and obtains a user selection by calling private utility method DisplayMenuOfAmounts (declared in lines 83127). This method displays the menu of amounts and returns either an int withdrawal amount or an int constant CANCELED to indicate that the user has chosen to cancel the transaction.
Displaying Options With private Utility Method DisplayMenuOfAmounts
Method DisplayMenuOfAmounts (lines 83127) first declares local variable userChoice (initially 0) to store the value that the method will return (line 85). Line 88 declares an integer array of withdrawal amounts that correspond to the amounts displayed in the withdrawal menu. We ignore the first element in the array (index 0), because the menu has no option 0. The while statement at lines 91124 repeats until userChoice takes on a value other than 0. We will see shortly that this occurs when the user makes a valid selection from the menu. Lines 94102 display the withdrawal menu on the screen and prompt the user to enter a choice. Line 105 obtains integer input through the keypad. The switch statement at lines 108123 determines how to proceed based on the user's input. If the user selects 1, 2, 3, 4 or 5, line 114 sets userChoice to the value of the element in the amounts array at index input. For example, if the user enters 3 to withdraw $60, line 114 sets userChoice to the value of amounts[ 3 ]i.e., 60. Variable userChoice no longer equals 0, so the while at lines 91124 terminates, and line 126 returns userChoice. If the user selects the cancel menu option, line 117 executes, setting userChoice to CANCELED and causing the method to return this value. If the user does not enter a valid menu selection, lines 120121 display an error message, and the user is returned to the withdrawal menu.
The if statement at line 38 in method Execute determines whether the user has selected a withdrawal amount or chosen to cancel. If the user cancels, line 75 displays an appropriate message to the user before control is returned to the calling methodATM method PerformTransactions. If the user has chosen a withdrawal amount, line 41 assigns local variable selection to instance variable amount. Lines 4445 retrieve the available balance of the current user's Account and store it in a local decimal variable availableBalance. Next, the if statement at line 48 determines whether the selected amount is less than or equal to the user's available balance. If it is not, lines 6971 display an error message. Control then continues to the end of the do...while statement, and the loop repeats because both cashDispensed and transactionCanceled are still false. If the user's balance is high enough, the if statement at line 51 determines whether the cash dispenser has enough money to satisfy the withdrawal request by invoking the cashDispenser's IsSufficientCashAvailable method. If this method returns false, lines 6466 display an error message, and the do...while statement repeats. If sufficient cash is available, the requirements for the withdrawal are satisfied, and line 54 debits the user's account in the database by amount. Lines 5657 then instruct the cash dispenser to dispense the cash to the user and set cashDispensed to true. Finally, lines 6061 display a message to the user to take the dispensed cash. Because cashDispensed is now true, control continues after the do...while statement. No additional statements appear below the loop, so the method returns control to class ATM.
J 12 Class Deposit |
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index