// main for Chapter 10, Section 8 version of contract program using a list   - with strings
//			Also: Prints all contracts from this file
// file:   ch10tst1.cpp


#include <iostream.h>
#include <conio.h>
#include "contrct6.h"
#include "common2.h"
#include "ch10lst1.h"

typedef  char String80[81];

void GetProperties(String80 properties);
void GetNewString(String80 string, String80 property);
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 DisplayContract(ListOfContracts& contracts);
void ChangeContract(ListOfContracts& contracts);
void DisplayAllContracts(ListOfContracts& contracts);
void AddContract(ListOfContracts& contracts);
void DeleteContract(ListOfContracts& contracts);

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;
}

void GetNewString(String80 string, String80 property)
{
	cout << "Please enter the new " << property << endl;
	cin.ignore(80, '\n'); // clear out any unprocessed characters - especially \0
	cin.get(string, 81);
}

void 	ChangeInfo( Contract& contract)
{  String80 string;
	String80 properties;
	GetProperties(properties);
	int index = 0;
	while (properties[index])
	{	switch (properties[index])
		{  case '1':
				GetNewString(string, "ContracteeName");
				contract.ChangeContracteeName(string);
				break;

			case '2':
				GetNewString(string, "Office Street Address");
				contract.ChangeOfficeStreetAddress(string);
				break;
			case '3':
				GetNewString(string, "Office City");
				contract.ChangeOfficeCity(string);
				break;
			case '4':
				GetNewString(string, "Office State");
				contract.ChangeOfficeState(string);
				break;
			case '5':
				GetNewString(string, "Office Zip Code");
				contract.ChangeOfficeZip(string);
				break;
			case '6':
				int sqFootage;
				sqFootage = GetSquareFootage();
				contract.ChangeSquareFootage(sqFootage);
				break;
			case '7':
				int numDesks;
				numDesks = GetNumDesks();
				contract.ChangeNumDesks(numDesks);
				break;
			case '8':
				int numDays;
				numDays = GetNumDays();
				contract.ChangeNumDays(numDays);
				break;
			default:
				cout << endl << properties[index] << " is an invalid code.\n";

		}  // end switch
		index++;
	} // end while
	clrscr();   // Clear the screen before displaying new contact
                      // Need include the file 'conio.h'
	DisplayInfo(contract); // allow user to see changes before proceeding
}



void CreateContract(int contractID, Contract& contract)
{  String80 string;
	contract.ChangeID(contractID);

	GetNewString(string, "ContracteeName");
	contract.ChangeContracteeName(string);

	GetNewString(string, "Office Street Address");
	contract.ChangeOfficeStreetAddress(string);

	GetNewString(string, "Office City");
	contract.ChangeOfficeCity(string);

	GetNewString(string, "Office State");
	contract.ChangeOfficeState(string);

	GetNewString(string, "Office Zip Code");
	contract.ChangeOfficeZip(string);

	int sqFootage;
	sqFootage = GetSquareFootage();
	contract.ChangeSquareFootage(sqFootage);

	int numDesks;
	numDesks = GetNumDesks();
	contract.ChangeNumDesks(numDesks);

	int numDays;
	numDays = GetNumDays();
	contract.ChangeNumDays(numDays);

	clrscr();
	DisplayInfo(contract);
}


void DisplayInfo(Contract contract)
{
	cout << "              Contract ID: "
		  << contract.ProvideID() << endl;
	cout << "        Contractee Name:  " << contract.ProvideContracteeName()
		  << endl;

	cout << "\n        Office ";
	cout << "Address:   "<< contract.ProvideOfficeStreetAddress() << "  "
		  << endl
		  << "                          " << contract.ProvideOfficeCity() << ", "
		  << contract.ProvideOfficeState() << " "
		  << contract.ProvideOfficeZip() <<  endl;

	int sqFootage;
	sqFootage = contract.ProvideSquareFootage();
	cout << "\nThe square footage: " << sqFootage << "     ";

	int numDesks;
	numDesks = contract.ProvideNumberOfDesks();
	cout << "The number of desks: " << numDesks << endl;

	int numDays;
	numDays = contract.ProvideNumberOfDays();
	cout << "The number of days:  " << numDays << "   ";

	double charge;
	charge = contract.ProvidePerWeekCharge();
	cout << "The per week charge:   " << charge << endl;
	cout << endl << endl;

}



void GetProperties(String80 properties)
{  cin.ignore(80, '\n');
	cout << "Please enter the codes corresponding to the properties you want to change\n";
	cout << "Enter as many as you desire. When finished, hit Enter.\n";

	cout << "          '1':  Contractee Name\n";
	cout << "          '2':  Office Street Address\n";
	cout << "          '3':  Office City\n";
	cout << "          '4':  Office State\n";
	cout << "          '5':  Office Zip\n";
	cout << "          '6':  square Footage\n";
	cout << "          '7':  number of desks\n";
	cout << "          '8':  number of days per week\n";
	cin.get(properties,81);
}

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;
choice = GetOperatorChoice();

while (!done)
	{	switch (choice)
		{	case 'D':
			case 'd':
				DisplayContract(contracts);
				break; // case 'd'
			case 'C':
			case 'c':
				ChangeContract(contracts);
				break; // case 'c'

			case 'L':
			case 'l':
				DisplayAllContracts(contracts);
				break;

			case 'A':
			case 'a':
				AddContract(contracts);
				break;

			case 'E':
			case 'e':
				DeleteContract(contracts);
				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 DisplayContract(ListOfContracts& contracts)
{ int contractID;
  bool found;
  Contract contract;
  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();
	}
}

void ChangeContract(ListOfContracts& contracts)
{  int contractID;
	bool found;
	Contract contract;
	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();
	}
}

void DisplayAllContracts(ListOfContracts& contracts)
{  Contract contract;
	contracts.Reset();
	for (; contracts.AreThereMore(); )
	{  contract.CopyContract(contracts.GetNext());
		DisplayInfo(contract);
	}
}

void AddContract(ListOfContracts& contracts)
{  int contractID;
	contractID = GetContractID();
	Contract contract;
	while (contractID != 0)   // The user wants to add another contract
	{	CreateContract(contractID, contract);
		contracts.Add(contract);
		contractID = GetContractID();
	}
}

void DeleteContract(ListOfContracts& contracts)
{  int contractID;
	contractID = GetContractID();
	while (contractID != 0) // The user wants to delete a contract
	{	contracts.Delete(contractID);
		contractID = GetContractID();
	}
}


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.ChangeContracteeName("Freddy Smith");
	contract.ChangeOfficeStreetAddress("901 National Avenue");
	contract.ChangeOfficeCity("Las Vegas");
	contract.ChangeOfficeState("New Mexico");
	contract.ChangeOfficeZip("87701");

	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.ChangeContracteeName("Naomi Madrid");
	contract.ChangeOfficeStreetAddress("902 National Avenue");
	contract.ChangeOfficeCity("Las Vegas");
	contract.ChangeOfficeState("New Mexico");
	contract.ChangeOfficeZip("87701");

	contract.ChangeID(2345);
	contract.ChangeSquareFootage(500);
	contract.ChangeNumDesks(10);
	contract.ChangeNumDays(3);

	contracts.Add(contract);

	contract.ChangeContracteeName("Freddy Smith");
        contract.ChangeOfficeStreetAddress("1234 8th St");
	contract.ChangeOfficeCity("Las Vegas");
	contract.ChangeOfficeState("New Mexico");
	contract.ChangeOfficeZip("87701");

	contract.ChangeID(3456);
	contract.ChangeSquareFootage(200);
	contract.ChangeNumDesks(1);
	contract.ChangeNumDays(4);

	contracts.Add(contract);

	contract.ChangeContracteeName("Maria Galleogs");
        contract.ChangeOfficeStreetAddress("123 Old National Avenue");
	contract.ChangeOfficeCity("Las Vegas");
	contract.ChangeOfficeState("New Mexico");
	contract.ChangeOfficeZip("87701");

	contract.ChangeID(4567);
	contract.ChangeSquareFootage(2000);
	contract.ChangeNumDesks(20);
	contract.ChangeNumDays(5);

	contracts.Add(contract);

	contract.ChangeContracteeName("Tony Tallarico");
	contract.ChangeOfficeStreetAddress("777 La Plaza");
	contract.ChangeOfficeCity("Las Vegas");
	contract.ChangeOfficeState("New Mexico");
	contract.ChangeOfficeZip("87701");

	contract.ChangeID(5678);
	contract.ChangeSquareFootage(30);
	contract.ChangeNumDesks(2);
	contract.ChangeNumDays(2);

	contracts.Add(contract);
  }