J.7. Class Account

Class Account (Fig. J.6) represents a bank account. Each Account has four attributes (modeled in Fig. 11.22)accountNumber, pin, availableBalance and totalBalance. Lines 58 implement these attributes as private instance variables. For each of the instance variables accountNumber, availableBalance and totalBalance, we provide a property with the same name as the attribute, but starting with a capital letter. For example, property AccountNumber corresponds to the accountNumber attribute modeled in Fig. 11.22. Clients of this class do not need to modify the accountNumber instance variable, so AccountNumber is declared as a read-only property (i.e., it provides only a get accessor).

Figure J.6. Class Account represents a bank account.

 1 // Account.cs
 2 // Class Account represents a bank account.
 3 public class Account
 4 {
 5 private int accountNumber; // account number
 6 private int pin; // PIN for authentication
 7 private decimal availableBalance; // available withdrawal amount
 8 private decimal totalBalance; // funds available + pending deposit
 9
10 // four-parameter constructor initializes attributes
11 public Account( int theAccountNumber, int thePIN,
12 decimal theAvailableBalance, decimal theTotalBalance )
13 {
14 accountNumber = theAccountNumber;
15 pin = thePIN;
16 availableBalance = theAvailableBalance;
17 totalBalance = theTotalBalance;
18 } // end constructor
19
20 // read-only property that gets the account number
21 public int AccountNumber
22 {
23 get
24 {
25 return accountNumber;
26 } // end get
27 } // end property AccountNumber
28
29 // read-only property that gets the available balance
30 public decimal AvailableBalance
31 {
32 get
33 {
34 return availableBalance;
35 } // end get
36 } // end property AvailableBalance
37
38 // read-only property that gets the total balance
39 public decimal TotalBalance
40 {
41 get
42 {
43 return totalBalance;
44 } // end get
45 } // end property TotalBalance
46
47 // determines whether a user-specified PIN matches PIN in Account
48 public bool ValidatePIN( int userPIN )
49 {
50 return ( userPIN == pin );
51 } // end method ValidatePIN
52
53 // credits the account (funds have not yet cleared) 54 public void Credit( decimal amount ) 55 { 56 totalBalance += amount; // add to total balance 57 } // end method Credit 58 59 // debits the account 60 public void Debit( decimal amount ) 61 { 62 availableBalance -= amount; // subtract from available balance 63 totalBalance -= amount; // subtract from total balance 64 } // end method Debit 65 } // end class Account

Class Account has a constructor (lines 1118) that takes an account number, the PIN established for the account, the initial available balance and the initial total balance as arguments. Lines 1417 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.

(This item is displayed on pages 1540 - 1541 in the print version)

 1 // BankDatabase.cs
 2 // Represents the bank account information database
 3 public class BankDatabase
 4 {
 5 private Account[] accounts; // array of the bank's Accounts
 6
 7 // parameterless constructor initializes accounts
 8 public BankDatabase()
 9 {
10 // create two Account objects for testing and
11 // place them in the accounts array
12 accounts = new Account[ 2 ]; // create accounts array
13 accounts[ 0 ] = new Account( 12345, 54321, 1000.00M, 1200.00M );
14 accounts[ 1 ] = new Account( 98765, 56789, 200.00M, 200.00M );
15 } // end constructor
16
17 // retrieve Account object containing specified account number
18 private Account GetAccount( int accountNumber )
19 {
20 // loop through accounts searching for matching account number
21 foreach ( Account currentAccount in accounts )
22 {
23 if ( currentAccount.AccountNumber == accountNumber )
24 return currentAccount;
25 } // end foreach
26
27 // account not found
28 return null;
29 } // end method GetAccount
30 31 // determine whether user-specified account number and PIN match 32 // those of an account in the database 33 public bool AuthenticateUser( int userAccountNumber, int userPIN) 34 { 35 // attempt to retrieve the account with the account number 36 Account userAccount = GetAccount( userAccountNumber ); 37 38 // if account exists, return result of Account function ValidatePIN 39 if ( userAccount != null ) 40 return userAccount.ValidatePIN( userPIN ); // true if match 41 else 42 return false; // account number not found, so return false 43 } // end method AuthenticateUser 44 45 // return available balance of Account with specified account number 46 public decimal GetAvailableBalance( int userAccountNumber ) 47 { 48 Account userAccount = GetAccount( userAccountNumber ); 49 return userAccount.AvailableBalance; 50 } // end method GetAvailableBalance 51 52 // return total balance of Account with specified account number 53 public decimal GetTotalBalance( int userAccountNumber ) 54 { 55 Account userAccount = GetAccount(userAccountNumber); 56 return userAccount.TotalBalance; 57 } // end method GetTotalBalance 58 59 // credit the Account with specified account number 60 public void Credit( int userAccountNumber, decimal amount ) 61 { 62 Account userAccount = GetAccount( userAccountNumber ); 63 userAccount.Credit( amount ); 64 } // end method Credit 65 66 // debit the Account with specified account number 67 public void Debit( int userAccountNumber, decimal amount ) 68 { 69 Account userAccount = GetAccount( userAccountNumber ); 70 userAccount.Debit( amount ); 71 } // end method Debit 72 } // end class BankDatabase

public Read-Only Properties of Class Account

Read-only property AccountNumber (lines 2127) provides access to an Account's accountNumber instance variable. 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.

Read-only properties AvailableBalance (lines 3036) and TotalBalance (lines 3945) allow clients to retrieve the values of private decimal instance variables availableBalance and totalBalance, 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 pending confirmation of cash in deposit envelopes or clearance of checks in deposit envelopes.

public Methods of Class Account

Method ValidatePIN (lines 4851) 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.23. If the two PINs match, the method returns true; otherwise, it returns false.

Method Credit (lines 5457) 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 totalBalance (line 56). 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 be verified and 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 availableBalance to confirm a deposit or to subtract from attribute totalBalance to reject a deposit.

Method Debit (lines 6064) 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 availableBalance (line 62) and instance variable totalBalance (line 63), because a withdrawal affects both balances.

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



    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