Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit AddUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin, MainUnit,
- Vcl.ExtDlgs, Vcl.Menus;
- type
- TAddForm = class(TForm)
- AddButton: TButton;
- TelephoneEdit: TEdit;
- SurnameEdit: TEdit;
- YearSpinEdit: TSpinEdit;
- TelephoneLabel: TLabel;
- SurnameLabel: TLabel;
- YearLabel: TLabel;
- AddFromFileButton: TButton;
- GoBackButton: TButton;
- ResultLabel: TLabel;
- MainMenu: TMainMenu;
- InstructionMenu: TMenuItem;
- OpenFileDialog: TOpenTextFileDialog;
- procedure TelephoneEditKeyPress(Sender: TObject; var Key: Char);
- procedure SurnameEditKeyPress(Sender: TObject; var Key: Char);
- function IsTelCorrect(CurrEdit: TEdit): boolean;
- function IsSurnameCorrect(CurrEdit: TEdit): boolean;
- procedure AddFromFileButtonClick(Sender: TObject);
- procedure AddButtonClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure FormCreate(Sender: TObject);
- procedure GoBackButtonClick(Sender: TObject);
- procedure YearSpinEditChange(Sender: TObject);
- procedure SurnameEditChange(Sender: TObject);
- procedure TelephoneEditChange(Sender: TObject);
- procedure FormShow(Sender: TObject);
- procedure InstructionMenuClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- AddForm: TAddForm;
- implementation
- {$R *.dfm}
- procedure TAddForm.SurnameEditChange(Sender: TObject);
- begin
- ResultLabel.Visible := false;
- end;
- procedure TAddForm.SurnameEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key <> #08) then
- begin
- if not(Key in LETTERS) then
- Key := #0;
- end;
- end;
- procedure TAddForm.TelephoneEditChange(Sender: TObject);
- begin
- ResultLabel.Visible := false;
- end;
- procedure TAddForm.TelephoneEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key <> #08) then
- begin
- if not(Key in DIGITS) then
- Key := #0;
- end;
- end;
- procedure TAddForm.YearSpinEditChange(Sender: TObject);
- begin
- ResultLabel.Visible := false;
- end;
- function TAddForm.IsTelCorrect(CurrEdit: TEdit): boolean;
- var
- IsCorrect: boolean;
- i: integer;
- Text: string;
- begin
- IsCorrect := true;
- i := 1;
- Text := CurrEdit.Text;
- if (length(Text) > CurrEdit.MaxLength) or (length(Text) < 3) then
- IsCorrect := false
- else
- while i < length(Text) do
- begin
- if not (Text[i] in DIGITS) then
- IsCorrect := false;
- Inc(i);
- end;
- Result := IsCorrect;
- end;
- procedure TAddForm.AddButtonClick(Sender: TObject);
- var
- CurrSub: TSubscriber;
- begin
- ResultLabel.Visible := false;
- if IsTelCorrect(TelephoneEdit) and IsSurnameCorrect(SurnameEdit) then
- begin
- CurrSub.telephoneNumber := StrToInt(TelephoneEdit.Text);
- CurrSub.yearOfReg := YearSpinEdit.value;
- CurrSub.surname := SurnameEdit.text;
- setlength(Subscribers, (length(Subscribers) + 1));
- Subscribers[high(Subscribers)] := CurrSub;
- ResultLabel.Visible := true;
- WriteSubsInFile();
- end
- else
- Application.MessageBox('Введены некорректные данные!', 'Ошибка!', MB_ICONERROR);
- end;
- procedure TAddForm.AddFromFileButtonClick(Sender: TObject);
- var
- InputFile: TextFile;
- RecordFile: File of TSubscriber;
- CurrSub: TSubscriber;
- IsCorrect: boolean;
- begin
- ResultLabel.Visible := false;
- if OpenFileDialog.Execute() then
- begin
- AssignFile(RecordFile, FullFileName);
- if FileExists(FullFileName) then
- begin
- Reset(RecordFile);
- Seek(RecordFile, FileSize(RecordFile));
- end
- else
- Rewrite(RecordFile);
- AssignFile(InputFile, OpenFileDialog.FileName);
- Reset(InputFile);
- while not EOF(InputFile) and IsCorrect do
- begin
- try
- Readln(InputFile, CurrSub.surname);
- Readln(InputFile, CurrSub.telephoneNumber);
- Readln(InputFile, CurrSub.yearOfReg);
- except
- IsCorrect := false;
- end;
- Write(RecordFile, CurrSub);
- end;
- CloseFile(RecordFile);
- CloseFile(InputFile);
- ResultLabel.Visible:= true;
- end;
- if not IsCorrect then
- Application.MessageBox('Некорректные данные в файле!', 'Ошибка!',
- MB_ICONERROR);
- end;
- procedure TAddForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- ResultLabel.Visible := false;
- SurnameEdit.Text := '';
- TelephoneEdit.Text := '';
- AddForm.hide;
- MainForm.Show;
- end;
- procedure TAddForm.FormCreate(Sender: TObject);
- begin
- SurnameEdit.MaxLength := MAX_SURNAME_LENGTH;
- TelephoneEdit.MaxLength := MAX_TELEPHONE_LENGTH;
- YearSpinEdit.MinValue := MIN_YEAR_OF_REGISTRATION;
- YearSpinEdit.MaxValue := MAX_YEAR_OF_REGISTRATION;
- YearSpinEdit.MaxLength := MIN_YEAR_OF_REGISTRATION_LENGTH;
- end;
- procedure TAddForm.FormShow(Sender: TObject);
- begin
- YearSpinEdit.Value := 2000;
- end;
- procedure TAddForm.GoBackButtonClick(Sender: TObject);
- begin
- ResultLabel.Visible := false;
- SurnameEdit.Text := '';
- TelephoneEdit.Text := '';
- AddForm.hide;
- MainForm.Show;
- end;
- procedure TAddForm.InstructionMenuClick(Sender: TObject);
- begin
- MessageDlg('Окно добавления.' + #13#10 + #13#10 +
- 'Если вы хотите добавить нового абонента самостоятельно:'
- + #13#10 + '1. Заполните все поля;' + #13#10
- + '2. Нажмите клавишу "Добавить".' + #13#10 + #13#10 +
- 'Для добавления из файла:' + #13#10 +
- '1. Нажмите "Добавить из файла";' + #13#10 + '1. Выберите файл.'
- + #13#10 + 'Для возвращения к главному окну нажмите "Назад".',
- MtInformation, [mbOk], 0);
- end;
- function TAddForm.IsSurnameCorrect(CurrEdit: TEdit): boolean;
- var
- IsCorrect: boolean;
- i: integer;
- Text: string;
- begin
- IsCorrect := true;
- Text := CurrEdit.Text;
- i := 1;
- if (length(Text) > CurrEdit.MaxLength) or (length(Text) = 0) then
- IsCorrect := false
- else
- while (i < length(CurrEdit.Text)) and IsCorrect do
- begin
- if not (Text[i] in LETTERS) then
- IsCorrect := false;
- inc(i);
- end;
- Result := IsCorrect;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement