// Program to handle inventory for a small company
//  File: ch7inv1.cpp

#include <iostream.h>
#include <conio.h>

const  int MAX_ITEMS = 5;

typedef int InventoryArray[MAX_ITEMS];

void DisplayWelcomeMessage();
void DisplayClosingMessage();
void OutputInventory(const InventoryArray items);  // const used to protect the array from being changed
void InputInventory(InventoryArray items);
void ProcessChoices();

void OutputOneInventoryAmount(int amount, int itemCode);
void ChangeOneInventoryAmount(InventoryArray items, int itemCode);
char GetItemCode();

void DeductInventory(InventoryArray items, int itemCode);


void AddInventory(InventoryArray items, int itemCode);

char GetUsersChoice();


void main()
{


	DisplayWelcomeMessage();
	ProcessChoices();
	DisplayClosingMessage();
}



void DisplayWelcomeMessage()
{       clrscr; // clears the screen 
         cout << "\n\n\n\n\n\n\n\n\n\n";
	cout << "                 W E L C O M E   T O\n\n";
	cout << "                         T H E\n\n";
	cout << "             I N V E N T O R Y   P R O G R A M\n\n\n\n\n\n";
// This messabe could be made much more interesting
}

void DisplayClosingMessage()
{   cout << "           T H A N K S   F O R  S T O P P I N G   B Y\n\n\n\n\n\n";
}

void ProcessChoices()
{
	InventoryArray itemInventory;

	char choice;
	int itemCode;

	InputInventory(itemInventory);   // Create the initial inventory

	bool done = false;

	choice = GetUsersChoice();
	while (!done)
	{
		switch (choice)
		{	case 'L':  // List Inventory
			case 'l':
				OutputInventory(itemInventory);
				break;

			case 'S':  // Single product listed
			case 's':
				itemCode = GetItemCode();
				while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
				// check to make sure code is valid - user is told to enter neg # to stop
				{	OutputOneInventoryAmount(itemInventory[itemCode], itemCode);
					itemCode = GetItemCode();
					cout <<  "\n\n\n";
				}
				break;
			case 'C':
			case 'c':
				itemCode = GetItemCode();
				while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
				// check to make sure code is valid - user is told to enter neg # to stop
				{	ChangeOneInventoryAmount(itemInventory, itemCode);
					itemCode = GetItemCode();
					cout <<  "\n\n\n";
				}
				break;
			case 'A': // Add to inventory
			case 'a':
				itemCode = GetItemCode();
				while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
				// check to make sure code is valid - user is told to enter neg # to stop
				{	AddInventory(itemInventory, itemCode);
					itemCode = GetItemCode();
					cout <<  "\n\n\n";
				}
				break;
			case 'D': // Deduct from inventory
			case 'd':
                                itemCode = GetItemCode();
				while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
				// check to make sure code is valid - user is told to enter neg # to stop
				{ DeductInventory(itemInventory, itemCode);
					itemCode = GetItemCode();
					cout << "\n\n\n";
				}
				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 Select again.\n\n";
		} // end switch
               if (!done)
		{
			choice = GetUsersChoice();
		}

	} // end while
}

char GetUsersChoice()
{  char choice;
	cout <<  "\n\n\n\n";
	cout << "Please indicate what you wish to do\n";
	cout << "   <L> to List all the inventory\n";
	cout << "   <S> to get the inventory of one item\n";
	cout << "   <D> to deduct from the inventory of an item\n";
	cout << "   <A> to add to the inventory of an item\n";
	cout << "   <C> to change the inventory of an item\n";
	cout << "   <Q> to quit\n";
	cin >> choice;
	return choice;
}


char GetItemCode()
{	int code;
	cout << "Please enter the code for the item you wish to work with\n";
	cout << "Enter a negative number to indicate you are finished\n";
	cin >> code;
	while (code > MAX_ITEMS -1) // check for code that is too high
	{	cout << "Invalid item code. Please enter a valid code.\n";
		cin >> code;
	}
//	cin.ignore(1);
	return code;

}


void OutputOneInventoryAmount(int amount, int itemCode)
{ 	cout << endl << endl << "The inventory amount for item " << itemCode
			  << " is: "				  <<  amount << endl << endl;
}

void OutputInventory(const InventoryArray items)
{
	cout << "\n\n\n\n\n";
	for (int itemCode = 0; itemCode < MAX_ITEMS;  itemCode++)
	{
		cout << "The inventory amount for item " << itemCode
			  << " is: "				  <<  items[itemCode] << endl;
	}
	cout << "\n\n\n\n\n\n";
}


void InputInventory(InventoryArray items)
{ 	int itemAmount;
	for (int itemCode = 0; itemCode < MAX_ITEMS;  itemCode++)
	{
		cout << "Please enter the inventory count for item " << itemCode << ": ";
		cin >> itemAmount;
		items[itemCode] = itemAmount;
	}
}

void ChangeOneInventoryAmount(InventoryArray items, int itemCode)
{	int amount;
	cout << "Please enter the new amount in the inventory\n";
	cin >> amount;
	items[itemCode] = amount;
}

void DeductInventory(InventoryArray items, int itemCode)
{	int amount;
	cout << "Please enter the amount sold of item\n";
	cin >> amount;
	items[itemCode] -= amount;
}


void AddInventory(InventoryArray items, int itemCode)
{	int amount;
	cout << "Please enter the amount to add of item\n";
	cin >> amount;
	items[itemCode] += amount;
}