1  #include <iostream>
  2  #include <list>
  3  
  4  using namespace std;
  5  
  6  /**
  7     Describes a product by identification number.
  8  */
  9  class Product
 10  {
 11  public:
 12     Product(int id = 0);
 13     /**
 14        Gets the product identification number
 15        @return the identification number
 16     */
 17     int get_id() const;
 18  private:
 19     int identification_number;
 20  };
 21  
 22  inline int Product::get_id() const
 23  {
 24     return identification_number;
 25  }
 26  
 27  inline ostream& operator<<(ostream& out, const Product& p)
 28  {
 29     out << "Product " << p.get_id();
 30     return out;
 31  }
 32  
 33  inline Product::Product(int id)
 34  {
 35     identification_number = id;
 36  }
 37  
 38  /**
 39     Maintains an inventory list of products on hand and back orders.
 40  */
 41  class Inventory
 42  {
 43  public:
 44     /**
 45        Processes an order for a product with a given number.
 46        @param pid product id number
 47     */
 48     void order(int pid);
 49  
 50     /**
 51        Processes receipt of shipment for a product.
 52        @param newp new product
 53     */
 54     void receive(const Product& newp);
 55  private:
 56     list<Product> on_hand;
 57     list<int> on_order;
 58  };
 59  
 60  void Inventory::order(int pid)
 61  {
 62     cout << "Received order for product " << pid << "\n";
 63     list<Product>::iterator we_have = on_hand.begin();
 64     while (we_have != on_hand.end()) 
 65     {
 66        if (we_have->get_id() == pid)
 67        {
 68           // Have it, ship
 69           cout << "Ship " << *we_have << "\n";
 70           on_hand.erase(we_have);
 71           return;
 72        }
 73        ++we_have;
 74     }
 75     // Don't have it, back order
 76     cout << "Back order product " << pid << "\n";
 77     on_order.push_back(pid);
 78  }
 79  
 80  void Inventory::receive(const Product& newp)
 81  {
 82     cout << "Received shipment of product " << newp << "\n";
 83     list<int>::iterator we_need = 
 84        find(on_order.begin(), on_order.end(), newp.get_id());
 85     if (we_need != on_order.end())
 86     {
 87        cout << "Ship " << newp << " to fill back order\n";
 88        on_order.erase(we_need);
 89     }
 90     else
 91        on_hand.push_front(newp);
 92  }
 93  
 94  int main()
 95  {
 96     Inventory inv;
 97     inv.receive(Product(37));
 98     inv.receive(Product(14));
 99     inv.order(37);
100     inv.order(37);
101     inv.receive(Product(21));
102     inv.order(14);
103     inv.receive(Product(37));
104     return 0;
105  }