Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit EditUnit;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.Menus, Vcl.StdCtrls, ListUnit,
- System.Actions, Vcl.ActnList, System.UITypes;
- type
- TEditForm = class(TForm)
- SubscribersStringGrid: TStringGrid;
- TelephoneEdit: TEdit;
- MainMenu: TMainMenu;
- AddButton: TButton;
- EditButton: TButton;
- DeleteButton: TButton;
- InstructionMenu: TMenuItem;
- TelephoneRButton: TRadioButton;
- SortLabel: TLabel;
- NameRButton: TRadioButton;
- SurnameRButton: TRadioButton;
- PatronymicRButton: TRadioButton;
- CityRButton: TRadioButton;
- NameLabel: TLabel;
- SurnameLabel: TLabel;
- NameEdit: TEdit;
- SurnameEdit: TEdit;
- PatronymicEdit: TEdit;
- CItyEdit: TEdit;
- ShowButton: TButton;
- PatronymicLabel: TLabel;
- CityLabel: TLabel;
- TelephoneLabel: TLabel;
- TaskLabel: TLabel;
- ClearButton: TButton;
- DateRButton: TRadioButton;
- procedure FormShow(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure AddButtonClick(Sender: TObject);
- procedure fillStringGrid(SubscribersStringGrid: TStringGrid; TelephoneEdit, SurnameEdit, NameEdit,
- PatronymicEdit, CityEdit: TEdit);
- procedure ShowButtonClick(Sender: TObject);
- procedure clearStringGrid();
- procedure DeleteButtonClick(Sender: TObject);
- procedure RadioButtonCLick(Sender: TObject);
- procedure ClearButtonClick(Sender: TObject);
- function isCorrectTextData(CurrentEdit: TEdit): boolean;
- function isCorrectNumberData(CurrentEdit: TEdit): boolean;
- procedure DataEditKeyPress(Sender: TObject; var Key: Char);
- procedure FormKeyPress(Sender: TObject; var Key: Char);
- procedure TelephoneEditKeyPress(Sender: TObject; var Key: Char);
- function areEditsCorrect(): boolean;
- procedure EditButtonClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure InstructionMenuClick(Sender: TObject);
- procedure buildStringGrid(SubscribersStringGrid: TStringGrid);
- procedure setEditButtonsCondition();
- procedure sortList();
- procedure getRequiredSubscriber(var RequiredSubscriber: TRequiredSubscriber;
- RequiredTelephoneNumber: string; RequiredSurname: string; RequiredName: string;
- RequiredPatronymic: string; RequiredCity: string);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- EditForm: TEditForm;
- SubscriberList: TSubscriberList;
- SubscriberIndex: TNumber;
- implementation
- {$R *.dfm}
- uses
- ReadUnit, WriteUnit, AddUnit, CorrectUnit, StartUnit;
- const
- TEXT_TOP_OFFSET = 3;
- BIG_LETTERS = ['A'..'Z'];
- SMALL_LETTERS = ['a'..'z'];
- DIGITS = ['0'..'9'];
- var
- CurrentRadioButton: integer = 0;
- NumberArray: array of TNumber;
- procedure TEditForm.AddButtonClick(Sender: TObject);
- begin
- AddForm.Show;
- EditForm.Enabled := false;
- end;
- procedure TEditForm.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- SubscriberList.deleteList;
- clearStringGrid;
- StartForm.enabled := true;
- StartForm.show;
- end;
- procedure TEditForm.setEditButtonsCondition();
- begin
- if SubscribersStringGrid.RowCount > 1 then
- begin
- DeleteButton.Enabled := true;
- EditButton.Enabled := true;
- end
- else
- begin
- DeleteButton.Enabled := false;
- EditButton.Enabled := false;
- end;
- end;
- procedure TEditForm.buildStringGrid(SubscribersStringGrid: TStringGrid);
- var
- StringGridSize: integer;
- begin
- SubscribersStringGrid.Cells[0, 0] := ' №';
- SubscribersStringGrid.Cells[1, 0] := ' Telephone';
- SubscribersStringGrid.Cells[2, 0] := ' Surname';
- SubscribersStringGrid.Cells[3, 0] := ' Name';
- SubscribersStringGrid.Cells[4, 0] := ' Patronymic';
- SubscribersStringGrid.Cells[5, 0] := ' City';
- SubscribersStringGrid.Cells[6, 0] := 'Days left';
- StringGridSize := SubscribersStringGrid.width;
- SubscribersStringGrid.ColWidths[0] := Round(StringGridSize / 14);
- SubscribersStringGrid.ColWidths[1] := Round(StringGridSize / 7.5);
- SubscribersStringGrid.ColWidths[2] := Round(StringGridSize / 5.1);
- SubscribersStringGrid.ColWidths[3] := Round(StringGridSize / 6.6);
- SubscribersStringGrid.ColWidths[4] := Round(StringGridSize / 5.1);
- SubscribersStringGrid.ColWidths[5] := Round(StringGridSize / 6.5);
- SubscribersStringGrid.ColWidths[6] := Round(StringGridSize / 13);
- end;
- procedure TEditForm.FormCreate(Sender: TObject);
- begin
- buildStringGrid(SubscribersStringGrid);
- end;
- procedure TEditForm.FormKeyPress(Sender: TObject; var Key: Char);
- begin
- if key = #27 then
- EditForm.Close()
- else
- if Key = #13 then
- ShowButtonClick(Sender);
- end;
- procedure TEditForm.FormShow(Sender: TObject);
- begin
- SubscriberList := TSubscriberList.NewSubscriberList();
- EditButton.Enabled := false;
- DeleteButton.Enabled := false;
- clearStringGrid;
- end;
- procedure TEditForm.InstructionMenuClick(Sender: TObject);
- begin
- MessageDlg('This is the edit window. It includes next functions:' + #13#10 + '1. Searching by certain data;' + #13#10
- + '2. Sorting by all categories;' + #13#10 +
- '3. Adding new subscriber;' + #13#10 + '4. Edtining selected one;' + #13#10 +
- '5. Deleting selected one.' + #13#10 + #13#10 +
- 'Right input:' + #13#10 +
- 'a) Telephone number field: first char - "+" or digit and next are digits; length = 13;'
- + #13#10 + 'b) Text fields: first char - big letter and next are small letters. Length:'
- + #13#10 + 'City -11, Surname - 14, Name - 11, Patronymic - 14.' + #13#10
- + #13#10 + 'Use "clear" to clear all fields and turn sort off;'
- + #13#10 + #13#10 + 'Press "ctrl + i" to open instruction;'
- + #13#10 + 'Press "enter" to show subscribers;'
- + #13#10 + 'Press "esc" to go back to start widnow.', MtInformation, [mbOk], 0);
- end;
- procedure TEditForm.ShowButtonClick(Sender: TObject);
- begin
- if areEditsCorrect() then
- begin
- clearStringGrid();
- if not isFileEmpty() then
- begin
- fillStringGrid(SubscribersStringGrid, TelephoneEdit, SurnameEdit, NameEdit,
- PatronymicEdit, CityEdit);
- setEditButtonsCondition();
- end
- else
- MessageDlg('Subscriber file is empty.', mtError, [mbOk], 0, mbOk);
- end
- else
- begin
- MessageDlg('Wrong data.', mtError, [mbCancel], 0);
- clearButtonClick(Sender);
- end;
- end;
- function TEditForm.areEditsCorrect(): boolean;
- begin
- Result := (IsCorrectNumberData(TelephoneEdit) and IsCorrectTextData(SurnameEdit) and
- IsCorrectTextData(NameEdit) and IsCorrectTextData(PatronymicEdit) and
- IsCorrectTextData(CityEdit));
- end;
- procedure TEditForm.DataEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key <> #08) then
- begin
- if length((Sender as TEdit).text) = 0 then
- begin
- if not (Key in BIG_LETTERS) then
- Key := #0
- end
- else
- if not(Key in SMALL_LETTERS) then
- Key := #0;
- end;
- end;
- procedure TEditForm.TelephoneEditKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Key <> #08) then
- begin
- if length((Sender as TEdit).text) = 0 then
- begin
- if ((not(Key in DIGITS)) and (Key <> '+')) then
- Key := #0
- end
- else
- if not(Key in DIGITS) then
- Key := #0;
- end;
- end;
- procedure TEditForm.RadioButtonCLick(Sender: TObject);
- begin
- CurrentRadioButton := (Sender as TRadioButton).tag;
- end;
- procedure TEditForm.ClearButtonClick(Sender: TObject);
- begin
- TelephoneEdit.text := '';
- NameEdit.text := '';
- SurnameEdit.text := '';
- PatronymicEdit.text := '';
- CityEdit.text := '';
- TelephoneRButton.Checked := false;
- SurnameRButton.Checked := false;
- NameRButton.Checked := false;
- PatronymicRButton.Checked := false;
- CityRButton.Checked := false;
- DateRButton.Checked := false;
- CurrentRadioButton := 0;
- SubscribersStringGrid.RowCount := 1;
- EditButton.Enabled := false;
- DeleteButton.Enabled := false;
- end;
- procedure TEditForm.sortList();
- begin
- if not SubscriberList.IsEmpty then
- case CurrentRadioButton of
- 0:
- ;
- 1:
- SubscriberList.sort(TSubscriberList.CompareByTelephoneNumber);
- 2:
- SubscriberList.sort(TSubscriberList.CompareBySurname);
- 3:
- SubscriberList.sort(TSubscriberList.CompareByName);
- 4:
- SubscriberList.sort(TSubscriberList.CompareByPatronymic);
- 5:
- SubscriberList.sort(TSubscriberList.CompareByCity);
- 6:
- SubscriberList.sort(TSubscriberList.CompareByDate);
- end;
- end;
- procedure TEditForm.fillStringGrid(SubscribersStringGrid: TStringGrid;
- TelephoneEdit, SurnameEdit, NameEdit,
- PatronymicEdit, CityEdit: TEdit);
- var
- i, j, ListLength: Integer;
- PCurrentSubscriber: TSubscriberPointer;
- RequiredSubscriber: TRequiredSubscriber;
- begin
- i := 1;
- j := 0;
- SubscriberList.deleteList();
- getRequiredSubscriber(RequiredSubscriber, TelephoneEdit.text, SurnameEdit.text, NameEdit.text,
- PatronymicEdit.text, CityEdit.text);
- readSubscribers(RequiredSubscriber);
- sortList();
- ListLength := SubscriberList.length();
- setlength(NumberArray, ListLength);
- PCurrentSubscriber := SubscriberList.getHeader()^.next;
- while (PCurrentSubscriber <> nil) do
- begin
- SubscribersStringGrid.RowCount := SubscribersStringGrid.RowCount + 1;
- SubscribersStringGrid.Cells[0, i] := intToStr(i);
- SubscribersStringGrid.Cells[1, i] := PCurrentSubscriber^.telephoneNumber;
- SubscribersStringGrid.Cells[2, i] := PCurrentSubscriber^.surname;
- SubscribersStringGrid.Cells[3, i] := PCurrentSubscriber^.name;
- SubscribersStringGrid.Cells[4, i] := PCurrentSubscriber^.patronymic;
- SubscribersStringGrid.Cells[5, i] := PCurrentSubscriber^.city;
- SubscribersStringGrid.Cells[6, i] := intToStr(PCurrentSubscriber^.numberOfDays);
- NumberArray[j] := PCurrentSubscriber^.number;
- Inc(i);
- inc(j);
- PCurrentSubscriber := PCurrentSubscriber^.next;
- end;
- end;
- procedure TEditForm.getRequiredSubscriber(var RequiredSubscriber: TRequiredSubscriber; RequiredTelephoneNumber: string; RequiredSurname: string;
- RequiredName: string; RequiredPatronymic: string;
- RequiredCity: string);
- begin
- RequiredSubscriber.telephoneNumber := RequiredTelephoneNumber;
- RequiredSubscriber.surname := RequiredSurname;
- RequiredSubscriber.name := RequiredName;
- RequiredSubscriber.patronymic := RequiredPatronymic;
- RequiredSubscriber.city := RequiredCity;
- end;
- procedure TEditForm.clearStringGrid();
- begin
- SubscribersStringGrid.RowCount := 1;
- SubscribersStringGrid.ColCount := 7;
- end;
- procedure TEditForm.DeleteButtonClick(Sender: TObject);
- var
- SubscriberNumber: integer;
- begin
- SubscriberNumber := NumberArray[SubscribersStringGrid.Row - 1];
- deleteSubscriber(SubscriberNumber);
- ShowButtonClick(Sender);
- end;
- procedure TEditForm.EditButtonClick(Sender: TObject);
- begin
- SubscriberIndex := NumberArray[SubscribersStringGrid.Row - 1];
- EditForm.Enabled := false;
- CorrectForm.Show;
- end;
- function TEditForm.IsCorrectTextData(CurrentEdit: TEdit): boolean;
- var
- IsCorrectInput: boolean;
- TextLength, i: integer;
- begin
- isCorrectInput := false;
- TextLength := length(CurrentEdit.text);
- i := 2;
- if TextLength <= CurrentEdit.MaxLength then
- begin
- if TextLength = 0 then
- IsCorrectInput := true
- else
- begin
- if (TextLength = 1) and (CurrentEdit.text[1] in BIG_LETTERS) then
- IsCorrectInput := True
- else
- begin
- IsCorrectInput := CurrentEdit.text[1] in BIG_LETTERS;
- while IsCorrectInput and (i <= TextLength) do
- begin
- if not(CurrentEdit.text[i] in SMALL_LETTERS) then
- IsCorrectInput := false;
- inc(i);
- end;
- end;
- end;
- end;
- Result := IsCorrectInput;
- end;
- function TEditForm.isCorrectNumberData(CurrentEdit: TEdit): boolean;
- var
- IsCorrectInput: boolean;
- TextLength, i: integer;
- begin
- isCorrectInput := false;
- TextLength := length(CurrentEdit.text);
- i := 2;
- if TextLength <= CurrentEdit.MaxLength then
- begin
- if TextLength = 0 then
- IsCorrectInput := true
- else
- begin
- if (TextLength = 1) and ((CurrentEdit.text[1] in DIGITS) or (CurrentEdit.text[1] = '+')) then
- isCorrectInput := true
- else
- begin
- IsCorrectInput := (CurrentEdit.text[1] in DIGITS) or (CurrentEdit.text[1] = '+');
- while isCorrectInput and (i <= TextLength) do
- begin
- if not (CurrentEdit.text[i] in DIGITS) then
- IsCorrectInput := false;
- inc(i);
- end;
- end;
- end;
- end;
- Result := IsCorrectInput;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement