Thursday, June 15, 2017

Programming Challenge 11.14 - Inventory Bins

Include File: UtilityCls.h


/* Inventory Bins - This program simulates inventory bins in a warehouse.
    Each bin holds a number of the same type of parts. The program uses a
    structure that keeps the following data:
   
        * Description of the part kept in the bin
        * Number of parts in the bin

    The program has an array of 10 bins, initialized with the following
    data:

        * Part Description                Number of Parts in the Bin
          --------------------                 ---------------------------------
          Valve                                                                          10
          Bearing                                                                         5
          Bushing                                                                      15
          Coupling                                                                     21
          Flange                                                                           7
          Gear                                                                              5
          Gear Housing                                                                5
          Vacuum Gripper                                                          25
          Cable                                                                           18
          Rod                                                                              12

    The program has the following functions:

        * AddParts:            A function that increases a specific bin's part
                                count by a specified number.
        * RemoveParts:        A function that decreases a specific bin's part
                                count by a specified number.

    When the program runs, it repeats a loop that performs the following
    steps: The user sees a list of what each bin holds and how many parts
    are in each bin. The user can choose to either quit the program or
    select a bin. When a bin is selected, the user can either add parts to
    it or remove parts from it. The loop then repeats, showing the updated
    bin data on the screen.

    Input validation: No bin can hold more than 30 parts, so the user is
    not allowed to add more than a bin can hold. Also, no negative values
    for the number of parts being added or removed are allowed. */

#include "UtilityCls.h"

struct InventoryBin
{
    string partDesc;        /* Inventory bin part description         */
    int    numParts;        /* Number of parts in the invetory bins */

    InventoryBin(string pD = "N/A", int nP = 30)
    {
        partDesc = pD;
        numParts = nP;
    }

    ~InventoryBin()
    {
    }
};

enum options
{
    ADD_PARTS = 1, REMOVE_PARTS = 2, QUIT = 3
};

void menu(InventoryBin[], const int);
void displayBins(InventoryBin [], const int);
int  getBinID(const int);
void addParts(InventoryBin [], const int, const int, const int);
void removeParts(InventoryBin [], const int, const int, const int);

int main()
{
    const int BINS = 10;


    InventoryBin invBins[BINS] = { { "Valve",               10 },    { "Bearing",           5 },
                                                          { "Bushing",           15 },   { "Coupling",         21 },
                                                          { "Flange",          7 },         { "Gear",            5 },
                                                          { "Gear Housing",    5 },    { "Vacuum Gripper", 25 },
                                                          { "Cable",          18 },         { "Rod",            12 } };

    menu(invBins, BINS);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: menu

    This function accepts an array of structures as argument.
    It acts as a menu from which all the other functions are
    called.
   ********************************************************** */

void menu(InventoryBin invBins[], const int BINS)
{
    const int MAX_PARTS = 30;
    const int MIN_PARTS = 0;
    int selection = 0;
    int binID = 0;

    displayBins(invBins, BINS);

    do
    {
   
      cout << "\n\tSelect [1] to add items\n"
            << "\tSelect [2] to remove items\n"
            << "\tSelect [3] to quit\n\n"
            << "\tSelect: ";
      cin >> selection;

      while (selection < ADD_PARTS || selection > QUIT)
      {
          cout << "\tSelect: ";
          cin >> selection;
      }

      switch (selection)
      {
          case ADD_PARTS:
          {
              binID = getBinID(BINS);

              if (invBins[binID].numParts != MAX_PARTS)
              {
                  addParts(invBins, binID, MAX_PARTS, BINS);
              }
             clearScreen();
            displayBins(invBins, BINS);
          } break;

          case REMOVE_PARTS:
          {
                binID = getBinID(BINS);

                if (invBins[binID].numParts != MIN_PARTS)
                {
                    removeParts(invBins, binID, MIN_PARTS, BINS);
                }
                clearScreen();
                displayBins(invBins, BINS);
          } break;

          case QUIT:
          {
              cout << "\n\tMARAVILHOSO Warehouse - 'Experience is the mother of wisdom'";
          }
      }
    } while (selection != QUIT);
}

/* **********************************************************
   Definition: displayBins

    This function accepts an array of structures as argument.
    It displays the contents of the array of structures.
   ********************************************************** */

void displayBins(InventoryBin invBins[], const int BINS)
{
    cout << "\n\t\t    MARAVILHOSO Warehouse - Inventory\n\n\n"
         << "\tBin\t    Part Description"
          << setw(31) << right << "Number of parts\n\n";

    for (int index = 0; index < BINS; index++)
    {
        cout << "\t";
        cout << setw(12) << left << (index + 1)
              << setw(15) << left  << invBins[index].partDesc
              << setw(30) << right << invBins[index].numParts << "\n";
    }
}

/* **********************************************************
   Definition: getBinID

    This function asks the user to enter the bin number he or
    she wishes to change. This number is returned from the
    function.
   ********************************************************** */

int getBinID(const int BINS)
{
    int binID = 0;

    cout << "\n\tEnter Bin Number: ";
    cin >> binID;

    while (binID < 1 || binID > BINS)
    {
        cout << "\n\tEnter Bin Number: ";
        cin >> binID;
    }

    return binID -= 1;
}

/* **********************************************************
   Definition: addParts

    This function accepts an array of structures as argument.
    It allows the user to enter the number of inventory parts
    he or she wishes to add to a bin previously selected. The
    user is only able to enter numbers as long as the number
    of items is not greater than the maximum number of parts
    a bin can hold. He or she is also unable to enter negative
    numbers for items to add.
   ********************************************************** */

void addParts(InventoryBin invBins[], const int selection,
                  const int MAX_PARTS, const int BINS)
{
    int add = 0;

    cout << "\n\tEnter number of parts to add: ";
    cin >> add;

    while (add < 0)
    {
        cout << "\tEnter number of parts to add: ";
        cin >> add;
    }

    while (invBins[selection].numParts + add > MAX_PARTS)
    {
        cout << "\n\tMaximum Part Number 30 items\n\n"
              << "\tEnter number of parts: ";
        cin >> add;
    }

    if (invBins[selection].numParts + add <= MAX_PARTS)
    {
        invBins[selection].numParts += add;
    }
}

/* **********************************************************
   Definition: removeParts

    This function accepts an array of structures as argument.
    It allows the user to enter the number of inventory parts
    he or she wishes to remove from a bin previously selected.
    The user is only able to enter numbers as long as the
    number of items is not less than 0. He or she is also
    unable to enter negative numbers for items to remove.
   ********************************************************** */

void removeParts(InventoryBin invBins[], const int selection,
                      const int MIN_PARTS, const int BINS)
{
   int remove = 0;

    cout << "\n\tEnter number of parts to remove: ";
    cin >> remove;

    while (remove < 0)
    {
        cout << "\tEnter number of parts to remove: ";
        cin >> remove;
    }

    while (invBins[selection].numParts - remove < MIN_PARTS)
    {
        cout << "\n\tToo many parts selected\n\n"
              << "\tEnter number of parts to remove: ";
        cin >> remove;
    }

    if (invBins[selection].numParts - remove >= MIN_PARTS)
    {
        invBins[selection].numParts -= remove;
    }
}

Example Output:








No comments:

Post a Comment