|
|
36 Module 9: Creating and Destroying Objects
7. Add two accessor methods, called Amount and When, that return the
values of the two instance variables:
private readonly decimal amount;
private readonly DateTime when;
...
public decimal Amount( )
{
return amount;
}
public DateTime When( )
{
return when;
}
? To create the constructor
1. Define a public constructor for the BankTransaction class. It will take a
decimal parameter called tranAmount that will be used to populate the
amount instance variable.
2. In the constructor, initialize when with DateTime.Now.
DateTime.Now is a property and not a method, so you do not need to
use parentheses.
The completed constructor is as follows:
public BankTransaction(decimal tranAmount)
{
amount = tranAmount;
when = DateTime.Now;
}
3. Compile the project and correct any errors.
? To create transactions
1. As described above, transactions will be created by the BankAccount class
and stored in a queue whenever the Deposit or Withdraw method is
invoked. Return to the BankAccount class.
2. Before the start of the BankAccount class, add the following using
directive:
using System.Collections;
3. Add a private instance variable call tranQueue to the BankAccount class.
Its data type should be Queue and it should be initialized with a new empty
queue:
private Queue tranQueue = new Queue( );
Tip
Module 9: Creating and Destroying Objects 37
4. In the Deposit method, before returning, create a new transaction using the
deposit amount as the parameter, and append it to the queue by using the
Enqueue method, as follows:
public decimal Deposit(decimal amount)
{
accBal += amount;
BankTransaction tran = new BankTransaction(amount);
tranQueue.Enqueue(tran);
return accBal;
}
5. In the Withdraw method, if there are sufficient funds, create a transaction
and append it to tranQueue as in the Deposit method, as follows:
public bool Withdraw(decimal amount)
{
bool sufficientFunds = accBal >= amount;
if (sufficientFunds) {
accBal -= amount;
BankTransaction tran = new BankTransaction(-amount);
tranQueue.Enqueue(tran);
}
return sufficientFunds;
}
For the Withdraw method, the value passed to the constructor of the
BankTransaction should be the amount being withdrawn preceded by the
negative sign.
Note
38 Module 9: Creating and Destroying Objects
? To test transactions
1. For testing purposes, add a public method called Transactions to the
BankAccount class. Its return type should be Queue , and the method
should return tranQueue. You will use this method for displaying
transactions in the next step. The method will be as follows:
public Queue Transactions( )
{
return tranQueue;
}
2. In the CreateAccount class, modify the Write method to display the details
of transactions for each account. Queues implement the IEnumerable
interface, which means that you can use the foreach construct to iterate
through them.
3. In the body of the foreach loop, print out the date and time and the amount
for each transaction, by using the When and Amount methods, as follows:
static void Write(BankAccount acc)
{
Console.WriteLine("Account number is {0}",
acc.Number( ));
Console.WriteLine("Account balance is {0}",
acc.Balance( ));
Console.WriteLine("Account type is {0}", acc.Type( ));
Console. WriteLine("Transactions:" ;
foreach (BankTransaction tran in acc.Transactions( ))
{
Console.WriteLine("Date/Time: {0}\tAmount: {1}",
êtran.When( ), tran.Amount( ));
}
Console.WriteLine( );
}
4. In the Main method, add statements to deposit and w ithdraw money from
each of the four accounts (acc1, acc2, acc3, and acc4).
5. Compile the project and correct any errors.
6. Execute the project. Examine the output and check whether transactions are
displayed as expected.
Module 9: Creating and Destroying Objects 39
u Objects and Memory
n Object Lifetime
n Objects and Scope
n Garbage Collection
In this section, you will learn what happens when an object, as opposed to a
value, goes out of scope or is destroyed and about the role of garbage collection
in this process. |
|