Advertisement
SolahYana

Untitled

May 22nd, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.37 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, ComCtrls, StdCtrls, ExtCtrls, Grids;
  8.  
  9. type
  10.   TClient = record
  11.     Name: string;
  12.     Surname: string;
  13.     RoomNumber: Integer;
  14.     ArrivalDate: TDateTime; // Дата заезда
  15.     DepartureDate: TDateTime; // Дата выезда
  16.   end;
  17.   TForm1 = class(TForm)
  18.     StringGrid1: TStringGrid;
  19.     Panel1: TPanel;
  20.     Label1: TLabel;
  21.     Label2: TLabel;
  22.     Label3: TLabel;
  23.     Label4: TLabel;
  24.     Label5: TLabel;
  25.     Edit1: TEdit;
  26.     Edit2: TEdit;
  27.     Edit3: TEdit;
  28.     Edit4: TEdit;
  29.     Edit5: TEdit;
  30.     Button1: TButton;
  31.     Button2: TButton;
  32.     Button3: TButton;
  33.     ComboBox1: TComboBox;
  34.     Label6: TLabel;
  35.     Edit6: TEdit;
  36.     Button4: TButton;
  37.     DateTimePicker1: TDateTimePicker;
  38.     Label7: TLabel;
  39.     DateTimePicker2: TDateTimePicker;
  40.     Label8: TLabel;
  41.     procedure FormCreate(Sender: TObject);
  42.     procedure Button1Click(Sender: TObject);
  43.     procedure Button2Click(Sender: TObject);
  44.     procedure Button3Click(Sender: TObject);
  45.     procedure ComboBox1Change(Sender: TObject);
  46.     procedure Button4Click(Sender: TObject);
  47.   private
  48.     Clients: array of TClient;
  49.     ClientCount: Integer;
  50.     FilteredClients: array of TClient;
  51.     FilteredCount: Integer;
  52.     procedure AddClient(const Name, Surname: string; RoomNumber: Integer; ArrivalDate, DepartureDate: TDateTime);
  53.     procedure DeleteClient(Index: Integer);
  54.     procedure UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer; ArrivalDate, DepartureDate: TDateTime);
  55.     procedure ApplyFilter;
  56.     procedure DisplayClients(Clients: array of TClient; Count: Integer);
  57.   public
  58.     { Public declarations }
  59.   end;
  60.  
  61. var
  62.   Form1: TForm1;
  63.  
  64. implementation
  65.  
  66. {$R *.dfm}
  67. procedure TForm1.FormCreate(Sender: TObject);
  68. begin
  69.   // Инициализация StringGrid
  70.   StringGrid1.ColCount := 7;
  71.   StringGrid1.RowCount := 2;
  72.   StringGrid1.Cells[0, 0] := 'ID';
  73.   StringGrid1.Cells[1, 0] := 'Имя';
  74.   StringGrid1.Cells[2, 0] := 'Фамилия';
  75.   StringGrid1.Cells[3, 0] := 'Номер комнаты';
  76.   StringGrid1.Cells[4, 0] := 'Дата заезда';
  77.   StringGrid1.Cells[5, 0] := 'Дата выезда';
  78.   StringGrid1.Cells[6, 0] := 'Дней'; // Добавляем столбец для количества дней
  79.  
  80.  
  81.  
  82.   // Инициализация ComboBox для фильтрации
  83.   ComboBox1.Items.Add('Все');
  84.   ComboBox1.Items.Add('Имя');
  85.   ComboBox1.Items.Add('Фамилия');
  86.   ComboBox1.Items.Add('Номер комнаты'); // Добавляем номер комнаты в фильтр
  87.   ComboBox1.ItemIndex := 0;
  88.  
  89.   ClientCount := 0;
  90.   FilteredCount := 0;
  91.   end;
  92. // ?????????? ???????
  93. procedure TForm1.AddClient(const Name, Surname: string; RoomNumber: Integer; ArrivalDate, DepartureDate: TDateTime);
  94. var
  95.   NewClient: TClient;
  96. begin
  97.   NewClient.Name := Name;
  98.   NewClient.Surname := Surname;
  99.   NewClient.RoomNumber := RoomNumber;
  100.   NewClient.ArrivalDate := ArrivalDate;
  101.   NewClient.DepartureDate := DepartureDate;
  102.  
  103.   SetLength(Clients, ClientCount + 1);
  104.   Clients[ClientCount] := NewClient;
  105.   Inc(ClientCount);
  106. end;
  107.  
  108. // ???????? ???????
  109. procedure TForm1.DeleteClient(Index: Integer);
  110. var
  111.   i: Integer;
  112. begin
  113.   if (Index >= 0) and (Index <= ClientCount - 1) then
  114.   begin
  115.     for i := Index to ClientCount - 2 do
  116.       Clients[i] := Clients[i + 1];
  117.  
  118.     SetLength(Clients, ClientCount - 1);
  119.     Dec(ClientCount);
  120.   end;
  121. end;
  122.  
  123. // ????????? ?????? ???????
  124. procedure TForm1.UpdateClient(Index: Integer; const Name, Surname: string; RoomNumber: Integer; ArrivalDate, DepartureDate: TDateTime);
  125. begin
  126.   if (Index >= 0) and (Index <= ClientCount - 1) then
  127.   begin
  128.     Clients[Index].Name := Name;
  129.     Clients[Index].Surname := Surname;
  130.     Clients[Index].RoomNumber := RoomNumber;
  131.     Clients[Index].ArrivalDate := ArrivalDate;
  132.     Clients[Index].DepartureDate := DepartureDate;
  133.   end;
  134. end;
  135.  
  136. // ?????????? ???????
  137. procedure TForm1.ApplyFilter;
  138. var
  139.   i: Integer;
  140.   Days: Integer; // ?????????? ????
  141. begin
  142.   FilteredCount := 0;
  143.   SetLength(FilteredClients, 0);
  144.  
  145.   case ComboBox1.ItemIndex of
  146.     1: // ???
  147.       begin
  148.         for i := 0 to ClientCount - 1 do
  149.           if Clients[i].Name = Edit6.Text then
  150.           begin
  151.              SetLength(FilteredClients, FilteredCount + 1);
  152.             FilteredClients[FilteredCount] := Clients[i];
  153.             Inc(FilteredCount);
  154.           end;
  155.       end;
  156.     2: // ???????
  157.       begin
  158.         for i := 0 to ClientCount - 1 do
  159.           if Clients[i].Surname = Edit6.Text then
  160.           begin
  161.             SetLength(FilteredClients, FilteredCount + 1);
  162.             FilteredClients[FilteredCount] := Clients[i];
  163.             Inc(FilteredCount);
  164.           end;
  165.       end;
  166.     3: // ????? ???????
  167.       begin
  168.         for i := 0 to ClientCount - 1 do
  169.           if Clients[i].RoomNumber = StrToInt(Edit6.Text) then
  170.           begin
  171.             SetLength(FilteredClients, FilteredCount + 1);
  172.             FilteredClients[FilteredCount] := Clients[i];
  173.             Inc(FilteredCount);
  174.           end;
  175.       end;
  176.     else
  177.       begin
  178.         // ???? ?????? ?? ??????, ?? ???????????? ??? ???????
  179.         SetLength(FilteredClients, ClientCount);
  180.         for i := 0 to ClientCount - 1 do // ???????? ???????? ???????
  181.           FilteredClients[i] := Clients[i];
  182.         FilteredCount := ClientCount;
  183.       end;
  184.   end;
  185. end;
  186.  
  187. // ??????????? ???????? ? StringGrid
  188. procedure TForm1.DisplayClients(Clients: array of TClient; Count: Integer);
  189. var
  190.   i: Integer;
  191.   Days: Integer; // ?????????? ????
  192. begin
  193.   StringGrid1.RowCount := Count + 1;
  194.   for i := 0 to Count - 1 do
  195.   begin
  196.     StringGrid1.Cells[0, i + 1] := IntToStr(i + 1);
  197.     StringGrid1.Cells[1, i + 1] := Clients[i].Name;
  198.     StringGrid1.Cells[2, i + 1] := Clients[i].Surname;
  199.     StringGrid1.Cells[3, i + 1] := IntToStr(Clients[i].RoomNumber);
  200.     StringGrid1.Cells[4, i + 1] := DateToStr(Clients[i].ArrivalDate); // ?????????? ???? ??????
  201.     StringGrid1.Cells[5, i + 1] := DateToStr(Clients[i].DepartureDate); // ?????????? ???? ??????
  202.  
  203.     // ????????? ?????????? ????
  204.     Days :=  Trunc(Clients[i].DepartureDate - Clients[i].ArrivalDate);
  205.     StringGrid1.Cells[6, i + 1] := IntToStr(Days);
  206.   end;
  207. end;
  208.  
  209. // ??????????? ???????
  210.  
  211. procedure TForm1.Button1Click(Sender: TObject);
  212. begin
  213.   AddClient(Edit1.Text, Edit2.Text, StrToInt(Edit3.Text), DateTimePicker1.Date, DateTimePicker2.Date);
  214.   ApplyFilter;
  215.   DisplayClients(FilteredClients, FilteredCount);
  216. end;
  217.  
  218. procedure TForm1.Button2Click(Sender: TObject);
  219. begin
  220.   if StringGrid1.Row > 1 then
  221.   begin
  222.     DeleteClient(StringGrid1.Row - 2);
  223.     ApplyFilter;
  224.     DisplayClients(FilteredClients, FilteredCount);
  225.   end;
  226. end;
  227.  
  228. procedure TForm1.Button3Click(Sender: TObject);
  229. begin
  230.   if StringGrid1.Row > 1 then
  231.   begin
  232.     UpdateClient(StringGrid1.Row - 2, Edit1.Text, Edit2.Text, StrToInt(Edit3.Text), DateTimePicker1.Date, DateTimePicker2.Date);
  233.     ApplyFilter;
  234.     DisplayClients(FilteredClients, FilteredCount);
  235.   end;
  236. end;
  237.  
  238. procedure TForm1.ComboBox1Change(Sender: TObject);
  239. begin
  240.   ApplyFilter;
  241.   DisplayClients(FilteredClients, FilteredCount);
  242. end;
  243.  
  244. procedure TForm1.Button4Click(Sender: TObject);
  245. begin
  246.   ComboBox1.ItemIndex := 0;
  247.   Edit6.Text := '';
  248.   ApplyFilter;
  249.   DisplayClients(FilteredClients, FilteredCount);
  250. end;
  251. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement