home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / COLLECT1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  2.5 KB  |  108 lines

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