// Class Definitions for ListOfCOntracts with the ability to add as many contracts
// as desired
// Also set up to allow a user program to iterate through the list
// File: ch10lst5.cpp
#include <iostream.h>
#include "common2.h"
#include "ch10lst5.h"
ListOfContracts :: ListOfContracts()
{
numContracts = 0;
presentMax = CONTRACT_GROWTH;
listOfContracts = new Contract[CONTRACT_GROWTH];
next = 0;
}
ListOfContracts :: ~ListOfContracts()
{
delete [] listOfContracts;
}
void ListOfContracts :: Add(Contract contract)
{ int contractNum = contract.ProvideID();
int index = Search(contractNum);
if (index == numContracts) // contract with this ID does not exist
{ if (numContracts == presentMax) // Need add contract but no room
{ IncreaseSize();
}
listOfContracts[numContracts].CopyContract(contract);
// copy contract into list
numContracts++;
}
else
{ cout << "\nA contract with this ID already exists.\n";
}
}
void ListOfContracts :: Delete(int contractNum)
// This function will not worry about returning memory if the list grow
// very small after having grown big. This is only important if there
// are many lists in the same program
{
int index;
index = Search(contractNum);
if (index < numContracts) // the contract to be deleted exists
{ for (int i = index; i < numContracts - 1; i++)
{ //listOfContracts[i] = listOfContracts[i+1];
listOfContracts[i].CopyContract(listOfContracts[i+1]);
// copy contract into list
}
numContracts--;
}
else
{ cout << "Invalid Contract ID\n\n";
}
}
int ListOfContracts :: Search(int contractID)
{
int index;
for (index = 0; index < numContracts; index++)
{ if (contractID == listOfContracts[index].ProvideID())
{ return index;
}
}
return index;
}
bool ListOfContracts :: FindContract(int ID, Contract& contract)
{
int index = 0;
while (index < numContracts) // keep searching until ID found
// or no more contracts
{ if (ID == listOfContracts[index].ProvideID())
{ //contract = listOfContracts[index];
contract.CopyContract(listOfContracts[index]);
// copy contract into list
return true;
}
else
{ index++;
}
}
return false; // contract ID not found in list
}
void ListOfContracts :: ReplaceContract (Contract contract)
{
int index;;
int ID = contract.ProvideID(); // Get ID of the contract so we can find it in
// in the list.
index = Search(ID);
if (index < numContracts) // the contract to be replaced exists
{ // listOfContracts[index] = contract;
listOfContracts[index].CopyContract(contract);
}
else
{ cout << "\nContract to be replaced does not exist in the list.\n";
}
}
void ListOfContracts :: Reset()
{ next = 0;
}
Contract ListOfContracts :: GetNext()
{
return listOfContracts[next++];
}
bool ListOfContracts :: AreThereMore()
{ if (next < numContracts)
{ return true;
}
else
{ return false;
}
// More complex: the single line: return (next < numContracts)
}
void ListOfContracts :: IncreaseSize()
{ presentMax += CONTRACT_GROWTH;
Contract* tempArray = new Contract[presentMax]; // create new list
for (int index = 0; index < numContracts; index++)
{ tempArray[index].CopyContract(listOfContracts[index]);
}
delete [] listOfContracts;
listOfContracts = tempArray;
}