home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / docdemos / collect1.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  3KB  |  109 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program                                 }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. program Collect1;
  10.  
  11. uses WObjects, WinCrt, Strings;
  12.  
  13. type
  14.   PClient = ^TClient;
  15.   TClient = object(TObject)
  16.     Account, Name, Phone: PChar;
  17.     constructor Init(NewAccount, NewName, NewPhone: PChar);
  18.     destructor Done; virtual;
  19.     procedure Print; virtual;
  20.   end;
  21.  
  22. { TClient }
  23. constructor TClient.Init(NewAccount, NewName, NewPhone: PChar);
  24. begin
  25.   Account := StrNew(NewAccount);
  26.   Name := StrNew(NewName);
  27.   Phone := StrNew(NewPhone);
  28. end;
  29.  
  30. destructor TClient.Done;
  31. begin
  32.   StrDispose(Account);
  33.   StrDispose(Name);
  34.   StrDispose(Phone);
  35. end;
  36.  
  37. procedure TClient.Print;
  38. begin
  39.   Writeln('  ',
  40.     Account, '':10 - StrLen(Account),
  41.     Name, '':20 - StrLen(Name),
  42.     Phone, '':16 - StrLen(Phone));
  43. end;
  44.  
  45. { Use ForEach iterator to display client information }
  46.  
  47. procedure PrintAll(C: PCollection);
  48.  
  49. procedure CallPrint(P : PClient); far;
  50. begin
  51.   P^.Print;                   { Call Print method }
  52. end;
  53.  
  54. begin { Print }
  55.   Writeln;
  56.   Writeln;
  57.   Writeln('Client list:');
  58.   C^.ForEach(@CallPrint);     { Print each client }
  59. end;
  60.  
  61. { Use FirstThat iterator to search non-key field }
  62.  
  63. procedure SearchPhone(C: PCollection; PhoneToFind: PChar);
  64.  
  65. function PhoneMatch(Client: PClient): Boolean; far;
  66. begin
  67.   PhoneMatch := StrPos(Client^.Phone, PhoneToFind) <> nil;
  68. end;
  69.  
  70. var
  71.   FoundClient: PClient;
  72.  
  73. begin { SearchPhone }
  74.   Writeln;
  75.   FoundClient := C^.FirstThat(@PhoneMatch);
  76.   if FoundClient = nil then
  77.     Writeln('No client met the search requirement')
  78.   else
  79.   begin
  80.     Writeln('Found client:');
  81.     FoundClient^.Print;
  82.   end;
  83. end;
  84.  
  85. var
  86.   ClientList: PCollection;
  87.  
  88. begin
  89.   ClientList := New(PCollection, Init(10, 5));
  90.  
  91.   { Build collection }
  92.   with ClientList^ do
  93.   begin
  94.     Insert(New(PClient, Init('91-100', 'Anders, Smitty', '(406) 111-2222')));
  95.     Insert(New(PClient, Init('90-167', 'Smith, Zelda', '(800) 555-1212')));
  96.     Insert(New(PClient, Init('90-177', 'Smitty, John', '(406) 987-4321')));
  97.     Insert(New(PClient, Init('90-160', 'Johnson, Agatha', '(302) 139-8913')));
  98.   end;
  99.  
  100.   { Use ForEach iterator to print all }
  101.   PrintAll(ClientList);
  102.  
  103.   { Use FirstThat iterator to find match with search pattern }
  104.   SearchPhone(ClientList, '(406)');
  105.  
  106.   { Clean up }
  107.   Dispose(ClientList, Done);
  108. end.
  109.