J.12. Class Deposit

Class Deposit (Fig. J.11) inherits from TRansaction and represents an ATM deposit transaction. Recall from the class diagram of Fig. 11.22 that class Deposit has one attribute, amount, which line 5 declares as a decimal instance variable. Lines 67 create reference attributes keypad and depositSlot that implement the associations between class Deposit and classes Keypad and DepositSlot, modeled in Fig. 11.21. Line 10 declares a constant CANCELED that corresponds to the value a user enters to cancel a deposit transaction.

Figure J.11. Class Deposit represents an ATM deposit transaction.

 1 // Deposit.cs
 2 // Represents a deposit ATM transaction.
 3 public class Deposit : Transaction
 4 {
 5 private decimal amount; // amount to deposit
 6 private Keypad keypad; // reference to the Keypad
 7 private DepositSlot depositSlot; // reference to the deposit slot
9 // constant representing cancel option 10 private const int CANCELED = 0; 11 12 // five-parameter constructor initializes class's instance variables 13 public Deposit( int userAccountNumber, Screen atmScreen, 14 BankDatabase atmBankDatabase, Keypad atmKeypad, 15 DepositSlot atmDepositSlot ) 16 : base( userAccountNumber, atmScreen, atmBankDatabase ) 17 { 18 // initialize references to keypad and deposit slot 19 keypad = atmKeypad; 20 depositSlot = atmDepositSlot; 21 } // end five-parameter constructor 22 23 // perform transaction; overrides Transaction's abstract method 24 public override void Execute() 25 { 26 amount = PromptForDepositAmount(); // get deposit amount from user 27 28 // check whether user entered a deposit amount or canceled 29 if ( amount != CANCELED ) 30 { 31 // request deposit envelope containing specified amount 32 UserScreen.DisplayMessage( 33 " Please insert a deposit envelope containing " ); 34 UserScreen.DisplayDollarAmount( amount ); 35 UserScreen.DisplayMessageLine( " in the deposit slot." ); 36 37 // retrieve deposit envelope 38 bool envelopeReceived = depositSlot.IsDepositEnvelopeReceived(); 39 40 // check whether deposit envelope was received 41 if ( envelopeReceived ) 42 { 43 UserScreen.DisplayMessageLine( 44 " Your envelope has been received. " + 45 "The money just deposited will not be available " + 46 "until we verify the amount of any " + 47 "enclosed cash, and any enclosed checks clear." ); 48 49 // credit account to reflect the deposit 50 Database.Credit( AccountNumber, amount ); 51 } // end inner if 52 else 53 UserScreen.DisplayMessageLine( 54 " You did not insert an envelope, so the ATM has " + 55 "canceled your transaction." ); 56 } // end outer if 57 else 58 UserScreen.DisplayMessageLine( " Canceling transaction..." ); 59 } // end method Execute 60 61 // prompt user to enter a deposit amount to credit 62 private decimal PromptForDepositAmount() 63 { 64 // display the prompt and receive input 65 UserScreen.DisplayMessage( 66 " Please input a deposit amount in CENTS (or 0 to cancel): " ); 67 int input = keypad.GetInput(); 68 69 // check whether the user canceled or entered a valid amount 70 if ( input == CANCELED ) 71 return CANCELED; 72 else 73 return input / 100.00M; 74 } // end method PromptForDepositAmount 75 } // end class Deposit

Class Deposit contains a constructor (lines 1321) that passes three parameters to base class transaction's constructor using a constructor initializer. The constructor also has parameters atmKeypad and atmDepositSlot, which it assigns to the corresponding reference instance variables (lines 1920).

Overriding abstract Method Execute

Method Execute (lines 2459) overrides abstract method Execute in base class transaction with a concrete implementation that performs the steps required in a deposit transaction. Line 26 prompts the user to enter a deposit amount by invoking private utility method PromptForDepositAmount (declared in lines 6274) and sets attribute amount to the value returned. Method PromptForDepositAmount asks the user to enter a deposit amount as an integer number of cents (because the ATM's keypad does not contain a decimal point; this is consistent with many real ATMs) and returns the decimal value representing the dollar amount to be deposited.

Getting Deposit Amount with private Utility Method PromptForDepositAmount

Lines 6566 in method PromptForDepositAmount display a message asking the user to input a deposit amount as a number of cents or "0" to cancel the transaction. Line 67 receives the user's input from the keypad. The if statement at lines 7073 determines whether the user has entered a deposit amount or chosen to cancel. If the user chooses to cancel, line 71 returns constant CANCELED. Otherwise, line 73 returns the deposit amount after converting the int number of cents to a dollar-and-cents amount by dividing by the decimal literal 100.00M. For example, if the user enters 125 as the number of cents, line 73 returns 125 divided by 100.00M, or 1.25125 cents is $1.25.

The if statement at lines 2958 in method Execute determines whether the user has chosen to cancel the transaction instead of entering a deposit amount. If the user cancels, line 58 displays an appropriate message, and the method returns. If the user enters a deposit amount, lines 3235 instruct the user to insert a deposit envelope with the correct amount. Recall that Screen method DisplayDollarAmount outputs a decimal value formatted as a dollar amount (including the dollar sign).

Line 38 sets a local bool variable to the value returned by depositSlot's IsDepositEnvelopeReceived method, indicating whether a deposit envelope has been received. Recall that we coded method IsDepositEnvelopeReceived (lines 710 of Fig. J.5) to always return TRue, because we are simulating the functionality of the deposit slot and assume that the user always inserts an envelope in a timely fashion (i.e., within the two-minute time limit). However, we code method Execute of class Deposit to test for the possibility that the user does not insert an envelopegood software engineering demands that programs account for all possible return values. Thus, class Deposit is prepared for future versions of IsDepositEnvelopeReceived that could return false. Lines 4350 execute if the deposit slot receives an envelope. Lines 4347 display an appropriate message to the user. Line 50 credits the user's account in the database with the deposit amount. Lines 5355 execute if the deposit slot does not receive a deposit envelope. In this case, we display a message stating that the ATM has canceled the transaction. The method then returns without crediting the user's account.



    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


    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


    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



    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


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

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