home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / parcel / parcel.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  3.7 KB  |  121 lines

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