Monday, January 23, 2017

Programming Challenge 7.3 - Chips And Salsa

/* Chips And Salsa - This program lets a maker of chips and salsa keep
   track of sales for five different types of salsa:
 
      * Mild, Medium, Sweet, Hot, Zesty
    
   The program uses two parallel 5-element arrays:
 
      * An array of strings that holds the five salsa names
      * An array of integers that holds the number of jars
        sold during the past month for each salsa type.
 
   The salsa names are stored using an initialization list at the time
   the name array is created. The program prompts the user to enter the
   number of jars sold for each type. Once this sales data has been
   entered, the program produces a report that displays the sales for
   each salsa type, total sales, and the names of the highest and lowest
   selling products.
 
   Input Validation: No negative values for number of jars sold are
   accepted. */

#include "Utility.h"

/* Prototypes: Get jars sold, Get bestseller, Get non-seller, Get total sales, Show sales */
void getJarsSold(string[], int [], int);
int getBestseller(const int[], int, int &);
int getNonseller(const int[], int, int &);
int getTotalSales(const int[], int);
void showSales(const string[], const int [], int, int, int, int);

int main()
{
   /* Constants: Number of products, Salsa names, Best selling, Non-seller,
                 Total sales, Number of bestseller, Number of non-seller*/
   const int NUM_PRODUCTS = 5;
   string salsaNames[NUM_PRODUCTS] = { "California Mild", "New Orleans Medium",
                                       "Maine Sweet", "Texas Hot",
                                       "Alabama Zesty" };
   int jarsSold[NUM_PRODUCTS],
       bestSelling = 0,
       nonSeller = 0,
       numBestseller = 0,
       numNonseller = 0,
       totalSales = 0;

   /* Display: Header */
   cout << "\t\tCHABBA CHIPS & SALSA SALES REPORT\n\n";

   /* Call: getJarsSold, getBestseller, getNonseller, getTotalSales, showSales */
   getJarsSold(salsaNames, jarsSold, NUM_PRODUCTS);

   bestSelling = getBestseller(jarsSold, NUM_PRODUCTS, numBestseller);
   nonSeller = getNonseller(jarsSold, NUM_PRODUCTS, numNonseller);
   totalSales = getTotalSales(jarsSold, NUM_PRODUCTS);

   showSales(salsaNames, jarsSold, NUM_PRODUCTS,
             numBestseller, numNonseller, totalSales);
     
   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getJarsSold

   This function accepts two arrays as argument. The user is
   asked to enter the number of jars sold for each salsa
   category.
   ********************************************************** */

void getJarsSold(string salsaNames[], int jarsSold[], int numProducts)
{
   /* Get: The number of jars sold */
   for (int index = 0; index < numProducts; index++)
   {
      cout << "How many jars of " << salsaNames[index]
           << " were sold this month? ";
      cin >> jarsSold[index];

      /* Validate: Input */
      while (jarsSold[index] < 0)
      {
         cout << "\nInput Failure: The number of jars sold has "
              << "to be positive!\n"
              << "How many jars of " << salsaNames[index]
              << " were sold this month? ";
         cin >> jarsSold[index];

      }
   }
}

/* **********************************************************
   Definition: getBestseller

   This function accepts two arrays as arguments. It finds
   and returns the bestselling salsa product.
   ********************************************************** */

int getBestseller(const int jarsSold[], int numProducts,
                        int &numBestseller)
{
   /* Variable: Best selling (gets jarsSold[0] as a
                first value to compare the amount of
                jars sold to) */
   int bestSelling = jarsSold[0];

   for (int index = 0; index < numProducts; index++)
   {
      if (jarsSold[index] > bestSelling)
      {
         bestSelling = jarsSold[index];

         /* When a bestseller is found, numBestseller gets the
            arrays element subscript position */
         numBestseller = index;
      }
   }

   return bestSelling;
}

/* **********************************************************
   Definition: getNonseller

   This function accepts one array as argument. It finds
   and returns the least sold salsa product.
   ********************************************************** */

int getNonseller(const int jarsSold[], int numProducts,
                        int &numNonseller)
{
   /* Variable: Non-selling (gets jarsSold[0] as a
                first value to compare the amount of
                jars sold to) */
   int nonSeller = jarsSold[0];

   for (int index = 0; index < numProducts; index++)
   {
      if (jarsSold[index] < nonSeller)
      {
         nonSeller = jarsSold[index];

         /* When a non-seller is found, numNonseller gets the
         arrays element subscript position */
         numNonseller = index;
      }
   }

   return nonSeller;
}

/* **********************************************************
   Definition: getTotalSales

   This function accepts an array as argument. It determines
   the total number of jars sold, and returns this value.
   ********************************************************** */

int getTotalSales(const int jarsSold[], int numProducts)
{
   /* Variable: Sold total (accumulator) */
   int soldTotal = 0;

   /* Get: The total sales */
   for (int index = 0; index < numProducts; index++)
   {
      soldTotal += jarsSold[index];
   }

   return soldTotal;
}

/* **********************************************************
   Definition: showSales

   This function accepts two integer arrays as arguments. It
   displays the number of sold units for each salsa category,
   and the top and non-seller of each of the five products
   offered.
   ********************************************************** */

void showSales(const string salsaNames[], const int jarsSold[],
               int numProducts, int numBestselling,
               int numNonselling, int totalSales)
{
   /* Display: The formatted output */
   cout << "\n\t\tCHABBA CHIPS & SALSA SALES SUMMARY\n\n"
      << "Product Name" << "\t\t\t\t" << "Units Sold\n"
      << "------------------" << "\t\t\t" << "----------\n";

   for (int index = 0; index < numProducts; index++)
   {
      cout << setw(20) << left << salsaNames[index] << " "
           << setw(29) << right << jarsSold[index] << endl;

   }

   cout << "------------------" << "\t\t\t" << "----------\n"
        << "Total Number Sold " << setw(32) << right << totalSales;
   cout << "\n\nThis months bestseller was: " << salsaNames[numBestselling];
   cout << "\nThis months non-seller was: " << salsaNames[numNonselling];
}

Example Output: 



No comments:

Post a Comment