home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 4.ddi / DOCDEMOS.ZIP / TVGUID17.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  2.6 KB  |  110 lines

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