Class transaction (Fig. J.8) is an abstract base class that represents the notion of an ATM transaction. It contains the common features of derived classes BalanceInquiry, Withdrawal and Deposit. This class expands on the "skeleton" code first developed in Section 11.9. Line 3 declares this class to be abstract. Lines 57 declare the class's private instance variables. Recall from the class diagram of Fig. 11.22 that class transaction contains the property AccountNumber that indicates the account involved in the TRansaction. Line 5 implements the instance variable accountNumber to maintain the AccountNumber property's data. We derive attributes screen (implemented as instance variable userScreen in line 6) and bankDatabase (implemented as instance variable database in line 7) from class transaction's associations, modeled in Fig. 11.21. All transactions require access to the ATM's screen and the bank's database.
Figure J.8. abstract base class TRansaction represents an ATM transaction.
1 // Transaction.cs
2 // Abstract base class Transaction represents an ATM transaction.
3 public abstract class Transaction
4 {
5 private int accountNumber; // account involved in the transaction
6 private Screen userScreen; // reference to ATM's screen
7 private BankDatabase database; // reference to account info database
8
9 // three-parameter constructor invoked by derived classes
10 public Transaction( int userAccount, Screen theScreen,
11 BankDatabase theDatabase )
12 {
13 accountNumber = userAccount;
14 userScreen = theScreen;
15 database = theDatabase;
16 } // end constructor
17
18 // read-only property that gets the account number
19 public int AccountNumber
20 {
21 get
22 {
23 return accountNumber;
24 } // end get
25 } // end property AccountNumber
26
27 // read-only property that gets the screen reference
28 public Screen UserScreen
29 {
30 get
31 {
32 return userScreen;
33 } // end get
34 } // end property UserScreen
35
36 // read-only property that gets the bank database reference
37 public BankDatabase Database
38 {
39 get
40 {
41 return database;
42 } // end get
43 } // end property Database
44
45 // perform the transaction (overridden by each derived class)
46 public abstract void Execute(); // no implementation here
47 } // end class Transaction
|
Class transaction has a constructor (lines 1016) that takes the current user's account number and references to the ATM's screen and the bank's database as arguments. Because transaction is an abstract class (line 3), this constructor is never called directly to instantiate transaction objects. Instead, this constructor is invoked by the constructors of the TRansaction derived classes via constructor initializers.
Class TRansaction has three public read-only propertiesAccountNumber (lines 1925), UserScreen (lines 2834) and Database (lines 3743). Derived classes of transaction inherit these properties and use them to gain access to class transaction's private instance variables. Note that we chose the names of the UserScreen and Database properties for claritywe wanted to avoid property names that are the same as the class names Screen and BankDatabase, which can be confusing.
Class transaction also declares abstract method Execute (line 46). It does not make sense to provide an implementation for this method in class transaction, because a generic transaction cannot be executed. Thus, we declare this method to be abstract, forcing each transaction concrete derived class to provide its own implementation that executes the particular type of transaction.
J 10 Class BalanceInquiry |
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