Advertisement
SolahYana

Untitled

May 21st, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.27 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Panel1: TPanel;
  12.     Label1: TLabel;
  13.     Label2: TLabel;
  14.     Label3: TLabel;
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     Edit3: TEdit;
  18.     Button1: TButton;
  19.     Button2: TButton;
  20.     Button3: TButton;
  21.     ComboBox1: TComboBox;
  22.     Label4: TLabel;
  23.     Edit4: TEdit;
  24.     Button4: TButton;
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure Button1Click(Sender: TObject);
  27.     procedure Button2Click(Sender: TObject);
  28.     procedure Button3Click(Sender: TObject);
  29.     procedure ComboBox1Change(Sender: TObject);
  30.     procedure Button4Click(Sender: TObject);
  31.   private
  32.     Clients: array of TClient;
  33.     ClientCount: Integer;
  34.     FilteredClients: array of TClient;
  35.     FilteredCount: Integer;
  36.     procedure AddClient(const Name, Surname: string; RoomNumber: Integer);
  37.     procedure DeleteClient(Index: Integer);
  38.     procedure UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer);
  39.     procedure ApplyFilter;
  40.     procedure DisplayClients(Clients: array of TClient; Count: Integer);
  41.   public
  42.     { Public declarations }
  43.   end;
  44.  
  45. var
  46.   Form1: TForm1;
  47.  
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52. procedure TForm1.FormCreate(Sender: TObject);
  53. begin
  54.      // Èíèöèàëèçàöèÿ StringGrid
  55.   StringGrid1.ColCount := 4;
  56.   StringGrid1.RowCount := 2;
  57.   StringGrid1.Cells[0, 0] := 'ID';
  58.   StringGrid1.Cells[1, 0] := 'Èìÿ';
  59.   StringGrid1.Cells[2, 0] := 'Ôàìèëèÿ';
  60.   StringGrid1.Cells[3, 0] := 'Íîìåð êîìíàòû';
  61.  
  62.   // Èíèöèàëèçàöèÿ ComboBox äëÿ ôèëüòðàöèè
  63.   ComboBox1.Items.Add('Âñå');
  64.   ComboBox1.Items.Add('Èìÿ');
  65.   ComboBox1.Items.Add('Ôàìèëèÿ');
  66.   ComboBox1.ItemIndex := 0;
  67.  
  68.   ClientCount := 0;
  69.   FilteredCount := 0;
  70. end;
  71. // Äîáàâëåíèå êëèåíòà
  72. procedure TForm1.AddClient(const Name, Surname: string; RoomNumber: Integer);
  73. var
  74.   NewClient: TClient;
  75. begin
  76.   NewClient.Name := Name;
  77.   NewClient.Surname := Surname;
  78.   NewClient.RoomNumber := RoomNumber;
  79.  
  80.   SetLength(Clients, ClientCount + 1);
  81.   Clients[ClientCount] := NewClient;
  82.   Inc(ClientCount);
  83. end;
  84.  
  85. // Óäàëåíèå êëèåíòà
  86. procedure TForm1.DeleteClient(Index: Integer);
  87. var
  88.   i: Integer;
  89. begin
  90.   if (Index >= 0) and (Index < ClientCount) then
  91.   begin
  92.     for i := Index to ClientCount - 2 do
  93.       Clients[i] := Clients[i + 1];
  94.  
  95.     SetLength(Clients, ClientCount - 1);
  96.     Dec(ClientCount);
  97.   end;
  98. end;
  99. // ????????? ?????? ???????
  100. procedure TForm1.UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer);
  101. begin
  102.   if (Index >= 0) and (Index < ClientCount) then
  103.   begin
  104.     Clients[Index].Name := Name;
  105.     Clients[Index].Surname := Surname;
  106.     Clients[Index].RoomNumber := RoomNumber;
  107.   end;
  108. end;
  109. // ?????????? ???????
  110. procedure TForm1.ApplyFilter;
  111. var
  112.   i: Integer;
  113. begin
  114.   FilteredCount := 0;
  115.   SetLength(FilteredClients, 0);
  116.  
  117.   case ComboBox1.ItemIndex of
  118.     0: // ???
  119.       begin
  120.         FilteredClients := Copy(Clients, 0, ClientCount);
  121.         FilteredCount := ClientCount;
  122.       end;
  123.     1: // ???
  124.       begin
  125.         for i := 0 to ClientCount - 1 do
  126.           if Clients[i].Name = Edit4.Text then
  127.           begin
  128.             SetLength(FilteredClients, FilteredCount + 1);
  129.             FilteredClients[FilteredCount] := Clients[i];
  130.             Inc(FilteredCount);
  131.           end;
  132.       end;
  133.     2: // ???????
  134.       begin
  135.         for i := 0 to ClientCount - 1 do
  136.           if Clients[i].Surname = Edit4.Text then
  137.           begin
  138.             SetLength(FilteredClients, FilteredCount + 1);
  139.             FilteredClients[FilteredCount] := Clients[i];
  140.             Inc(FilteredCount);
  141.           end;
  142.       end;
  143.   end;
  144. end;
  145. // ??????????? ???????? ? StringGrid
  146. procedure TForm1.DisplayClients(Clients: array of TClient; Count: Integer);
  147. var
  148.   i: Integer;
  149. begin
  150.   StringGrid1.RowCount := Count + 1;
  151.   for i := 0 to Count - 1 do
  152.   begin
  153.     StringGrid1.Cells[0, i + 1] := IntToStr(i + 1);
  154.     StringGrid1.Cells[1, i + 1] := Clients[i].Name;
  155.     StringGrid1.Cells[2, i + 1] := Clients[i].Surname;
  156.     StringGrid1.Cells[3, i + 1] := IntToStr(Clients[i].RoomNumber);
  157.   end;
  158. end;
  159.  
  160. procedure TForm1.Button1Click(Sender: TObject);
  161. begin
  162.     AddClient(Edit1.Text, Edit2.Text, StrToInt(Edit3.Text));
  163.   ApplyFilter;
  164.   DisplayClients(FilteredClients, FilteredCount);
  165. end;
  166.  
  167. procedure TForm1.Button2Click(Sender: TObject);
  168. begin
  169.   if StringGrid1.Row > 1 then
  170.   begin
  171.     DeleteClient(StringGrid1.Row - 2);
  172.     ApplyFilter;
  173.     DisplayClients(FilteredClients, FilteredCount);
  174.   end;
  175. end;
  176.  
  177. procedure TForm1.Button3Click(Sender: TObject);
  178. begin
  179.   if StringGrid1.Row > 1 then
  180.   begin
  181.     UpdateClient(StringGrid1.Row - 2, Edit1.Text, Edit2.Text, StrToInt(Edit3.Text));
  182.     ApplyFilter;
  183.     DisplayClients(FilteredClients, FilteredCount);
  184.   end;
  185. end;
  186.  
  187. procedure TForm1.ComboBox1Change(Sender: TObject);
  188. begin
  189.    ApplyFilter;
  190.   DisplayClients(FilteredClients, FilteredCount);
  191. end;
  192.  
  193. procedure TForm1.Button4Click(Sender: TObject);
  194. begin
  195.    ComboBox1.ItemIndex := 0;
  196.   Edit4.Text := '';
  197.   ApplyFilter;
  198.   DisplayClients(FilteredClients, FilteredCount);
  199. end;
  200.  
  201. end.
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement