Top of document
©Copyright 1999 Rogue Wave Software

Example Program - An Inventory System

Obtaining the Sample Program

We will use a simple inventory management system to illustrate the use of several list operations. Assume a business, named WorldWideWidgetWorks, requires a software system to manage their supply of widgets. Widgets are simple devices, distinguished by different identification numbers:

class  Widget {
 public:
    Widget(int a = 0) : id(a) { }
    void operator = (const Widget& rhs) { id = rhs.id; }
    int id;
    friend ostream & operator << (ostream & out,const Widget & w)
       { return out << "Widget " << w.id; }
    friend bool operator == (const Widget& lhs, const Widget& rhs)
       { return lhs.id == rhs.id; }
    friend bool operator< (const Widget& lhs, const Widget& rhs)
       { return lhs.id < rhs.id; }
 };

The state of the inventory is represented by two lists. One list represents the stock of widgets on hand, while the second represents the type of widgets that customers have backordered. The first is a list of widgets, while the second is a list of widget identification types. To handle our inventory we have two commands; the first, order(), processes orders, while the second, receive(), processes the shipment of a new widget.

class inventory {
 public:
    void order (int wid);     // process order for widget type wid
    void receive (int wid);   // receive widget of type wid in shipment
 private:
    list<Widget> on_hand;
    list<int> on_order;
 };
 

When a new widget arrives in shipment, we compare the widget identification number with the list of widget types on backorder. We use find() to search the backorder list, immediately shipping the widget if necessary. Otherwise it is added to the stock on hand.

void inventory::receive (int wid)
 {
    cout << "Received shipment of widget type " << wid << endl;
    list<int>::iterator weneed = 
       find (on_order.begin(), on_order.end(), wid); 
    if (weneed != on_order.end()) 
    {
       cout << "Ship " << Widget(wid) 
            << " to fill back order" << endl;
       on_order.erase(weneed);
    }
    else
       on_hand.push_front(Widget(wid));
 }
 

When a customer orders a new widget, we scan the list of widgets in stock to determine if the order can be processed immediately. We can use the function find_if() to search the list. To do so we need a binary function that takes as its argument a widget and determines whether the widget matches the type requested. We can do this by taking a general binary widget-testing function, and binding the second argument to the specific widget type. To use the function bind2nd(), however, requires that the binary function be an instance of the class binary_function. The general widget-testing function is written as follows:

class WidgetTester : public binary_function<Widget, int, bool> {
 public:
    bool operator () (const Widget & wid, int testid) const
       { return wid.id == testid; }
 };
 

The widget order function is then written as follows:

void inventory::order (int wid)
 {
    cout << "Received order for widget type " << wid << endl;
    list<Widget>::iterator wehave = 
          find_if (on_hand.begin(), on_hand.end(), 
             bind2nd(WidgetTester(), wid));
    if (wehave != on_hand.end()) 
    {
       cout << "Ship " << *wehave << endl;
       on_hand.erase(wehave);
    }
    else 
    {
       cout << "Back order widget of type "  << wid  << endl;
       on_order.push_front(wid);
    }
 }
 

Top of document