// Denetris Leadership Centre (DLC) product lookup program 
// This program allows the user to enter a product number
// and then display the title, description, and price of 
// that product.
#include <iostream>
#include <string>
using namespace std;

const int NUM_PRODS = 9;     // The number of product produced
const int MIN_PRODNUM = 914; // The lowest product number
const int MAX_PRODNUM = 922; // The highest product number

// Function prototype
int getProdNum();
int binarySearch (const int [],int, int);
void displayProd(const string [], const string [], const double [], int);

int main()
{
    // Array of product IDs
    int id[NUM_PRODS] ={ 914, 915, 916, 917, 918, 919, 920, 921, 922 };
    
    // Array of product titles
    string title[NUM_PRODS] =
            { "Six Steps to Leadership",
              "Six steps to Leadership",
              "The road to excellence",
              "Seven Lesson of Quality",
              "Seven Lesson of Quality",
              "Seven Lesson of Quality",
              "Teams Are Made, Not Born",
              "Leadership For The Future",
              "Leadership For The Future"};
              
    // Array of product descriptions
    string description[ NUM_PRODS] = { "Books", "Audio CD", "DVD",
                                       "Books", "Audio CD", "DVD",
                                       "Books", "Books","Audio CD"};
                                       
    // Array of product prices
    double prices[NUM_PRODS] = {12.95, 14.95, 18.95, 16.95, 21.95,
                                31.95, 14.95, 14.95, 16.95};
                                
    int prodNum;   // to hold a product number
    int index;     // to hold search results
    char again;   // to hold a Y or N answer
    
    do{
        // Get the desired product number
        prodNum = getProdNum();
        
        // Search for the product number
        index = binarySearch(id, NUM_PRODS, prodNum);
        
        // Display the results of the search 
        if(index == 1)
           cout << "That product number was not found.\n";
        else
           displayProd(title, description, prices, index);
           
           // does the user want to do this again?
           cout << "Would you like to look up another product?  (y/n)  ";
           cin  >> again;
    }
    while (again == 'y' || again == 'Y');
    return 0;
}

//******************************************************
// Definiton of getProdNum Function                    * 
// The getProdNum function asks the user to enter a    *
// product umber. The input is validated, and when     *
// a valid number is enterd, it is returned            *
//******************************************************

int getProdNum()
{
    int prodNum;   // product number
    
    cout << "Enter the item's product number: ";
    cin  >> prodNum;
    // Validate input
    while (prodNum < MIN_PRODNUM || prodNum > MAX_PRODNUM)
    {
      cout << "Enter a number in the range of " << MIN_PRODNUM;
      cout << " through " << MAX_PRODNUM << ".\n";
      cin >> prodNum;
    }
    return prodNum;
}

//***************************************************************
// Definition of binarySearch function                          *
// The binarySearch function perfoms a binary search on and     *
// integer array. array , which has a maximum od numElems       *
// elements, is searched for the number stored in value. If the *
// number is found, its array subsrcipt is returned. Otherwise  *
// -1 is returned indicating the value was not in the array     *
//***************************************************************

int binarySearch (const int array[], int numElems, int value)
{
    int first = 0,            // first array element
        last = numElems -1,   // last array element
        middle,               // mid point of search
        position = -1;        // position of search value
    bool found = false;       // flag
    
    while (!found && first <= last)
    {
        middle = (first+last)/2;    // calculate mid point
        if (array[middle] == value) // if value is found at mid
        {
            found = true;
            position = middle;
        }
        else if (array[middle] > value)    // if value is in lower half
           last = middle -1;
        else 
           first = middle + 1;           // if value is in upper half
    }
    return position;
}

//************************************************************
// the displayProd function accepts three arrays and an int  *
// the arrays parameters are expected to hold the title      *
// description, and prices arrays defied in main. The index  * 
// parameter holds a subscript. This function displays the   *
// information in each array contained at the   subscript    *
//************************************************************

void displayProd (const string title[], const string desc[], const double price[], int index)
{
    cout << "Title: " << title[index] << endl;
    cout << "Description: " << desc[index] << endl;
    cout << "Price: $" << price[index] << endl;
}