Advertisement
believe_me

Untitled

Sep 5th, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.17 KB | None | 0 0
  1. unit MainUnit;
  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.StdCtrls, Vcl.Menus, Vcl.Grids,
  8.   Vcl.Samples.Spin;
  9.  
  10. type
  11.   TSubscriber = record
  12.     yearOfReg: integer;
  13.     surname: string[15];
  14.     telephoneNumber: int64;
  15.   end;
  16.   TSubscriberArray = array of TSubscriber;
  17.   TIndexArray = array of integer;
  18.   TMainForm = class(TForm)
  19.     SubscribersStringGrid: TStringGrid;
  20.     MainMenu: TMainMenu;
  21.     InstructionMenu: TMenuItem;
  22.     Developermenu: TMenuItem;
  23.     AddButton: TButton;
  24.     DeleteButton: TButton;
  25.     EditButton: TButton;
  26.     SurnameEdit: TEdit;
  27.     SurnameLabel: TLabel;
  28.     YearLabel: TLabel;
  29.     CountLabel: TLabel;
  30.     ShowButton: TButton;
  31.     YearSpinEdit: TSpinEdit;
  32.     procedure FormShow(Sender: TObject);
  33.     procedure ShowSubscribers(CurrSub: TSubscriber);
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure ShowButtonClick(Sender: TObject);
  36.     procedure SurnameEditKeyPress(Sender: TObject; var Key: Char);
  37.     procedure DeleteButtonClick(Sender: TObject);
  38.     procedure AddButtonClick(Sender: TObject);
  39.     procedure EditButtonClick(Sender: TObject);
  40.     procedure DevelopermenuClick(Sender: TObject);
  41.     procedure InstructionMenuClick(Sender: TObject);
  42.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  43.   private
  44.     { Private declarations }
  45.   public
  46.     { Public declarations }
  47.   end;
  48.   procedure ReadSubsFromFile();
  49.   procedure WriteSubsInFile();
  50.   procedure SortBySurname(Subs: TSubscriberArray);
  51.   function IsInputCorrect(CurrEdit: TEdit): boolean;
  52.   function IsSubscriberFileEmpty(): boolean;
  53.   function isSameString(Full: string; Part: string): boolean;
  54.  
  55. const
  56.    MAX_SURNAME_LENGTH = 15;
  57.    MIN_SURNAME_LENGTH = 3;
  58.    MAX_TELEPHONE_LENGTH = 15;
  59.    MIN_TELEPHONE_LENGTH = 1;
  60.    MIN_YEAR_OF_REGISTRATION_LENGTH = 4;
  61.    MIN_YEAR_OF_REGISTRATION = 1980;
  62.    MAX_YEAR_OF_REGISTRATION = 2022;
  63.    LETTERS = ['A'..'Z', 'a'..'z', 'А'..'Я', 'а'..'Я'];
  64.    DIGITS = ['0'..'9'];
  65.  
  66. var
  67.   MainForm: TMainForm;
  68.   Subscribers: TSubscriberArray;
  69.   IndexArray: TIndexArray;
  70.   SelectedSub: integer;
  71.   FullFileName: string;
  72.  
  73. implementation
  74.  
  75. {$R *.dfm}
  76.  
  77. uses
  78.    AddUnit, EditUnit;
  79.  
  80. procedure SortBySurname(Subs: TSubscriberArray);
  81. var
  82.    i, j: integer;
  83.    Temp: TSubscriber;
  84. begin
  85.    for i := 1 to high(Subs) do
  86.    begin
  87.       j := i;
  88.       while Subs[j].surname < Subs[j - 1].surname do
  89.       begin
  90.          Temp := Subs[j - 1];
  91.          Subs[j - 1] := Subs[j];
  92.          Subs[j] := Temp;
  93.       end;
  94.    end;
  95. end;
  96.  
  97. procedure ReadSubsFromFile();
  98. var
  99.    i: integer;
  100.    RecordFile: File of TSubscriber;
  101. begin
  102.    i := 0;
  103.    AssignFile(RecordFile, FullFileName);
  104.    Reset(RecordFile);
  105.    setlength(Subscribers, 0);
  106.    while not EOF(RecordFile) do
  107.    begin
  108.       setlength(Subscribers, (length(Subscribers) + 1));
  109.       Read(RecordFile, Subscribers[i]);
  110.       inc(i);
  111.    end;
  112.    CLose(RecordFile);
  113. end;
  114.  
  115. procedure WriteSubsInFile();
  116. var
  117.    i: integer;
  118.    RecordFile: File of TSubscriber;
  119. begin
  120.    AssignFile(RecordFile, FullFileName);
  121.    Rewrite(RecordFile);
  122.    for i := 0 to high(Subscribers) do
  123.    begin
  124.       Write(RecordFile, Subscribers[i]);
  125.    end;
  126.    Close(RecordFile);
  127. end;
  128.  
  129. function isSameString(Full: string; Part: string): boolean;
  130. var
  131.    IsSame: boolean;
  132.    i: integer;
  133. begin
  134.    IsSame := true;
  135.    i := 1;
  136.    while (i <= length(part)) and IsSame do
  137.    begin
  138.       if full[i] <> part[i] then
  139.          IsSame := false;
  140.       inc(i);
  141.    end;
  142.    Result := IsSame;
  143. end;
  144.  
  145. procedure TMainForm.ShowSubscribers(CurrSub: TSubscriber);
  146. var
  147.    i, j: integer;
  148. begin
  149.    setlength(IndexArray, 0);
  150.    ReadSubsFromFile;
  151.    sortBySurname(Subscribers);
  152.    j := 0;
  153.    SubscribersStringGrid.RowCount := 1;
  154.    setlength(IndexArray,1);
  155.    for i := 0 to high(Subscribers) do
  156.    begin
  157.       if (IsSameString(Subscribers[i].surname ,CurrSub.surname))
  158.       and (CurrSub.yearOfReg <= Subscribers[i].yearOfReg) then
  159.       begin
  160.          SubscribersStringGrid.RowCount := SubscribersStringGrid.RowCount + 1;
  161.          SubscribersStringGrid.Cells[0, j + 1] := Subscribers[i].surname;
  162.          SubscribersStringGrid.Cells[1, j + 1] := intToStr(Subscribers[i].telephoneNumber);
  163.          SubscribersStringGrid.Cells[2, j + 1] := IntToStr(Subscribers[i].yearOfReg);
  164.          setlength(IndexArray,(length(IndexArray) + 1));
  165.          IndexArray[j + 1] := i;
  166.          inc(j);
  167.       end;
  168.    end;
  169.    CountLabel.Caption := 'Количетсво абонентов зарагестрированных после ' +
  170.                           YearSpinEdit.text + ' года: ' + IntToStr(j);
  171.    CountLabel.Visible := true;
  172. end;
  173.  
  174. procedure TMainForm.SurnameEditKeyPress(Sender: TObject; var Key: Char);
  175. begin
  176.    if (Key <> #08) then
  177.    begin
  178.       if not(Key in LETTERS) then
  179.          Key := #0;
  180.    end;
  181. end;
  182.  
  183. procedure TMainForm.ShowButtonClick(Sender: TObject);
  184. var
  185.    CurrSub: TSubscriber;
  186. begin
  187.    if IsInputCorrect(SurnameEdit) then
  188.    begin
  189.       if not IsSubscriberFileEmpty then
  190.       begin
  191.          CurrSub.yearOfReg := YearSpinEdit.Value;
  192.          CurrSub.surname := SurnameEdit.text;
  193.          ShowSubscribers(CurrSub);
  194.       end
  195.       else
  196.          Application.MessageBox('Файл с абонентами пуст!', 'Ошибка!', MB_ICONERROR);  
  197.    end
  198.    else
  199.       Application.MessageBox('Введены некорректные данные!', 'Ошибка!', MB_ICONERROR);  
  200. end;
  201.  
  202. function IsSubscriberFileEmpty(): boolean;
  203. var
  204.    RecordFile: File of TSubscriber;
  205. begin
  206.    AssignFile(RecordFile, FullFileName);
  207.    Reset(RecordFile);
  208.    Result := FileSize(RecordFile) = 0;
  209.    Close(RecordFile);
  210. end;
  211.  
  212. function IsInputCorrect(CurrEdit: TEdit): boolean;
  213. var
  214.    IsCorrect: boolean;
  215.    i: integer;
  216.    Text: string;
  217. begin
  218.    IsCorrect := true;
  219.    i := 1;
  220.    Text := CurrEdit.Text;
  221.    if (length(Text) > CurrEdit.MaxLength) then
  222.       IsCorrect := false
  223.    else
  224.       while (i <= length(Text)) and IsCorrect do
  225.       begin
  226.          if not (Text[i] in LETTERS) then
  227.             IsCorrect := false;
  228.          inc(i);
  229.       end;
  230.    Result := IsCorrect;
  231. end;
  232.  
  233. procedure TMainForm.AddButtonClick(Sender: TObject);
  234. begin
  235.    MainForm.hide;
  236.    AddForm.show;
  237. end;
  238.  
  239. procedure TMainForm.DeleteButtonClick(Sender: TObject);
  240. var
  241.    Index, i: integer;
  242. begin
  243.    Index := IndexArray[SubscribersStringGrid.Row];
  244.    for i := index to (high(Subscribers) - 1) do
  245.       Subscribers[i] := Subscribers[i + 1];
  246.    SetLength(Subscribers, (length(Subscribers) - 1));
  247.    WriteSubsInFile;
  248.    if SubscribersStringGrid.RowCount > 2 then
  249.       ShowButtonClick(Sender)
  250.    else
  251.    begin
  252.       SubscribersStringGrid.RowCount := 1;
  253.       CountLabel.Visible := false;
  254.    end;
  255. end;
  256.  
  257. procedure TMainForm.DevelopermenuClick(Sender: TObject);
  258. begin
  259.    MessageDlg('Раводин Александр. Группа 151002', MtInformation, [mbOk], 0);
  260. end;
  261.  
  262. procedure TMainForm.EditButtonClick(Sender: TObject);
  263. begin
  264.    SelectedSub := IndexArray[SubscribersStringGrid.Row];
  265.    MainForm.hide;
  266.    EditForm.show;
  267. end;
  268.  
  269. procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  270. begin
  271.    CanClose := False;
  272.     if MessageDlg('Вы уверены, что хотите выйти?',mtConfirmation, mbOKCancel, 0) = mrOk then
  273.     begin
  274.         CanClose := True;
  275.     end;
  276. end;
  277.  
  278. procedure TMainForm.FormCreate(Sender: TObject);
  279. const
  280.    FILE_NAME = 'records.bin';
  281. begin
  282.    FullFileName := GetCurrentDir + '\' + FILE_NAME;
  283.    YearSpinEdit.MinValue := MIN_YEAR_OF_REGISTRATION;
  284.    YearSpinEdit.MaxValue := MAX_YEAR_OF_REGISTRATION;
  285.    YearSpinEdit.MaxLength := MIN_YEAR_OF_REGISTRATION_LENGTH;
  286.    SubscribersStringGrid.Cells[0, 0] := '                    Фамилия';
  287.    SubscribersStringGrid.Cells[1, 0] := '             Номер телефона';
  288.    SubscribersStringGrid.Cells[2, 0] :=  'Год регистрации';
  289. end;
  290.  
  291. procedure TMainForm.FormShow(Sender: TObject);
  292. const
  293.    NUM_COL_COEF = 2.7;
  294.    SUR_COL_COEF = 2.5;
  295.    YEAR_COL_COEF = 5.1;
  296. var
  297.    FrameWidth: integer;
  298. begin
  299.    FrameWidth := SubscribersStringGrid.Width;
  300.    SubscribersStringGrid.ColWidths[0] :=  Round(FrameWidth / SUR_COL_COEF);
  301.    SubscribersStringGrid.ColWidths[1] :=  Round(FrameWidth / NUM_COL_COEF);
  302.    SubscribersStringGrid.ColWidths[2] :=  Round(FrameWidth / YEAR_COL_COEF);
  303.    CountLabel.Visible := false;
  304.    SubscribersStringGrid.RowCount := 1;
  305. end;
  306.  
  307. procedure TMainForm.InstructionMenuClick(Sender: TObject);
  308. begin
  309.    MessageDlg('Главное окно приложения.' + #13#10 + #13#10 +
  310.                'Абоненты отображаются в таблице в соответсвии с введёнными данными.'
  311.                + #13#10 + 'Для изменения данных в базе используйте клавиши:' + #13#10
  312.                + 'Добавить;' + #13#10 + 'Изменить;' + #13#10 + 'Удалить.' + #13#10 +
  313.                'Количество абонентов, зарегистрированных с ХХХХ года отображается снизу.'
  314.                , MtInformation, [mbOk], 0);
  315. end;
  316.  
  317. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement