// main for books version of contract program using a list - no strings
// Also: Displays all contracts from this file using iterator
// Used for code for end of part Section 4 of chapter 10.
// file: ch10tst7.cpp
#include <iostream.h>
#include <conio.h>
#include "contrct4.h"
#include "common2.h"
#include "ch10lst5.h"
char GetProperty();
char GetOperatorChoice();
int GetContractID();
void DisplayInfo(Contract contract);
void ChangeInfo(Contract& contract);
void DisplayClosingMessage();
void DisplayIntro();
int GetSquareFootage();
int GetNumDesks();
int GetNumDays();
void ProcessUserChoices();
void InitializeInstances(ListOfContracts& contracts);
void CreateContract(int contractID, Contract& contract);
void main()
{
DisplayIntro();
ProcessUserChoices();
DisplayClosingMessage();
}
char GetOperatorChoice()
{// please enter choice
// make a while loop and talk about redundancy.
char choice;
cout << "Please choose one of the following operations\n";
cout << " 'D' to display the information for a contract\n";
cout << " 'L' to display the information on ALL contracts\n";
cout << " 'C' to change the information for a contract\n";
cout << " 'A' to add a contract to the list\n";
cout << " 'E' to eliminate a contract from the list\n";
cout << " 'Q' to quit the program\n";
cin >> choice;
return choice;
}
int GetContractID()
{// loop choice 1-5
int num;
cout << "\nPlease enter the ID for the contract you wish to work with\n";
cout << "Choose 0 (zero) when you are finished with this operation.\n";
cin >> num;
return num;
}
char GetProperty()
{ char property;
cout << "Please enter the letter corresponding to the property you want to change\n";
cout << " 'S': square Footage\n";
cout << " 'K': number of desks\n";
cout << " 'Y': number of days per week\n";
cout << " 'N': none\n";
cin >> property;
return property;
}
void ChangeInfo(Contract& contract)
{
char property;
property = GetProperty();
while ((property != 'N') && (property != 'n'))
{ switch (property)
{ case 'S':
case 's':
int sqFootage;
sqFootage = GetSquareFootage();
contract.ChangeSquareFootage(sqFootage);
break;
case 'K':
case 'k':
int numDesks;
numDesks = GetNumDesks();
contract.ChangeNumDesks(numDesks);
break;
case 'Y':
case 'y':
int numDays;
numDays = GetNumDays();
contract.ChangeNumDays(numDays);
break;
default:
cout << "\nInvalid choice of properties. Please try again\n";
} // end switch
property = GetProperty();
}
}
void CreateContract(int contractID, Contract& contract)
{ contract.ChangeID(contractID);
int sqFootage;
sqFootage = GetSquareFootage();
contract.ChangeSquareFootage(sqFootage);
int numDesks;
numDesks = GetNumDesks();
contract.ChangeNumDesks(numDesks);
int numDays;
numDays = GetNumDays();
contract.ChangeNumDays(numDays);
}
void DisplayInfo(Contract contract)
{
cout << " Contract ID: "
<< contract.ProvideID() << endl << endl;
int sqFootage;
sqFootage = contract.ProvideSquareFootage();
cout << "\nThe square footage for the contract is: " << sqFootage << endl;
int numDesks;
numDesks = contract.ProvideNumberOfDesks();
cout << "The number of desks for the contract is: " << numDesks << endl;
int numDays;
numDays = contract.ProvideNumberOfDays();
cout << "The number of days for the contract is: " << numDays << endl;
double charge;
charge = contract.ProvidePerWeekCharge();
cout << "The per week charge for the contract is: " << charge << endl;
cout << endl << endl;
}
void DisplayClosingMessage()
{ cout << "\n\n\n H A V E F U N";
}
void DisplayIntro()
{ cout << " W E L CO M E TO O L Y M P I A ' S\n";
cout << " O F F I C E C O N T R A C T P R O G R A M\n\n\n";
}
int GetSquareFootage()
{ int sqFootage;
cout << "\nWhat is the new square footage for the contract?";
cin >> sqFootage;
return sqFootage;
}
int GetNumDesks()
{ int numDesks;
cout << "\nWhat is the new number of desks for the contract?";
cin >> numDesks;
return numDesks;
}
int GetNumDays()
{ int numDays;
cout << "\nWhat is the new number of days for the contract?";
cin >> numDays;
return numDays;
}
void ProcessUserChoices()
{
// Code to create the list of contracts
ListOfContracts contracts;
InitializeInstances(contracts);
bool done = false; // Is the user finished processing contracts?
char choice;
int contractID;
Contract contract;
bool found; // Was the contract found in the list of contracts
choice = GetOperatorChoice();
while (!done)
{ switch (choice)
{ case 'D':
case 'd':
contractID = GetContractID();
while (contractID != 0)
{ found = contracts.FindContract(contractID, contract);
if (found)
{ clrscr();
DisplayInfo(contract);
}
else
{ cout << "\nContract " << contractID << " is not in list.\n";
}
contractID = GetContractID();
}
break; // case 'd'
case 'C':
case 'c':
contractID = GetContractID();
while (contractID != 0)
{ found = contracts.FindContract(contractID, contract);
if (found)
{ ChangeInfo(contract);
contracts.ReplaceContract(contract);
}
else
{ cout << "\nContract " << contractID << " is not in list.\n";
}
contractID = GetContractID();
}
break; // case 'c'
case 'L':
case 'l':
contracts.Reset();
for (; contracts.AreThereMore() ; )
{ contract = contracts.GetNext();
DisplayInfo(contract);
}
break;
case 'A':
case 'a':
contractID = GetContractID();
while (contractID != 0) // The user wants to add another contract
{ CreateContract(contractID, contract);
contracts.Add(contract);
contractID = GetContractID();
}
break;
case 'E':
case 'e':
contractID = GetContractID();
while (contractID != 0) // The user wants to delete a contract
{ contracts.Delete(contractID);
contractID = GetContractID();
}
break;
case 'Q':
case 'q':
cout << "Do you want to really want top quit (Y) or (N)\n";
char answ;
cin >> answ;
if ((answ =='Y') || (answ == 'y'))
{
done = true;
}
break;
default:
cout << "Invalid choice. Please try again";
choice = GetOperatorChoice();
} // end switch
if (!done)
{
choice = GetOperatorChoice();
}
}
}
void InitializeInstances(ListOfContracts& contracts)
{ // In a real program this info would be read in from a file.
// In this test code we will hard code in the initial values.
Contract contract;
// Create a contract, assign its properties values and insert it into the list.
contract.ChangeID(1234);
contract.ChangeSquareFootage(1000);
contract.ChangeNumDesks(3);
contract.ChangeNumDays(5);
contracts.Add(contract);
// We take the same local contract variables, put new values in some of them
// and store the new contract in the list of contracts.
// This is repeated until we have five contracts in the list - enough for a test.
Contract contract1;
contract.ChangeID(2345);
contract.ChangeSquareFootage(500);
contract.ChangeNumDesks(10);
contract.ChangeNumDays(3);
contracts.Add(contract);
contract.ChangeID(3456);
contract.ChangeSquareFootage(200);
contract.ChangeNumDesks(1);
contract.ChangeNumDays(4);
contracts.Add(contract);
contract.ChangeID(4567);
contract.ChangeSquareFootage(2000);
contract.ChangeNumDesks(20);
contract.ChangeNumDays(5);
contracts.Add(contract);
contract.ChangeID(5678);
contract.ChangeSquareFootage(30);
contract.ChangeNumDesks(2);
contract.ChangeNumDays(2);
contracts.Add(contract);
}