Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, Grids, ExtCtrls;
- type
- TClient = record
- Name: string;
- Surname: string;
- RoomNumber: Integer;
- end;
- TForm1 = class(TForm)
- StringGrid1: TStringGrid;
- Panel1: TPanel;
- Label1: TLabel;
- Label2: TLabel;
- Label3: TLabel;
- Edit1: TEdit;
- Edit2: TEdit;
- Edit3: TEdit;
- Button1: TButton;
- Button2: TButton;
- Button3: TButton;
- ComboBox1: TComboBox;
- Label4: TLabel;
- Edit4: TEdit;
- Button4: TButton;
- procedure FormCreate(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- procedure ComboBox1Change(Sender: TObject);
- procedure Button4Click(Sender: TObject);
- private
- Clients: array of TClient;
- ClientCount: Integer;
- FilteredClients: array of TClient;
- FilteredCount: Integer;
- procedure AddClient(const Name, Surname: string; RoomNumber: Integer);
- procedure DeleteClient(Index: Integer);
- procedure UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer);
- procedure ApplyFilter;
- procedure DisplayClients(Clients: array of TClient; Count: Integer); // Исправлено: &Count -> Count
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- // Инициализация StringGrid
- StringGrid1.ColCount := 4;
- StringGrid1.RowCount := 2;
- StringGrid1.Cells[0, 0] := 'ID';
- StringGrid1.Cells[1, 0] := 'Имя';
- StringGrid1.Cells[2, 0] := 'Фамилия';
- StringGrid1.Cells[3, 0] := 'Номер комнаты';
- // Инициализация ComboBox для фильтрации
- ComboBox1.Items.Add('Все');
- ComboBox1.Items.Add('Имя');
- ComboBox1.Items.Add('Фамилия');
- ComboBox1.ItemIndex := 0;
- ClientCount := 0;
- FilteredCount := 0;
- end;
- // Добавление клиента
- procedure TForm1.AddClient(const Name, Surname: string; RoomNumber: Integer);
- var
- NewClient: TClient;
- begin
- NewClient.Name := Name;
- NewClient.Surname := Surname;
- NewClient.RoomNumber := RoomNumber;
- SetLength(Clients, ClientCount + 1);
- Clients[ClientCount] := NewClient;
- Inc(ClientCount);
- end;
- // Удаление клиента
- procedure TForm1.DeleteClient(Index: Integer);
- var
- i: Integer;
- begin
- if (Index gt;= 0) and (Index lt; ClientCount) then
- begin
- for i := Index to ClientCount - 2 do
- Clients[i] := Clients[i + 1];
- SetLength(Clients, ClientCount - 1);
- Dec(ClientCount);
- end;
- end;
- // Изменение данных клиента
- procedure TForm1.UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer);
- begin
- if (Index gt;= 0) and (Index lt; ClientCount) then
- begin
- Clients[Index].Name := Name;
- Clients[Index].Surname := Surname;
- Clients[Index].RoomNumber := RoomNumber;
- end;
- end;
- // Применение фильтра
- procedure TForm1.ApplyFilter;
- var
- i: Integer;
- begin
- FilteredCount := 0;
- SetLength(FilteredClients, 0);
- case ComboBox1.ItemIndex of
- 0: // Все
- begin
- FilteredClients := Copy(Clients, 0, ClientCount);
- FilteredCount := ClientCount;
- end;
- 1: // Имя
- begin
- for i := 0 to ClientCount - 1 do
- if Clients[i].Name = Edit4.Text then
- begin
- SetLength(FilteredClients, FilteredCount + 1);
- FilteredClients[FilteredCount] := Clients[i];
- Inc(FilteredCount);
- end;
- end;
- 2: // Фамилия
- begin
- for i := 0 to ClientCount - 1 do
- if Clients[i].Surname = Edit4.Text then
- begin
- SetLength(FilteredClients, FilteredCount + 1);
- FilteredClients[FilteredCount] := Clients[i];
- Inc(FilteredCount);
- end;
- end;
- end;
- end;
- // Отображение клиентов в StringGrid
- procedure TForm1.DisplayClients(Clients: array of TClient; Count: Integer); // Исправлено: &Count -> Count
- var
- i: Integer;
- begin
- StringGrid1.RowCount := Count + 1;
- for i := 0 to Count - 1 do
- begin
- StringGrid1.Cells[0, i + 1] := IntToStr(i + 1);
- StringGrid1.Cells[1, i + 1] := Clients[i].Name;
- StringGrid1.Cells[2, i + 1] := Clients[i].Surname;
- StringGrid1.Cells[3, i + 1] := IntToStr(Clients[i].RoomNumber);
- end;
- end;
- // Обработчики событий
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- AddClient(Edit1.Text, Edit2.Text, StrToInt(Edit3.Text));
- ApplyFilter;
- DisplayClients(FilteredClients, FilteredCount);
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- if StringGrid1.Row gt; 1 then
- begin
- DeleteClient(StringGrid1.Row - 2);
- ApplyFilter;
- DisplayClients(FilteredClients, FilteredCount);
- end;
- end;
- procedure TForm1.Button3Click(Sender: TObject);
- begin
- if StringGrid1.Row gt; 1 then
- begin
- UpdateClient(StringGrid1.Row - 2, Edit1.Text, Edit2.Text, StrToInt(Edit3.Text));
- ApplyFilter;
- DisplayClients(FilteredClients, FilteredCount);
- end;
- end;
- procedure TForm1.ComboBox1Change(Sender: TObject);
- begin
- ApplyFilter;
- DisplayClients(FilteredClients, FilteredCount);
- end;
- procedure TForm1.Button4Click(Sender: TObject);
- begin
- ComboBox1.ItemIndex := 0;
- Edit4.Text := '';
- ApplyFilter;
- DisplayClients(FilteredClients, FilteredCount);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement