// Marvyn Jesse De La Torre       CS1A       Chapter 7, pg. 444, #3
//
/****************************************************************************
 * CALCULATE SALSA SALES
 *
 * This program accepts a number of jars sold for five
 * types of salsa. It stores the sales in an array and displays the sales
 * for each type, the total sales, and the highest and lowest selling types.
 *
 * INPUT
 *   jarsSold       // Number of jars sold for each salsa type
 *
 * OUTPUT
 *   jarsSold       // Sales for each type of salsa
 *   totalSales     // Total number of jars sold
 *   highestType    // Salsa type with the most jars sold
 *   lowestType     // Salsa type with the fewest jars sold
 ****************************************************************************/

#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
using namespace std;

int main()
{
    const int SIZE = 5;

    // Store the five salsa names.
    string salsaNames[SIZE] =
    {
        "Mild", "Medium", "Sweet", "Hot", "Zesty"
    };

    // Store the number of jars sold for each type.
    int jarsSold[SIZE];
    int totalSales = 0;
    int highestIndex = 0;
    int lowestIndex = 0;

    // Get the sales amount for each salsa type.
    for (int i = 0; i < SIZE; i++)
    {
        cout << "Enter the number of jars sold for "
             << salsaNames[i] << " salsa: ";
        cin >> jarsSold[i];

        // Do not accept negative sales amounts or non-integer input.
        while (cin.fail() || jarsSold[i] < 0)
        {
            cout << "Invalid amount. Enter zero or a positive number: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cin >> jarsSold[i];
        }

        // Add the current sales amount to the total.
        totalSales += jarsSold[i];
    }

    // Find the highest and lowest selling salsa types.
    for (int i = 1; i < SIZE; i++)
    {
        if (jarsSold[i] > jarsSold[highestIndex])
        {
            highestIndex = i;
        }

        if (jarsSold[i] < jarsSold[lowestIndex])
        {
            lowestIndex = i;
        }
    }

    // Display the sales report.
    cout << "\nSalsa Sales Report\n";
    cout << "------------------\n";
    cout << left << setw(15) << "Salsa Type"
         << "Jars Sold\n";

    for (int i = 0; i < SIZE; i++)
    {
        cout << left << setw(15) << salsaNames[i]
             << jarsSold[i] << endl;
    }

    cout << "\nTotal jars sold: " << totalSales << endl;
    cout << "Highest selling salsa: " << salsaNames[highestIndex] << endl;
    cout << "Lowest selling salsa: " << salsaNames[lowestIndex] << endl;

    return 0;
}