// CLass Definitions for ListOfCOntracts with the ability to add as many contracts
// as desired
// File: c10lst1a.cpp
#include <iostream.h>
#include "common.h"
#include "c10lst1a.h"
ListOfContracts :: ListOfContracts()
{
numContracts = 0;
presentMax = CONTRACT_GROWTH;
listOfContracts = new Contract[CONTRACT_GROWTH];
}
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] = 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];
}
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];
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;
}
else
{ cout << "\nContract to be replaced does not exist in the list.\n";
}
}
void ListOfContracts :: DisplayAll()
{
int contractID;
int sqFootage;
int numDesks;
int numDays;
double charge;
cout << endl << endl; // To set off the output
for (int contract = 0; contract < numContracts; contract++)
{
contractID = listOfContracts[contract].ProvideID();
sqFootage = listOfContracts[contract].ProvideSquareFootage();
cout << "The square footage for contract " << contractID << " is: "
<< sqFootage << endl;
numDesks = listOfContracts[contract].ProvideNumberOfDesks();
cout << "The number of desks for contract " << contractID << " is: "
<< numDesks << endl;
numDays = listOfContracts[contract].ProvideNumberOfDays();
cout << "The number of days for contract " << contractID << " is: "
<< numDays << endl;
charge = listOfContracts[contract].ProvidePerWeekCharge();
cout << "The per week charge for contract " << contractID <<" is: "
<< charge << endl;
cout << endl << endl;
}
}
void ListOfContracts :: IncreaseSize()
{ presentMax += CONTRACT_GROWTH;
Contract* tempArray = new Contract[presentMax]; // create new list
for (int index = 0; index < numContracts; index++)
{ tempArray[index] = listOfContracts[index];
}
delete [] listOfContracts;
listOfContracts = tempArray;
}