Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit MainUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus, Vcl.Grids,
- Vcl.Samples.Spin;
- type
- TSubscriber = record
- yearOfReg: integer;
- surname: string[15];
- telephoneNumber: int64;
- end;
- TSubscriberArray = array of TSubscriber;
- TIndexArray = array of integer;
- TMainForm = class(TForm)
- SubscribersStringGrid: TStringGrid;
- MainMenu: TMainMenu;
- InstructionMenu: TMenuItem;
- Developermenu: TMenuItem;
- AddButton: TButton;
- DeleteButton: TButton;
- EditButton: TButton;
- SurnameEdit: TEdit;
- SurnameLabel: TLabel;
- YearLabel: TLabel;
- CountLabel: TLabel;
- ShowButton: TButton;
- YearSpinEdit: TSpinEdit;
- procedure FormShow(Sender: TObject);
- procedure ShowSubscribers(CurrSub: TSubscriber);
- procedure FormCreate(Sender: TObject);
- procedure ShowButtonClick(Sender: TObject);
- procedure SurnameEditKeyPress(Sender: TObject; var Key: Char);
- procedure DeleteButtonClick(Sender: TObject);
- procedure AddButtonClick(Sender: TObject);
- procedure EditButtonClick(Sender: TObject);
- procedure DevelopermenuClick(Sender: TObject);
- procedure InstructionMenuClick(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- procedure ReadSubsFromFile();
- procedure WriteSubsInFile();
- procedure SortBySurname(Subs: TSubscriberArray);
- function IsInputCorrect(CurrEdit: TEdit): boolean;
- function IsSubscriberFileEmpty(): boolean;
- function isSameString(Full: string; Part: string): boolean;
- const
- MAX_SURNAME_LENGTH = 15;
- MIN_SURNAME_LENGTH = 3;
- MAX_TELEPHONE_LENGTH = 15;
- MIN_TELEPHONE_LENGTH = 1;
- MIN_YEAR_OF_REGISTRATION_LENGTH = 4;
- MIN_YEAR_OF_REGISTRATION = 1980;
- MAX_YEAR_OF_REGISTRATION = 2022;
- LETTERS = ['A'..'Z', 'a'..'z', 'А'..'Я', 'а'..'Я'];
- DIGITS = ['0'..'9'];
- var
- MainForm: TMainForm;
- Subscribers: TSubscriberArray;
- IndexArray: TIndexArray;
- SelectedSub: integer;
- FullFileName: string;
- implementation
- {$R *.dfm}
- uses
- AddUnit, EditUnit;
- procedure SortBySurname(Subs: TSubscriberArray);
- var
- i, j: integer;
- Temp: TSubscriber;
- begin
- for i := 1 to high(Subs) do
- begin
- j := i;
- while Subs[j].surname < Subs[j - 1].surname do
- begin
- Temp := Subs[j - 1];
- Subs[j - 1] := Subs[j];
- Subs[j] := Temp;
- end;
- end;
- end;
- procedure ReadSubsFromFile();
- var
- i: integer;
- RecordFile: File of TSubscriber;
- begin
- i := 0;
- AssignFile(RecordFile, FullFileName);
- Reset(RecordFile);
- setlength(Subscribers, 0);
- while not EOF(RecordFile) do
- begin
- setlength(Subscribers, (length(Subscribers) + 1));
- Read(RecordFile, Subscribers[i]);
- inc(i);
- end;
- CLose(RecordFile);
- end;
- procedure WriteSubsInFile();
- var
- i: integer;
- RecordFile: File of TSubscriber;
- begin
- AssignFile(RecordFile, FullFileName);
- Rewrite(RecordFile);
- for i := 0 to high(Subscribers) do
- begin
- Write(RecordFile, Subscribers[i]);
- end;
- Close(RecordFile);
- end;
- function isSameString(Full: string; Part: string): boolean;
- var
- IsSame: boolean;
- i: integer;
- begin
- IsSame := true;
- i := 1;
- while (i <= length(part)) and IsSame do
- begin
- if full[i] <> part[i] then
- IsSame := false;
- inc(i);
- end;
- Result := IsSame;
- end;
- procedure TMainForm.ShowSubscribers(CurrSub: TSubscriber);
- var
- i, j: integer;
- begin
- setlength(IndexArray, 0);
- ReadSubsFromFile;
- sortBySurname(Subscribers);
- j := 0;
- SubscribersStringGrid.RowCount := 1;
- setlength(IndexArray,1);
- for i := 0 to high(Subscribers) do
- begin
- if (IsSameString(Subscribers[i].surname ,CurrSub.surname))
- and (CurrSub.yearOfReg <= Subscribers[i].yearOfReg) then
- begin
- SubscribersStringGrid.RowCount := SubscribersStringGrid.RowCount + 1;
- SubscribersStringGrid.Cells[0, j + 1] := Subscribers[i].surname;
- SubscribersStringGrid.Cells[1, j + 1] := intToStr(Subscribers[i].telephoneNumber);
- SubscribersStringGrid.Cells[2, j + 1] := IntToStr(Subscribers[i].yearOfReg);
- setlength(IndexArray,(length(IndexArray) + 1));
- IndexArray[j + 1] := i;
- inc(j);
- end;
- end;
- CountLabel.Caption := 'Количетсво абонентов зарагестрированных после ' +
- YearSpinEdit.text + ' года: ' + IntToStr(j);
- CountLabel.Visible := true;
- end;
- procedure TMainForm.SurnameEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key <> #08) then
- begin
- if not(Key in LETTERS) then
- Key := #0;
- end;
- end;
- procedure TMainForm.ShowButtonClick(Sender: TObject);
- var
- CurrSub: TSubscriber;
- begin
- if IsInputCorrect(SurnameEdit) then
- begin
- if not IsSubscriberFileEmpty then
- begin
- CurrSub.yearOfReg := YearSpinEdit.Value;
- CurrSub.surname := SurnameEdit.text;
- ShowSubscribers(CurrSub);
- end
- else
- Application.MessageBox('Файл с абонентами пуст!', 'Ошибка!', MB_ICONERROR);
- end
- else
- Application.MessageBox('Введены некорректные данные!', 'Ошибка!', MB_ICONERROR);
- end;
- function IsSubscriberFileEmpty(): boolean;
- var
- RecordFile: File of TSubscriber;
- begin
- AssignFile(RecordFile, FullFileName);
- Reset(RecordFile);
- Result := FileSize(RecordFile) = 0;
- Close(RecordFile);
- end;
- function IsInputCorrect(CurrEdit: TEdit): boolean;
- var
- IsCorrect: boolean;
- i: integer;
- Text: string;
- begin
- IsCorrect := true;
- i := 1;
- Text := CurrEdit.Text;
- if (length(Text) > CurrEdit.MaxLength) then
- IsCorrect := false
- else
- while (i <= length(Text)) and IsCorrect do
- begin
- if not (Text[i] in LETTERS) then
- IsCorrect := false;
- inc(i);
- end;
- Result := IsCorrect;
- end;
- procedure TMainForm.AddButtonClick(Sender: TObject);
- begin
- MainForm.hide;
- AddForm.show;
- end;
- procedure TMainForm.DeleteButtonClick(Sender: TObject);
- var
- Index, i: integer;
- begin
- Index := IndexArray[SubscribersStringGrid.Row];
- for i := index to (high(Subscribers) - 1) do
- Subscribers[i] := Subscribers[i + 1];
- SetLength(Subscribers, (length(Subscribers) - 1));
- WriteSubsInFile;
- if SubscribersStringGrid.RowCount > 2 then
- ShowButtonClick(Sender)
- else
- begin
- SubscribersStringGrid.RowCount := 1;
- CountLabel.Visible := false;
- end;
- end;
- procedure TMainForm.DevelopermenuClick(Sender: TObject);
- begin
- MessageDlg('Раводин Александр. Группа 151002', MtInformation, [mbOk], 0);
- end;
- procedure TMainForm.EditButtonClick(Sender: TObject);
- begin
- SelectedSub := IndexArray[SubscribersStringGrid.Row];
- MainForm.hide;
- EditForm.show;
- end;
- procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := False;
- if MessageDlg('Вы уверены, что хотите выйти?',mtConfirmation, mbOKCancel, 0) = mrOk then
- begin
- CanClose := True;
- end;
- end;
- procedure TMainForm.FormCreate(Sender: TObject);
- const
- FILE_NAME = 'records.bin';
- begin
- FullFileName := GetCurrentDir + '\' + FILE_NAME;
- YearSpinEdit.MinValue := MIN_YEAR_OF_REGISTRATION;
- YearSpinEdit.MaxValue := MAX_YEAR_OF_REGISTRATION;
- YearSpinEdit.MaxLength := MIN_YEAR_OF_REGISTRATION_LENGTH;
- SubscribersStringGrid.Cells[0, 0] := ' Фамилия';
- SubscribersStringGrid.Cells[1, 0] := ' Номер телефона';
- SubscribersStringGrid.Cells[2, 0] := 'Год регистрации';
- end;
- procedure TMainForm.FormShow(Sender: TObject);
- const
- NUM_COL_COEF = 2.7;
- SUR_COL_COEF = 2.5;
- YEAR_COL_COEF = 5.1;
- var
- FrameWidth: integer;
- begin
- FrameWidth := SubscribersStringGrid.Width;
- SubscribersStringGrid.ColWidths[0] := Round(FrameWidth / SUR_COL_COEF);
- SubscribersStringGrid.ColWidths[1] := Round(FrameWidth / NUM_COL_COEF);
- SubscribersStringGrid.ColWidths[2] := Round(FrameWidth / YEAR_COL_COEF);
- CountLabel.Visible := false;
- SubscribersStringGrid.RowCount := 1;
- end;
- procedure TMainForm.InstructionMenuClick(Sender: TObject);
- begin
- MessageDlg('Главное окно приложения.' + #13#10 + #13#10 +
- 'Абоненты отображаются в таблице в соответсвии с введёнными данными.'
- + #13#10 + 'Для изменения данных в базе используйте клавиши:' + #13#10
- + 'Добавить;' + #13#10 + 'Изменить;' + #13#10 + 'Удалить.' + #13#10 +
- 'Количество абонентов, зарегистрированных с ХХХХ года отображается снизу.'
- , MtInformation, [mbOk], 0);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement