Advertisement
believe_me

Untitled

May 17th, 2022
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 14.00 KB | None | 0 0
  1. unit EditUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.Menus, Vcl.StdCtrls, ListUnit,
  8.   System.Actions, Vcl.ActnList, System.UITypes;
  9.  
  10. type
  11.   TEditForm = class(TForm)
  12.     SubscribersStringGrid: TStringGrid;
  13.     TelephoneEdit: TEdit;
  14.     MainMenu: TMainMenu;
  15.     AddButton: TButton;
  16.     EditButton: TButton;
  17.     DeleteButton: TButton;
  18.     InstructionMenu: TMenuItem;
  19.     TelephoneRButton: TRadioButton;
  20.     SortLabel: TLabel;
  21.     NameRButton: TRadioButton;
  22.     SurnameRButton: TRadioButton;
  23.     PatronymicRButton: TRadioButton;
  24.     CityRButton: TRadioButton;
  25.     NameLabel: TLabel;
  26.     SurnameLabel: TLabel;
  27.     NameEdit: TEdit;
  28.     SurnameEdit: TEdit;
  29.     PatronymicEdit: TEdit;
  30.     CItyEdit: TEdit;
  31.     ShowButton: TButton;
  32.     PatronymicLabel: TLabel;
  33.     CityLabel: TLabel;
  34.     TelephoneLabel: TLabel;
  35.     TaskLabel: TLabel;
  36.     ClearButton: TButton;
  37.     DateRButton: TRadioButton;
  38.     procedure FormShow(Sender: TObject);
  39.     procedure FormCreate(Sender: TObject);
  40.     procedure AddButtonClick(Sender: TObject);
  41.     procedure fillStringGrid(SubscribersStringGrid: TStringGrid; TelephoneEdit, SurnameEdit, NameEdit,
  42.                                 PatronymicEdit, CityEdit: TEdit);
  43.     procedure ShowButtonClick(Sender: TObject);
  44.     procedure clearStringGrid();
  45.     procedure DeleteButtonClick(Sender: TObject);
  46.     procedure RadioButtonCLick(Sender: TObject);
  47.     procedure ClearButtonClick(Sender: TObject);
  48.     function isCorrectTextData(CurrentEdit: TEdit): boolean;
  49.     function isCorrectNumberData(CurrentEdit: TEdit): boolean;
  50.     procedure DataEditKeyPress(Sender: TObject; var Key: Char);
  51.     procedure FormKeyPress(Sender: TObject; var Key: Char);
  52.     procedure TelephoneEditKeyPress(Sender: TObject; var Key: Char);
  53.     function areEditsCorrect(): boolean;
  54.     procedure EditButtonClick(Sender: TObject);
  55.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  56.     procedure InstructionMenuClick(Sender: TObject);
  57.     procedure buildStringGrid(SubscribersStringGrid: TStringGrid);
  58.     procedure setEditButtonsCondition();
  59.     procedure sortList();
  60.     procedure getRequiredSubscriber(var RequiredSubscriber: TRequiredSubscriber;
  61.                                     RequiredTelephoneNumber: string; RequiredSurname: string; RequiredName: string;
  62.                                             RequiredPatronymic: string; RequiredCity: string);
  63.   private
  64.     { Private declarations }
  65.   public
  66.     { Public declarations }
  67.   end;
  68.  
  69.  
  70. var
  71.   EditForm: TEditForm;
  72.   SubscriberList: TSubscriberList;
  73.   SubscriberIndex: TNumber;
  74.  
  75. implementation
  76. {$R *.dfm}
  77.  
  78. uses
  79.     ReadUnit, WriteUnit, AddUnit, CorrectUnit, StartUnit;
  80.  
  81. const
  82.     TEXT_TOP_OFFSET = 3;
  83.     BIG_LETTERS = ['A'..'Z'];
  84.     SMALL_LETTERS = ['a'..'z'];
  85.     DIGITS = ['0'..'9'];
  86.  
  87. var
  88.     CurrentRadioButton: integer = 0;
  89.     NumberArray: array of TNumber;
  90.  
  91. procedure TEditForm.AddButtonClick(Sender: TObject);
  92. begin
  93.     AddForm.Show;
  94.     EditForm.Enabled := false;
  95. end;
  96.  
  97. procedure TEditForm.FormClose(Sender: TObject; var Action: TCloseAction);
  98. begin
  99.     SubscriberList.deleteList;
  100.     clearStringGrid;
  101.     StartForm.enabled := true;
  102.     StartForm.show;
  103. end;
  104.  
  105. procedure TEditForm.setEditButtonsCondition();
  106. begin
  107.     if  SubscribersStringGrid.RowCount > 1 then
  108.     begin
  109.         DeleteButton.Enabled := true;
  110.         EditButton.Enabled := true;
  111.     end
  112.     else
  113.     begin
  114.         DeleteButton.Enabled := false;
  115.         EditButton.Enabled := false;
  116.     end;
  117. end;
  118.  
  119. procedure TEditForm.buildStringGrid(SubscribersStringGrid: TStringGrid);
  120. var
  121.     StringGridSize: integer;
  122. begin
  123.     SubscribersStringGrid.Cells[0, 0] := '  №';
  124.     SubscribersStringGrid.Cells[1, 0] := '   Telephone';
  125.     SubscribersStringGrid.Cells[2, 0] := '       Surname';
  126.     SubscribersStringGrid.Cells[3, 0] := '        Name';
  127.     SubscribersStringGrid.Cells[4, 0] := '       Patronymic';
  128.     SubscribersStringGrid.Cells[5, 0] := '           City';
  129.     SubscribersStringGrid.Cells[6, 0] := 'Days left';
  130.     StringGridSize := SubscribersStringGrid.width;
  131.     SubscribersStringGrid.ColWidths[0] := Round(StringGridSize / 14);
  132.     SubscribersStringGrid.ColWidths[1] := Round(StringGridSize / 7.5);
  133.     SubscribersStringGrid.ColWidths[2] := Round(StringGridSize / 5.1);
  134.     SubscribersStringGrid.ColWidths[3] := Round(StringGridSize / 6.6);
  135.     SubscribersStringGrid.ColWidths[4] := Round(StringGridSize / 5.1);
  136.     SubscribersStringGrid.ColWidths[5] := Round(StringGridSize / 6.5);
  137.     SubscribersStringGrid.ColWidths[6] := Round(StringGridSize / 13);
  138. end;
  139.  
  140. procedure TEditForm.FormCreate(Sender: TObject);
  141. begin
  142.     buildStringGrid(SubscribersStringGrid);
  143. end;
  144.  
  145. procedure TEditForm.FormKeyPress(Sender: TObject; var Key: Char);
  146. begin
  147.     if key = #27 then
  148.         EditForm.Close()
  149.     else
  150.         if Key = #13 then
  151.             ShowButtonClick(Sender);
  152. end;
  153.  
  154. procedure TEditForm.FormShow(Sender: TObject);
  155. begin
  156.     SubscriberList := TSubscriberList.NewSubscriberList();
  157.     EditButton.Enabled := false;
  158.     DeleteButton.Enabled := false;
  159.     clearStringGrid;
  160. end;
  161.  
  162. procedure TEditForm.InstructionMenuClick(Sender: TObject);
  163. begin
  164.     MessageDlg('This is the edit window. It includes next functions:' + #13#10 + '1. Searching by certain data;' + #13#10
  165.                             + '2. Sorting by all categories;' + #13#10 +
  166.                             '3. Adding new subscriber;' + #13#10 + '4. Edtining selected one;' +  #13#10 +
  167.                             '5. Deleting selected one.' + #13#10 + #13#10 +
  168.                             'Right input:' + #13#10 +
  169.                             'a) Telephone number field: first char - "+" or digit and next are digits; length = 13;'
  170.                             + #13#10 + 'b) Text fields: first char - big letter and next are small letters. Length:'
  171.                             + #13#10 + 'City -11, Surname - 14, Name - 11, Patronymic - 14.' + #13#10
  172.                             + #13#10 + 'Use "clear" to clear all fields and turn sort off;'
  173.                             + #13#10 + #13#10 + 'Press "ctrl + i" to open instruction;'
  174.                             + #13#10 + 'Press "enter" to show subscribers;'
  175.                             + #13#10 + 'Press "esc" to go back to start widnow.', MtInformation, [mbOk], 0);
  176. end;
  177.  
  178. procedure TEditForm.ShowButtonClick(Sender: TObject);
  179. begin
  180.     if areEditsCorrect() then
  181.     begin
  182.         clearStringGrid();
  183.         if not isFileEmpty() then
  184.         begin
  185.             fillStringGrid(SubscribersStringGrid, TelephoneEdit, SurnameEdit, NameEdit,
  186.                             PatronymicEdit, CityEdit);
  187.             setEditButtonsCondition();
  188.         end
  189.         else
  190.             MessageDlg('Subscriber file is empty.', mtError, [mbOk], 0, mbOk);
  191.     end
  192.     else
  193.     begin
  194.         MessageDlg('Wrong data.', mtError, [mbCancel], 0);
  195.         clearButtonClick(Sender);
  196.     end;
  197. end;
  198.  
  199. function TEditForm.areEditsCorrect(): boolean;
  200. begin
  201.     Result := (IsCorrectNumberData(TelephoneEdit) and IsCorrectTextData(SurnameEdit) and
  202.                IsCorrectTextData(NameEdit) and IsCorrectTextData(PatronymicEdit) and
  203.                IsCorrectTextData(CityEdit));
  204. end;
  205.  
  206. procedure TEditForm.DataEditKeyPress(Sender: TObject; var Key: Char);
  207. begin
  208.     if (Key <> #08) then
  209.     begin
  210.         if length((Sender as TEdit).text) = 0 then
  211.         begin
  212.             if not (Key in BIG_LETTERS) then
  213.                 Key := #0
  214.         end
  215.         else
  216.             if not(Key in SMALL_LETTERS) then
  217.                 Key := #0;
  218.     end;
  219. end;
  220.  
  221. procedure TEditForm.TelephoneEditKeyPress(Sender: TObject; var Key: Char);
  222. begin
  223.     if (Key <> #08) then
  224.     begin
  225.         if length((Sender as TEdit).text) = 0 then
  226.         begin
  227.             if ((not(Key in DIGITS)) and (Key <> '+')) then
  228.                 Key := #0
  229.         end
  230.         else
  231.             if not(Key in DIGITS) then
  232.                 Key := #0;
  233.     end;
  234. end;
  235.  
  236. procedure TEditForm.RadioButtonCLick(Sender: TObject);
  237. begin
  238.     CurrentRadioButton := (Sender as TRadioButton).tag;
  239. end;
  240.  
  241. procedure TEditForm.ClearButtonClick(Sender: TObject);
  242. begin
  243.     TelephoneEdit.text := '';
  244.     NameEdit.text := '';
  245.     SurnameEdit.text := '';
  246.     PatronymicEdit.text := '';
  247.     CityEdit.text := '';
  248.     TelephoneRButton.Checked := false;
  249.     SurnameRButton.Checked := false;
  250.     NameRButton.Checked := false;
  251.     PatronymicRButton.Checked := false;
  252.     CityRButton.Checked := false;
  253.     DateRButton.Checked := false;
  254.     CurrentRadioButton := 0;
  255.     SubscribersStringGrid.RowCount := 1;
  256.     EditButton.Enabled := false;
  257.     DeleteButton.Enabled := false;
  258. end;
  259.  
  260.  
  261. procedure TEditForm.sortList();
  262. begin
  263.     if not SubscriberList.IsEmpty then
  264.         case CurrentRadioButton of
  265.             0:
  266.                 ;
  267.             1:
  268.                 SubscriberList.sort(TSubscriberList.CompareByTelephoneNumber);
  269.             2:
  270.                 SubscriberList.sort(TSubscriberList.CompareBySurname);
  271.             3:
  272.                 SubscriberList.sort(TSubscriberList.CompareByName);
  273.             4:
  274.                 SubscriberList.sort(TSubscriberList.CompareByPatronymic);
  275.             5:
  276.                 SubscriberList.sort(TSubscriberList.CompareByCity);
  277.             6:
  278.                 SubscriberList.sort(TSubscriberList.CompareByDate);
  279.         end;
  280. end;
  281.  
  282. procedure TEditForm.fillStringGrid(SubscribersStringGrid: TStringGrid;
  283.                             TelephoneEdit, SurnameEdit, NameEdit,
  284.                                 PatronymicEdit, CityEdit: TEdit);
  285. var
  286.     i, j, ListLength: Integer;
  287.     PCurrentSubscriber: TSubscriberPointer;
  288.     RequiredSubscriber: TRequiredSubscriber;
  289. begin
  290.     i := 1;
  291.     j := 0;
  292.     SubscriberList.deleteList();
  293.     getRequiredSubscriber(RequiredSubscriber, TelephoneEdit.text, SurnameEdit.text, NameEdit.text,
  294.                                 PatronymicEdit.text, CityEdit.text);
  295.     readSubscribers(RequiredSubscriber);
  296.     sortList();
  297.     ListLength := SubscriberList.length();
  298.     setlength(NumberArray, ListLength);
  299.     PCurrentSubscriber := SubscriberList.getHeader()^.next;
  300.     while (PCurrentSubscriber <> nil) do
  301.     begin
  302.         SubscribersStringGrid.RowCount := SubscribersStringGrid.RowCount + 1;
  303.         SubscribersStringGrid.Cells[0, i] := intToStr(i);
  304.         SubscribersStringGrid.Cells[1, i] := PCurrentSubscriber^.telephoneNumber;
  305.         SubscribersStringGrid.Cells[2, i] := PCurrentSubscriber^.surname;
  306.         SubscribersStringGrid.Cells[3, i] := PCurrentSubscriber^.name;
  307.         SubscribersStringGrid.Cells[4, i] := PCurrentSubscriber^.patronymic;
  308.         SubscribersStringGrid.Cells[5, i] := PCurrentSubscriber^.city;
  309.         SubscribersStringGrid.Cells[6, i] := intToStr(PCurrentSubscriber^.numberOfDays);
  310.         NumberArray[j] := PCurrentSubscriber^.number;
  311.         Inc(i);
  312.         inc(j);
  313.         PCurrentSubscriber := PCurrentSubscriber^.next;
  314.     end;
  315. end;
  316.  
  317. procedure TEditForm.getRequiredSubscriber(var RequiredSubscriber: TRequiredSubscriber; RequiredTelephoneNumber: string; RequiredSurname: string;
  318.                                             RequiredName: string; RequiredPatronymic: string;
  319.                                             RequiredCity: string);
  320. begin
  321.     RequiredSubscriber.telephoneNumber := RequiredTelephoneNumber;
  322.     RequiredSubscriber.surname := RequiredSurname;
  323.     RequiredSubscriber.name := RequiredName;
  324.     RequiredSubscriber.patronymic := RequiredPatronymic;
  325.     RequiredSubscriber.city := RequiredCity;
  326. end;
  327.  
  328. procedure TEditForm.clearStringGrid();
  329. begin
  330.     SubscribersStringGrid.RowCount := 1;
  331.     SubscribersStringGrid.ColCount := 7;
  332. end;
  333.  
  334. procedure TEditForm.DeleteButtonClick(Sender: TObject);
  335. var
  336.     SubscriberNumber: integer;
  337. begin
  338.     SubscriberNumber := NumberArray[SubscribersStringGrid.Row - 1];
  339.     deleteSubscriber(SubscriberNumber);
  340.     ShowButtonClick(Sender);
  341. end;
  342.  
  343. procedure TEditForm.EditButtonClick(Sender: TObject);
  344. begin
  345.     SubscriberIndex := NumberArray[SubscribersStringGrid.Row - 1];
  346.     EditForm.Enabled := false;
  347.     CorrectForm.Show;
  348. end;
  349.  
  350. function TEditForm.IsCorrectTextData(CurrentEdit: TEdit): boolean;
  351. var
  352.     IsCorrectInput: boolean;
  353.     TextLength, i: integer;
  354. begin
  355.     isCorrectInput := false;
  356.     TextLength := length(CurrentEdit.text);
  357.     i := 2;
  358.     if TextLength <= CurrentEdit.MaxLength then
  359.     begin
  360.         if TextLength = 0 then
  361.             IsCorrectInput := true
  362.         else
  363.         begin
  364.             if (TextLength = 1) and (CurrentEdit.text[1] in BIG_LETTERS) then
  365.                 IsCorrectInput := True
  366.             else
  367.             begin
  368.                 IsCorrectInput := CurrentEdit.text[1] in BIG_LETTERS;
  369.                 while IsCorrectInput and (i <= TextLength) do
  370.                 begin
  371.                     if not(CurrentEdit.text[i] in SMALL_LETTERS) then
  372.                         IsCorrectInput := false;
  373.                     inc(i);
  374.                 end;
  375.             end;
  376.         end;
  377.     end;
  378.     Result := IsCorrectInput;
  379. end;
  380.  
  381. function TEditForm.isCorrectNumberData(CurrentEdit: TEdit): boolean;
  382.  
  383. var
  384.     IsCorrectInput: boolean;
  385.     TextLength, i: integer;
  386.  
  387. begin
  388.     isCorrectInput := false;
  389.     TextLength := length(CurrentEdit.text);
  390.     i := 2;
  391.     if TextLength <= CurrentEdit.MaxLength then
  392.     begin
  393.         if TextLength = 0 then
  394.             IsCorrectInput := true
  395.         else
  396.         begin
  397.             if (TextLength = 1) and ((CurrentEdit.text[1] in DIGITS) or (CurrentEdit.text[1] = '+')) then
  398.                 isCorrectInput := true
  399.             else
  400.             begin
  401.                 IsCorrectInput := (CurrentEdit.text[1] in DIGITS) or (CurrentEdit.text[1] = '+');
  402.                 while isCorrectInput and (i <= TextLength) do
  403.                 begin
  404.                     if not (CurrentEdit.text[i] in DIGITS) then
  405.                         IsCorrectInput := false;
  406.                     inc(i);
  407.                 end;
  408.             end;
  409.         end;
  410.     end;
  411.     Result := IsCorrectInput;
  412. end;
  413.  
  414. end.
  415.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement