What we have in this page?
Adding Accounts to the Bank Class
The purpose of the Bank class is to hold Accounts, so the next step is to modify the Bank class to hold a collection of Account objects. Rather than design something from scratch, you’ll use the System::Collections::ArrayList class to hold the Accounts.
Implementing the Add and Remove Methods
The Add and Remove methods provide a way to manipulate the collection of Accounts held by the Bank class.
1. Open the Bank.h header file. Add the following two lines of code immediately after the #pragma once at the top of the file:
|
The using declaration will make it easier to use ArrayList in the Bank class, and you’ll need to reference the Account class later.
2. Add an ArrayList variable to the Bank class (Bank.h), making sure that it’s private.
ArrayList^ accts;
3. Add the following line of code to the Bank constructor (Bank.cpp) to create the ArrayList member.
accts = gcnew ArrayList();
4. Add the code for the public Add method inline in the header file, Bank.h as follows.
bool Add(Account^ pAcc)
{
// check if the account is already in the list
if (accts->Contains(pAcc))
return false;
else
accts->Add(pAcc);
return true;
}
5. Add takes a pointer to an Account object and then uses the ArrayList Contains method to check whether the account already exists in the collection. If it doesn’t, Add adds the Account to the collection. Next, add the Remove that is very similar.
bool Remove(Account^ pAcc)
{
// check if the account is already in the list
if (accts->Contains(pAcc))
{
accts->Remove(pAcc);
return true;
}
else
return false;
}
Remove checks whether an Account is in the ArrayList and removes it if the account is there. It isn’t necessary to call Contains because Remove will silently do nothing if you try to remove an item that isn’t in the list. Users, though, might be interested in knowing that the account they’re trying to remove isn’t in the collection already.
Implementing an Indexed Property to Retrieve Accounts
You can now manipulate the collection of Accounts, adding and removing items. If you want to look up a particular account, you’ll probably want to do so by the account number, and an indexed property provides a good way to access accounts by account number. You’ll only need to retrieve Account references using the property, so you’ll implement a read-only indexed property.
1. Open the Bank.h header file if it isn’t already open. Add the following code to implement the property.
// indexed property of the account number...
property Account^ getAcct[int]
{
// get the account number...
Account^ get(long number)
{
IEnumerator^ ie = accts->GetEnumerator();
// iterate the array...
while (ie->MoveNext())
{
// cast to the Account^ type...
Account^ pa = dynamic_cast<Account^>(ie->Current);
// if found...
if (pa->AccountNumber == number)
// return the account number...
return pa;
}
throw gcnew ArgumentOutOfRangeException(L"Bad account number");
}
}
The function uses an enumerator to iterate over the Accounts in the ArrayList. As long as there are more elements in the collection, MoveNext moves to the next element, which can then be accessed using the Current method. The pointer returned by Current is a generic Object^, so it needs to be cast into an Account^ before you can get at the account number. The cast is done using a dynamic_cast, but you don’t need to check the return from the cast in this case because you can be certain that there is nothing except Account pointers in the collection. When you find an account whose number matches the one passed in, the pointer is returned. If no such account is found, an exception is thrown because trying to access a nonexistent account is equivalent to reading off the end of an array: it’s a serious error that should be signaled to the caller.
2. Test out the Bank class by adding some code to the main() function in Banker.cpp. You’ll need to start by making sure that the Bank.h and Account.h header files are included in the Banker.cpp. Next edit/add some code so that your main() function is similar to the following:
// Banker.cpp : main project file.
#include "stdafx.h"
#include "Bank.h"
#include "Account.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Bank Example");
// Create a Bank object
Bank^ theBank = gcnew Bank();
// Create an Account object
// Account^ theAccount = gcnew Account(123456, 0.0, 0.0);
// Create some accounts
Account^ accountOne = gcnew Account(123456, 100.0, 0.0);
Account^ accountTwo = gcnew Account(234567, 1000.0, 100.0);
Account^ accountThree = gcnew Account(345678, 10000.0, 1000.0);
// Add them to the Bank
theBank->Add(accountOne);
theBank->Add(accountTwo);
theBank->Add(accountThree);
// Use the indexed property to access an account
Account^ pa = theBank->getAcct[123456];
Console::WriteLine(L"Account Number: {0}, Balance is {1}", pa->AccountNumber, pa->Balance);
Account^ pb = theBank->getAcct[234567];
Console::WriteLine(L"Account Number: {0}, Balance is {1}", pb->AccountNumber, pb->Balance);
Account^ pc = theBank->getAcct[345678];
Console::WriteLine(L"Account Number: {0}, Balance is {1}", pc->AccountNumber, pc->Balance);
return 0;
}
After creating a Bank and a number of Account objects, you add the Account objects to the Bank collection by calling Add. You can then use the indexed property to access an account by number and use that pointer to display the balance.
3. Test by building and running your project and the following output should be expected.
------------------------------------------------------
4. Test the property by passing in an account number that doesn’t exist, and check that an exception is thrown. For example add the following code before the return 0; of the main().
Account^ pd = theBank->getAcct[541234];
Console::WriteLine(L"Account Number: {0}, Balance is {1}", pd->AccountNumber, pd->Balance);
The following output should be expected where the exception is thrown.