Adding ReorderItems to the Inventory class

Now let's examine the details of the part of this inventory control program that calculates how much of each item has to be ordered to refill the stock. As I mentioned previously, I've chosen the imaginative name ReorderItems for the member function in the Inventory class that will perform this operation. The ReorderItems function is pretty simple. Its behavior can be described as follows:

'For each element in the StockItem Vec in the Inventory object, call its member function Reorder to generate an order if that StockItem object needs to be reordered.'

Of course, this algorithm is much simpler than we would need in the real world; however, it's realistic enough to be useful in illustrating important issues in program design and implementation.

Figure 9.4 shows the version of the inventory test program that uses the initial versions of those classes that we will build on in our inheritance exercise.

Figure 9.4. The StockItem test program for the base StockItem class (codeitmtst20.cpp)
#include <iostream>
#include <fstream>
#include "Vec.h"
#include "item20.h"
#include "invent20.h"
using namespace std;

int main()
{
  ifstream ShopInfo("shop20.in");
  ofstream ReorderInfo("shop20.reo");

  Inventory MyInventory;

  MyInventory.LoadInventory(ShopInfo);

  MyInventory.ReorderItems(ReorderInfo);

  return 0;
}

This shouldn't be too hard to follow. We start by creating an ifstream (i.e., an input stream that can be connected to a file) and an ofstream (i.e., an output stream that can be connected to a file). Then we create an inventory object called MyInventory, load items into it from the ifstream, and call the ReorderItems function to create the reorder report that is written to the ofstream. Finally, we return 0 to indicate successful completion.

Assuming that you've installed the software from the CD in the back of this book, you can try out this program. First, you have to compile it by following the compilation instructions on the CD. Then type itmtst20 to run the program. However, this program doesn't have much of a user interface so you might want to watch it operating under the debugger, by following the usual instructions for that method. When the program terminates, you can look at its output file, shop20.reo, to see what the reorder report looks like.

Now let's take a look at how the Inventory class does its work (Figure 9.5).[3]

[3] Note that the declarations of LoadInventory, StoreInventory, and ReorderItems refer to istreams and ostreams, not the file stream types. As I mentioned previously, this is legal because of the relationships among these types. I'll explain exactly why on page 609.

Figure 9.5. The implementation of the Inventory class (codeinvent20.cpp)
#include <iostream>
#include <fstream>
#include "Vec.h"
#include "item20.h"
#include "invent20.h"
using namespace std;

Inventory::Inventory()
: m_Stock (Vec<StockItem>(100)),
  m_StockCount(0)
{
}

short Inventory::LoadInventory(istream& is)
{
 short i;

 for (i = 0; i < 100; i ++)
   {
    is >> m_Stock[i];
    if (is.fail())
      break;
    }

  m_StockCount = i;
  return m_StockCount;
}

StockItem Inventory::FindItem(string UPC)
{
 short i;
 bool Found = false;

 for (i = 0; i < m_StockCount; i ++)
   {
   if (m_Stock[i].GetUPC() == UPC)
     {
     Found = true;
     break;
     }
    }

  if (Found)
    return m_Stock[i];

  return StockItem();
}

bool Inventory::UpdateItem(StockItem Item)
{
  string UPC = Item.GetUPC();

  short i;
  bool Found = false;

  for (i = 0; i < m_StockCount; i ++)
    {
    if (m_Stock[i].GetUPC() == UPC)
      {
      Found = true;
      break;
      }
    }
  if (Found)
    m_Stock[i] = Item;

  return Found;

}

void Inventory::StoreInventory(ostream& os)
{
  short i;

  for (i = 0; i < m_StockCount; i ++)
    os << m_Stock[i];
}

void Inventory::ReorderItems(ostream& os)
{
  short i;

  for (i = 0; i < m_StockCount; i ++)
    m_Stock[i].Reorder(os);
}

The ReorderItems function can hardly be much simpler. As you can see, it merely tells each StockItem element in the m_Stock Vec to execute its Reorder function. Now let's see what the latter function, whose full name is void StockItem::Reorder(ostream&), needs to do:

  1. Check to see if the current stock of that item is less than the desired minimum.

  2. If we are below the desired stock minimum, order the amount needed to bring us back to the stock minimum, unless that order amount is less than the minimum allowable quantity from the distributor. In the latter case, order the minimum allowable reorder quantity.

  3. If we are not below the desired stock minimum, do nothing.

To support this Reorder function, StockItem uses the data items m_MinimumStock and m_MinimumReorder to calculate the number of units of the current StockItem object that have to be reordered. Figure 9.6 shows the code for the Reorder function.

Figure 9.6. The Reorder function for the StockItem class (from codeitem20.cpp)
void StockItem::Reorder(ostream& os)
{
  short ActualReorderQuantity;

  if (m_InStock < m_MinimumStock)
    {
    ActualReorderQuantity = m_MinimumStock - m_InStock;
    if (m_MinimumReorder > ActualReorderQuantity)
      ActualReorderQuantity = m_MinimumReorder;
    os << "Reorder " << ActualReorderQuantity;
    os <<  " units of " << m_Name;
    os << " with UPC " << m_UPC;
    os << " from " << m_Distributor << endl;
    }
}

Here's the translation of this code:

  1. If the number of units in stock is less than the minimum number desired, we calculate the number needed to bring the inventory back to the minimum.

  2. However, the number we want to order may be less than the minimum we are allowed to order; the latter quantity is specified by the variable m_MinimumReorder.

  3. If the amount we need is less than m_MinimumReorder, we have to order the latter quantity

  4. Finally, we display the order for the item. Of course, if we already have enough units in stock we don't have to reorder anything, so we don't display anything.

Susan had a question about the implementation of this function:

Susan: So, are you ordering more than needed in some cases?

Steve: Yes, if that's the minimum number that can be ordered.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset