beautypg.com

Example program - an inventory system – HP Integrity NonStop H-Series User Manual

Page 75

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 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 on_hand;
list 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;

This manual is related to the following products: