home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 15.ddi / TVDOCDEM.ZIP / TVG17B.CPP next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.0 KB  |  108 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   TVGUID17 Demo Source File                             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8.  
  9. #include <iostream.h>           // for cout, <<
  10. #include <iomanip.h>            // for setw
  11. #include <string.h>             // for strncmp
  12.  
  13. #define Uses_TNSCollection
  14. #include <tv.h>
  15.  
  16. struct TClient : public TObject
  17. {
  18.     // all members public by default
  19.     char account[7], name[35], phone[14];
  20.  
  21.     TClient( char *newAccount, char *newName, char *newPhone)
  22.         {
  23.         strcpy( account, newAccount );
  24.         strcpy( name, newName );
  25.         strcpy( phone, newPhone );
  26.         };
  27. };
  28.  
  29. class TClientCollection : public TNSCollection
  30. {
  31.  
  32. public:
  33.  
  34.     TClientCollection (ccIndex aLimit, ccIndex aDelta ) :
  35.         TNSCollection( aLimit, aDelta ) {}
  36.     virtual ccIndex insert( TClient *tc )
  37.         { return TNSCollection::insert( tc ); }
  38.         // overrides TNSCollection::insert( void *item )
  39.         // In a more complete application, you would also override
  40.         // at, indexOf, remove, free,... so that they all
  41.         // handle TClient pointers
  42.  
  43.     void printAll();
  44.     void searchArea( char *areaToFind );
  45.  
  46. };
  47.  
  48. static void print( void *c, void * )
  49. {
  50.     TClient *tc = (TClient *)c;
  51.     cout << setiosflags( ios::left )
  52.          << setw(10) << tc->account
  53.          << setw(20) << tc->name
  54.          << setw(16) << tc->phone
  55.          << endl;
  56. }
  57.  
  58. void TClientCollection::printAll()  // print info for all clients
  59. {
  60.     cout << endl << "Client List:" << endl;
  61.     forEach( &print, 0 ); // call print for each client
  62. }
  63.  
  64. Boolean areaMatch( void *c, void *ph )
  65. {
  66.     char *areaToFind = (char *)ph;
  67.     TClient *tc = (TClient *)c;
  68.     if( strncmp( areaToFind, tc->phone, 5 ) == 0 )
  69.         return True;
  70.     else
  71.         return False;
  72. }
  73.  
  74. void TClientCollection::searchArea( char *areaToFind )
  75. {
  76.     TClient *foundClient =
  77.         (TClient *)firstThat( &areaMatch, areaToFind );
  78.     if( !foundClient )
  79.         cout << "No client met the search requirement" << endl;
  80.     else
  81.         {
  82.         cout << "Found client:" << endl;
  83.         print( foundClient, 0 );
  84.         }
  85. }
  86.  
  87. int main()
  88. {
  89.  
  90.     TClientCollection clientList( 50, 10 );    // limit is 50, delta is 10
  91.  
  92.     clientList.insert( new TClient("90-167", "Smith, Zelda",
  93.                "(800) 555-1212" ));
  94.     clientList.insert( new TClient("90-160", "Johnson, Agatha",
  95.                "(302) 139-8913" ));
  96.     clientList.insert( new TClient("90-177", "Smitty, John",
  97.                "(406) 987-4321"));
  98.     clientList.insert( new TClient("91-100", "Anders, Smitty",
  99.                "(406) 111-2222"));
  100.  
  101.     clientList.printAll();
  102.     clientList.searchArea( "(406)" );
  103.     return 0;
  104.  
  105. };
  106.  
  107.  
  108.