//Programmer: Bo Bayles  Date: 5 October 2004
//File: lab7.cpp         Class: CS54 C
//Purpose: This program calculates total profit for a bookstore
//  by determining profits for different types of items.

#include <iostream>
using namespace std;

float AddItem();
float GetRate (int iProduct);
void DisplayProfit(float fTotalProfit);
//Declare functions to be used.


int main()
{
  int iChoice = 0;
  float fTotalProfit = 0;
  //Declare variables for the user choice and final profit.

  cout.setf (ios::showpoint);
  cout.precision (2);
  cout.setf (ios::fixed);
  //Set cout to display decimals to two places for money.

  cout << "Welcome to the Lab 7 University Bookstore Accounting Program"
       << endl;

  do
  {

    cout << endl << "Main Menu: " << endl
         << "1. Add an item" << endl
         << "2. Display Net Profit" << endl
         << "3. Quit Program" << endl
         << "Selection (1 - 3): ";
    cin >> iChoice;

    switch (iChoice)
    {
      case 1:
            fTotalProfit = fTotalProfit + AddItem();
            //Call the AddItem function and add its value to the total.

            break;

      case 2:
            DisplayProfit(fTotalProfit);
            //Call the DisplayProfit function with the running total.

            break;

      case 3:
            cout << "Exiting..." << endl;

            break;

      default:
             cout << endl << "Invalid menu choice." << endl;

             break;
      //Catch bad values and repeat the loop
    }

  }
  while (iChoice != 3);
  //Go as long as the user doesn't enter 3.

  return 0;
  //Exit normally.
}

float AddItem()
{
  int iProduct = 0;
  float fNetProfit = 0;
  //Declare variables for the user choice and profit for the item.

  cout << endl << "Add an item:" << endl
       << "1. Text book" << endl
       << "2. Clothing" << endl
       << "3. Misc." << endl
       << "Select item type (1 -3): ";
  cin >> iProduct;

  fNetProfit = GetRate(iProduct);
  //Call the GetRate function.to get the net profit for this item.

  return fNetProfit;
  //Give the item's net profit back to the calling function..
}

float GetRate(int iProduct)
{
  float fUnitCost = 0, fSalePrice = 0, fNetProfit;
  int iQuantity = 0;
  //Declare variables for the quantity, unit cost, price, and net profit.

  do
  {
    cout << endl << "Enter quantity: ";
    cin >> iQuantity;

    if (iQuantity <= 0)
      cout << "Invalid quantity." << endl;
  }
  while (iQuantity <= 0);
  //Catch negative values and ask again.

  do
  {
    cout << "Enter unit price: $";
    cin >> fUnitCost;

    if (fUnitCost <= 0);
      cout << "Invalid cost." << endl;
  }
  while (fUnitCost <= 0);
  //Catch negative values and ask again.

  switch (iProduct)
  {
    case 1:
          fSalePrice = ((iQuantity * fUnitCost) * 2.35);
          fNetProfit = fSalePrice - (iQuantity * fUnitCost);
          //Sale price is quantity * price * markup, profit is sale price
          // minus (quantity times price).

          cout << endl << "This item can gross: $" << fSalePrice << endl;
          cout << "With a 235% markup, this item can net: $" << fNetProfit
               << endl;
          //Output gross and net.

          break;
          //Break out of the switch. The other cases follow this pattern.

    case 2:
          fSalePrice = ((iQuantity * fUnitCost) * 1.5);
          fNetProfit = fSalePrice - (iQuantity * fUnitCost);

          cout << endl << "This item can gross: $" << fSalePrice << endl;
          cout << "With a 150% markup, this item can net: $" << fNetProfit
               << endl;

          break;

    case 3:
          fSalePrice = ((iQuantity * fUnitCost) * 1.75);
          fNetProfit = fSalePrice - (iQuantity * fUnitCost);

          cout << endl << "This item can gross: $" << fSalePrice << endl;
          cout << "With a 175% markup, this item can net: $" << fNetProfit
               << endl;

          break;
  }

  return fNetProfit;
  //Give the net profit back to the calling function.
}

void DisplayProfit(float fTotalProfit)
{
  cout << endl << "Your Net Profit is : $" << fTotalProfit << endl;
  //Display net profit.

  return;
  //Go back to calling function.
}

