home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / PARCEL / PARCEL.H < prev    next >
Text File  |  1995-03-15  |  3KB  |  116 lines

  1. /*************************************************************************
  2.   IBM C/C++ Tools Version 3.00 - Collection Class Library
  3.  (C) Copyright IBM Corporation 1992 ,1995, Licensed Program-Property of
  4.  IBM.  All Rights Reserved.  US Government Users Restricted Rights - Use,
  5.  duplication or disclosure restricted by GSA ADP Schedule Contract with
  6.  IBM Corp.
  7.  *************************************************************************/
  8.  
  9. /*-------------------------------------------------------------*\
  10. |  parcel.h  -  Class Parcel and its parts for use with the     |
  11. |               example for Key Sorted Set and Heap.            |
  12. \*-------------------------------------------------------------*/
  13. #include <iostream.h>
  14.  
  15.                         // For definition of Boolean:
  16. #include <iglobals.h>
  17.                         // Class IString:
  18. #if defined (_SUN)
  19. #include <istring.h>
  20. #else
  21. #include <istring.hpp>
  22. #endif
  23.  
  24.  
  25. class PlaceTime {
  26.  
  27.   IString cty;
  28.   int   daynum;  // Keeping it simple:  January 9 is day 9
  29.  
  30. public:
  31.  
  32.   PlaceTime(IString acity, int aday) : cty(acity), daynum(aday) {}
  33.  
  34.   PlaceTime(IString acity) : cty(acity) {daynum = 0;}
  35.  
  36.   IString const& city() const {
  37.      return cty;
  38.   }
  39.  
  40.   int const& day() const {
  41.      return daynum;
  42.   }
  43.  
  44.   void operator=(PlaceTime const& pt) {
  45.       cty = pt.cty;
  46.       daynum = pt.daynum;
  47.   }
  48.  
  49.   IBoolean operator==(PlaceTime const& pt)  const {
  50.      return ( (cty == pt.cty)
  51.            && (daynum == pt.daynum) );
  52.   }
  53. };
  54.  
  55.  
  56. class Parcel {
  57.  
  58.   PlaceTime org, lstAr;
  59.   IString dst, id;
  60.  
  61. public:
  62.  
  63.   Parcel(IString orig,  IString dest, int day, IString ident)
  64.      :  org(orig, day),  lstAr(orig, day),  dst(dest), id(ident) {}
  65.  
  66.   void arrivedAt(IString const& acity, int const& day) {
  67.      PlaceTime nowAt(acity, day);
  68.                               // Only if not already there before
  69.      if (nowAt.city() != lstAr.city())
  70.         lstAr = nowAt;
  71.   }
  72.  
  73.   void wasDelivered(int const& day) {arrivedAt(dst, day);  }
  74.  
  75.   PlaceTime const& origin() const {
  76.      return org;
  77.   }
  78.  
  79.   IString const& destination() const {
  80.      return dst;
  81.   }
  82.  
  83.   PlaceTime const& lastArrival() const {
  84.      return lstAr;
  85.   }
  86.  
  87.   IString const& ID() const {
  88.      return id;
  89.   }
  90.  
  91.   friend ostream& operator<<(ostream& os, Parcel const& p) {
  92.     os << p.id << ": From " << p.org.city()
  93.        << "(day "  << p.org.day() << ") to " << p.dst;
  94.  
  95.     if (p.lstAr.city() != p.dst) {
  96.        os << endl << "            is at " << p.lstAr.city()
  97.           << "  since day " << p.lstAr.day() << ".";
  98.     }
  99.     else {
  100.        os << endl << "            was delivered on day "
  101.           << p.lstAr.day() << ".";
  102.     }
  103.     return os;
  104.   }
  105. };
  106.  
  107.                         // Key access:
  108.   inline  IString const& key( Parcel const&  p) {
  109.   return p.ID();
  110.   }
  111.                         // We need a compare function for the key.
  112.                         // Let's use the default provided for IString:
  113.   inline long compare(Parcel const& p1, Parcel const& p2) {
  114.      return compare(p1.ID(), p2.ID());
  115.   }
  116.