Advertisement
believe_me

Untitled

Sep 5th, 2022
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.61 KB | None | 0 0
  1. unit AddUnit;
  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.Samples.Spin, MainUnit,
  8.   Vcl.ExtDlgs, Vcl.Menus;
  9.  
  10. type
  11.   TAddForm = class(TForm)
  12.     AddButton: TButton;
  13.     TelephoneEdit: TEdit;
  14.     SurnameEdit: TEdit;
  15.     YearSpinEdit: TSpinEdit;
  16.     TelephoneLabel: TLabel;
  17.     SurnameLabel: TLabel;
  18.     YearLabel: TLabel;
  19.     AddFromFileButton: TButton;
  20.     GoBackButton: TButton;
  21.     ResultLabel: TLabel;
  22.     MainMenu: TMainMenu;
  23.     InstructionMenu: TMenuItem;
  24.     OpenFileDialog: TOpenTextFileDialog;
  25.     procedure TelephoneEditKeyPress(Sender: TObject; var Key: Char);
  26.     procedure SurnameEditKeyPress(Sender: TObject; var Key: Char);
  27.     function IsTelCorrect(CurrEdit: TEdit): boolean;
  28.     function IsSurnameCorrect(CurrEdit: TEdit): boolean;
  29.     procedure AddFromFileButtonClick(Sender: TObject);
  30.     procedure AddButtonClick(Sender: TObject);
  31.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure GoBackButtonClick(Sender: TObject);
  34.     procedure YearSpinEditChange(Sender: TObject);
  35.     procedure SurnameEditChange(Sender: TObject);
  36.     procedure TelephoneEditChange(Sender: TObject);
  37.     procedure FormShow(Sender: TObject);
  38.     procedure InstructionMenuClick(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.   public
  42.     { Public declarations }
  43.   end;
  44. var
  45.   AddForm: TAddForm;
  46.  
  47. implementation
  48.  
  49. {$R *.dfm}
  50.  
  51. procedure TAddForm.SurnameEditChange(Sender: TObject);
  52. begin
  53.    ResultLabel.Visible := false;
  54. end;
  55.  
  56. procedure TAddForm.SurnameEditKeyPress(Sender: TObject; var Key: Char);
  57. begin
  58.    if (Key <> #08) then
  59.    begin
  60.       if not(Key in LETTERS) then
  61.          Key := #0;
  62.    end;
  63. end;
  64.  
  65. procedure TAddForm.TelephoneEditChange(Sender: TObject);
  66. begin
  67.    ResultLabel.Visible := false;
  68. end;
  69.  
  70. procedure TAddForm.TelephoneEditKeyPress(Sender: TObject; var Key: Char);
  71. begin
  72.    if (Key <> #08) then
  73.    begin
  74.       if not(Key in DIGITS) then
  75.          Key := #0;
  76.    end;
  77. end;
  78.  
  79. procedure TAddForm.YearSpinEditChange(Sender: TObject);
  80. begin
  81.    ResultLabel.Visible := false;
  82. end;
  83.  
  84. function TAddForm.IsTelCorrect(CurrEdit: TEdit): boolean;
  85. var
  86.    IsCorrect: boolean;
  87.    i: integer;
  88.    Text: string;
  89. begin
  90.    IsCorrect := true;
  91.    i := 1;
  92.    Text := CurrEdit.Text;
  93.    if (length(Text) > CurrEdit.MaxLength) or (length(Text) < 3) then
  94.       IsCorrect := false
  95.    else
  96.       while i < length(Text) do
  97.       begin
  98.          if not (Text[i] in DIGITS) then
  99.             IsCorrect := false;
  100.          Inc(i);
  101.       end;
  102.    Result := IsCorrect;
  103. end;
  104.  
  105. procedure TAddForm.AddButtonClick(Sender: TObject);
  106. var
  107.    CurrSub: TSubscriber;
  108. begin
  109.    ResultLabel.Visible := false;
  110.    if IsTelCorrect(TelephoneEdit) and IsSurnameCorrect(SurnameEdit) then
  111.    begin
  112.       CurrSub.telephoneNumber := StrToInt(TelephoneEdit.Text);
  113.       CurrSub.yearOfReg := YearSpinEdit.value;
  114.       CurrSub.surname := SurnameEdit.text;
  115.       setlength(Subscribers, (length(Subscribers) + 1));
  116.       Subscribers[high(Subscribers)] := CurrSub;
  117.       ResultLabel.Visible := true;
  118.       WriteSubsInFile();
  119.    end
  120.    else
  121.       Application.MessageBox('Введены некорректные данные!', 'Ошибка!', MB_ICONERROR);
  122. end;
  123.  
  124. procedure TAddForm.AddFromFileButtonClick(Sender: TObject);
  125. var
  126.    InputFile: TextFile;
  127.    RecordFile: File of TSubscriber;
  128.    CurrSub: TSubscriber;
  129.    IsCorrect: boolean;
  130. begin
  131.    ResultLabel.Visible := false;
  132.    if OpenFileDialog.Execute() then
  133.    begin
  134.       AssignFile(RecordFile, FullFileName);
  135.       if FileExists(FullFileName) then
  136.       begin
  137.          Reset(RecordFile);
  138.          Seek(RecordFile, FileSize(RecordFile));
  139.       end
  140.       else
  141.          Rewrite(RecordFile);
  142.       AssignFile(InputFile, OpenFileDialog.FileName);
  143.       Reset(InputFile);
  144.       while not EOF(InputFile) and IsCorrect do
  145.       begin
  146.          try
  147.             Readln(InputFile, CurrSub.surname);
  148.             Readln(InputFile, CurrSub.telephoneNumber);
  149.             Readln(InputFile, CurrSub.yearOfReg);
  150.         except
  151.             IsCorrect := false;
  152.         end;
  153.         Write(RecordFile, CurrSub);
  154.       end;
  155.       CloseFile(RecordFile);
  156.       CloseFile(InputFile);
  157.       ResultLabel.Visible:= true;
  158.    end;
  159.    if not IsCorrect then
  160.       Application.MessageBox('Некорректные данные в файле!', 'Ошибка!',
  161.        MB_ICONERROR);
  162.  end;
  163.  
  164.  procedure TAddForm.FormClose(Sender: TObject; var Action: TCloseAction);
  165. begin
  166.    ResultLabel.Visible := false;
  167.    SurnameEdit.Text := '';
  168.    TelephoneEdit.Text := '';
  169.    AddForm.hide;
  170.    MainForm.Show;
  171. end;
  172.  
  173. procedure TAddForm.FormCreate(Sender: TObject);
  174. begin
  175.    SurnameEdit.MaxLength := MAX_SURNAME_LENGTH;
  176.    TelephoneEdit.MaxLength := MAX_TELEPHONE_LENGTH;
  177.    YearSpinEdit.MinValue := MIN_YEAR_OF_REGISTRATION;
  178.    YearSpinEdit.MaxValue := MAX_YEAR_OF_REGISTRATION;
  179.    YearSpinEdit.MaxLength := MIN_YEAR_OF_REGISTRATION_LENGTH;
  180. end;
  181.  
  182. procedure TAddForm.FormShow(Sender: TObject);
  183. begin
  184.    YearSpinEdit.Value := 2000;
  185. end;
  186.  
  187. procedure TAddForm.GoBackButtonClick(Sender: TObject);
  188. begin
  189.    ResultLabel.Visible := false;
  190.    SurnameEdit.Text := '';
  191.    TelephoneEdit.Text := '';
  192.    AddForm.hide;
  193.    MainForm.Show;
  194. end;
  195.  
  196. procedure TAddForm.InstructionMenuClick(Sender: TObject);
  197. begin
  198.    MessageDlg('Окно добавления.' + #13#10 + #13#10 +
  199.                'Если вы хотите добавить нового абонента самостоятельно:'
  200.                + #13#10 + '1. Заполните все поля;' + #13#10
  201.                + '2. Нажмите клавишу "Добавить".' + #13#10 + #13#10 +
  202.                'Для добавления из файла:' + #13#10 +
  203.                '1. Нажмите "Добавить из файла";' + #13#10 + '1. Выберите файл.'
  204.                + #13#10 + 'Для возвращения к главному окну нажмите "Назад".',
  205.                MtInformation, [mbOk], 0);
  206. end;
  207.  
  208. function TAddForm.IsSurnameCorrect(CurrEdit: TEdit): boolean;
  209. var
  210.    IsCorrect: boolean;
  211.    i: integer;
  212.    Text: string;
  213. begin
  214.    IsCorrect := true;
  215.    Text := CurrEdit.Text;
  216.    i := 1;
  217.    if (length(Text) > CurrEdit.MaxLength) or (length(Text) = 0) then
  218.       IsCorrect := false
  219.    else
  220.       while (i < length(CurrEdit.Text)) and IsCorrect do
  221.       begin
  222.          if not (Text[i] in LETTERS) then
  223.             IsCorrect := false;
  224.          inc(i);
  225.       end;
  226.    Result := IsCorrect;
  227. end;
  228.  
  229. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement