Having trouble with using iterators in a tree
I'm having trouble with implementing code to iterator asset contrainer and sum by type ....commented in the code where it's to go but also need to implement a loop to scan m_Borrowers container for borrower by phone number. Both are commented out where they are to go. Can you help...almost through this book...stuck on this one.
Code:
//hierarchial data model ==> a general tree example
//
//root (first level) - Loan
//second level - Loan can have zero to 'n' Borrowers
//third level - Borrower can have zero to 'n' Assets
#pragma warning (disable : 4786)
#include <iostream>
#include <list>
#include <string>
using namespace std;
///////////////////////////////////////////////////////////////////////////////////////////
class Asset
{
public:
enum eAssetType
{
eAllAssets,
eAssetChecking,
eAssetSavings,
eAssetRealEstate,
eAssetStock,
eAssetBond,
eAssetMutualFund,
eAssetLifeIns,
eAsset401K,
eAssetIRA,
};
Asset (eAssetType a_type, char *a_sinstname, double a_value)
{
m_eType = a_type;
m_sInstitution = a_sinstname;
m_Value = a_value;
};
~Asset (void) {};
eAssetType m_eType;
string m_sInstitution;
double m_Value;
};
///////////////////////////////////////////////////////////////////////////////////////////////
class Borrower
{
public:
Borrower (void) {};
~Borrower (void) {};
Asset* AddAsset (Asset::eAssetType a_type, char *a_sinstname, double a_value)
{
Asset *pa = new Asset (a_type, a_sinstname, a_value);
m_Assets.push_back (pa);
return (pa);
};
double SumAssets (Asset::eAssetType a_type)
{
double sum_of_assets = 0.0;
Asset *pa = 0;
//implement code to iterate asset container and sum by type here
list <Borrower*> stlList;
typedef list<Borrower*>::iterator AssetIterator;
sum_of_assets = sum_of_assets + Borrower*
return (sum_of_assets);
};
string m_sFirstName;
string m_sLastName;
string m_sPhoneNumber;
string m_sSSN;
private:
list <Asset*> m_Assets;
};
////////////////////////////////////////////////////////////////////////////////////
class Loan
{
public:
Loan (void) { };
~Loan (void) { };
Borrower* AddBorrower (char *a_sfirstname, char *a_slastname, char *a_sphonenumber, char *a_sssn)
{
Borrower *pb = new Borrower;
pb->m_sFirstName = a_sfirstname;
pb->m_sLastName = a_slastname;
pb->m_sPhoneNumber = a_sphonenumber;
pb->m_sSSN = a_sssn;
m_Borrowers.push_back (pb);
return pb;
};
Borrower* GetBorrower (const char *a_sphonenumber)
{
Borrower *pb=0;
//implement for loop to scan m_Borrowers container for borrower by phone number here
return (pb);
}
private:
list <Borrower*> m_Borrowers;
};
////////////////////////////////////////////////////////////////////////////////////
void main (void)
{
Loan loan; //declare an instance of our root node
Borrower *pb1 = loan.AddBorrower ("Ralph", "Pratt", "319-263-0372", "123-45-6789");
Borrower *pb2 = loan.AddBorrower ("Sally", "Pratt", "319-264-8901", "987-65-4321");
pb1->AddAsset (Asset::eAssetChecking, "Wells Fargo", 2495.00);
pb1->AddAsset (Asset::eAsset401K, "Employer", 12459.00);
pb1->AddAsset (Asset::eAssetMutualFund, "Fidelity", 23000.00);
cout << "Borrower named: " << pb1->m_sFirstName << " " << pb1->m_sLastName << " sum of all assets: "
<< pb1->SumAssets(Asset::eAllAssets) << endl;
cout << "Borrower named: " << pb2->m_sFirstName << " " << pb2->m_sLastName << " sum of all assets: "
<< pb2->SumAssets(Asset::eAllAssets) << endl;
string starget = "319-263-2730";
Borrower *pb = loan.GetBorrower (starget.c_str());
if (0 == pb )
cout << "Cannot find borrower with phone number: " << starget << endl;
else
cout << "found phone number: " << starget << endl;
starget = "319-263-0372";
pb = loan.GetBorrower (starget.c_str());
if ( 0 == pb )
cout << "cannot find borrower with phone number: " << starget << endl;
else
cout << "found borrower phone number: " << starget << " " << pb->m_sFirstName << " " << pb->
m_sLastName << endl;
}