Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- procedure TFormAddComp.SortArr();
- var
- Left, Right, I: Integer;
- begin
- Left := 0;
- Right := Length(FormComps.ArrComp) - 1;
- Repeat
- for I := Left to Right - 1 do
- if FormComps.ArrComp[I].Price > FormComps.ArrComp[I + 1].Price then
- begin
- FormComps.Temp := FormComps.ArrComp[I];
- FormComps.ArrComp[I] := FormComps.ArrComp[I + 1];
- FormComps.ArrComp[I + 1] := FormComps.Temp;
- end;
- Dec(Right);
- for I := Right downto Left + 1 do
- if FormComps.ArrComp[I].Price < FormComps.ArrComp[I - 1].Price then
- begin
- FormComps.Temp := FormComps.ArrComp[I];
- FormComps.ArrComp[I] := FormComps.ArrComp[I - 1];
- FormComps.ArrComp[I - 1] := FormComps.Temp;
- end;
- Inc(Left);
- Until (Left >= Right);
- end;
- procedure AddCompInArr();
- var
- SizeArr: Integer;
- begin
- SizeArr := Length(FormComps.ArrComp) + 1;
- SetLength(FormComps.ArrComp, SizeArr);
- FormComps.ArrComp[SizeArr - 1].NameComp := FormAddComp.EditNameOfComp.Text;
- FormComps.ArrComp[SizeArr - 1].Manufacturer := FormAddComp.EditNameOfManufacturer.Text;
- FormComps.ArrComp[SizeArr - 1].MainChar := FormAddComp.EditMainChar.Text;
- FormComps.ArrComp[SizeArr - 1].Warranty :=
- StrToFloat(FormAddComp.EditWarranty.Text);
- FormComps.ArrComp[SizeArr - 1].Price :=
- StrToFloat(FormAddComp.EditPrice.Text);
- end;
- Procedure TFormAddComp.AddArrInStringGrid();
- var
- Size, I: Integer;
- begin
- Size := Length(FormComps.ArrComp);
- FormComps.StringGrid1.RowCount := Size + 1;
- if FormComps.StringGrid1.RowCount > 1 then
- FormComps.StringGrid1.Cells[0, Size] := ' ' + IntToStr(Size);
- for I := 1 to Size do
- begin
- FormComps.StringGrid1.Cells[1, I] := FormComps.ArrComp[I - 1].NameComp;
- FormComps.StringGrid1.Cells[2, I] := FormComps.ArrComp[I - 1].Manufacturer;
- FormComps.StringGrid1.Cells[3, I] := FormComps.ArrComp[I - 1].MainChar;
- FormComps.StringGrid1.Cells[4, I] :=
- FloatToStr(FormComps.ArrComp[I - 1].Warranty);
- FormComps.StringGrid1.Cells[5, I] :=
- FloatToStr(FormComps.ArrComp[I - 1].Price) + '$';
- end;
- end;
- procedure CorrectCompInArr();
- var
- Line: Integer;
- Warranty, Price: Double;
- begin
- Warranty := StrToFloat(FormAddComp.EditWarranty.Text);
- Price := StrToFloat(FormAddComp.EditPrice.Text);
- Line := FormComps.StringGrid1.Row - 1;
- if (Warranty < 101) and (Warranty > 1) and (Price < 9999) then
- begin
- FormComps.ArrComp[Line].NameComp := FormAddComp.EditNameOfComp.Text;
- FormComps.ArrComp[Line].Manufacturer := FormAddComp.EditNameOfManufacturer.Text;
- FormComps.ArrComp[Line].MainChar := FormAddComp.EditMainChar.Text;
- FormComps.ArrComp[Line].Warranty := Warranty;
- FormComps.ArrComp[Line].Price := Price;
- end
- else
- begin
- MessageDlg('Ошибка ввода. Повторите попытку.', mtError,
- [mbOK], 0);
- end;
- end;
- procedure TFormAddComp.ButtonEditCompClick(Sender: TObject);
- begin
- CorrectCompInArr();
- AddArrInStringGrid();
- FormAddComp.Close;
- FormComps.Enabled := True;
- end;
- procedure TFormAddComp.ButtonCreateCompClick(Sender: TObject);
- var
- Warranty, Price: Double;
- IsCorrect: Boolean;
- begin
- IsCorrect := true;
- try
- Warranty := StrToFloat(EditWarranty.Text);
- except
- MessageDlg('Ошибка ввода гарантии. Повторите попытку.', mtError,
- [mbOK], 0);
- IsCorrect := false;
- end;
- try
- Price := StrToFloat(EditPrice.Text);
- except
- MessageDlg('Ошибка ввода цены. Повторите попытку.', mtError,
- [mbOK], 0);
- IsCorrect := false;
- end;
- if IsCorrect then
- Begin
- if (Warranty < 100) and (Warranty > 0) then
- begin
- if (Price < 100000) then
- Begin
- AddCompInArr();
- AddArrInStringGrid();
- FormComps.NSaveFile.Enabled := True;
- FormComps.ButtonClean.Enabled := True;
- FormAddComp.Close;
- FormComps.Enabled := True;
- End
- else
- begin
- MessageDlg('Ошибка ввода, цена не должна превышать 99999$.' + #10 +
- 'Повторите попытку.', mtError, [mbOK], 0);
- EditPrice.Text := '';
- end
- end
- else
- begin
- MessageDlg('Ошибка ввода, гарантия не должна превышать 99 лет. Повторите попытку.', mtError,
- [mbOK], 0);
- EditWarranty.Text := '';
- end;
- End;
- end;
- procedure TFormAddComp.DeleteComp(Line: Integer);
- var
- I, Size: Integer;
- begin
- for I := Line to High(FormComps.ArrComp) do
- begin
- FormComps.Temp := FormComps.ArrComp[I];
- FormComps.ArrComp[I] := FormComps.ArrComp[I - 1];
- FormComps.ArrComp[I - 1] := FormComps.Temp;
- end;
- Size := Length(FormComps.ArrComp) - 1;
- SetLength(FormComps.ArrComp, Size);
- end;
- procedure TFormAddComp.ButtonDeleteClick(Sender: TObject);
- var
- Line: Integer;
- begin
- Line := FormComps.StringGrid1.Row;
- if Line <> 0 then
- begin
- if MessageDlg('Вы действительно хотите удалить компонент номер ' +
- IntToStr(Line), mtWarning, [mbYes, mbNo], 0) = mrYes then
- begin
- DeleteCOmp(Line);
- FormAddComp.AddArrInStringGrid();
- FormAddComp.Close;
- end;
- end;
- if FormComps.StringGrid1.RowCount = 1 then
- begin
- FormComps.ButtonClean.Enabled := False;
- FormComps.NSaveFile.Enabled := False;
- end;
- end;
- procedure TFormAddComp.EditMainCharKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Length(EditMainChar.Text) > 15) and not(Key = #8) then
- Key := #0;
- end;
- procedure TFormAddComp.EditNameOfManufacturerKeyPress(Sender: TObject;
- var Key: Char);
- begin
- if (Length(EditNameOfManufacturer.Text) > 15) and not(Key = #8) then
- Key := #0;
- end;
- procedure TFormAddComp.EditNameOfCompKeyPress(Sender: TObject; var Key: Char);
- begin
- if (Length(EditNameOfComp.Text) > 15) and not(Key = #8) then
- Key := #0;
- end;
- procedure TFormAddComp.EditChange(Sender: TObject);
- begin
- ButtonCreateComp.Enabled := (EditNameOfComp.Text <> '') and
- (EditNameOfComp.Text <> '') and (EditMainChar.Text <> '') and
- (EditNameOfManufacturer.Text <> '') and
- (EditWarranty.Text <> '') and
- (EditPrice.Text <> '');
- end;
- procedure TFormAddComp.EditWarrantyKeyPress(Sender: TObject; var Key: Char);
- begin
- if Key = '.' then
- key := ',';
- if not(Key in ['0' .. '9' ,',' ,#8]) then
- Key := #0;
- if (Length(EditWarranty.Text) > 2) and not(Key = #8) then
- Key := #0;
- end;
- procedure DeleteComp(Line: Integer);
- var
- I, Size: Integer;
- begin
- for I := Line to High(FormComps.ArrComp) do
- begin
- FormComps.Temp := FormComps.ArrComp[I];
- FormComps.ArrComp[I] := FormComps.ArrComp[I - 1];
- FormComps.ArrComp[I - 1] := FormComps.Temp;
- end;
- Size := Length(FormComps.ArrComp) - 1;
- SetLength(FormComps.ArrComp, Size);
- end;
- procedure TFormComps.ButtonCleanClick(Sender: TObject);
- var
- Line: Integer;
- begin
- with StringGrid1 do
- begin
- StringGrid1.RowCount := 0;
- DeleteComp(Line);
- end;
- if StringGrid1.RowCount = 1 then
- begin
- ButtonClean.Enabled := False;
- NSaveFile.Enabled := False;
- SortButton.Enabled := false;
- end;
- end;
- procedure TFormComps.ButtonCreateClick(Sender: TObject);
- begin
- FormAddComp.ButtonCreateComp.Visible := True;
- FormAddComp.ButtonEditComp.Visible := False;
- FormAddComp.ButtonDelete.Visible := False;
- FormAddComp.Show;
- FormComps.Enabled := False;
- FormComps.SortButton.Enabled := True;
- end;
- procedure TFormComps.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- case MessageDlg('Действительно хотите выйти?. Все данные будут утеряны',
- mtWarning, [mbYes, mbNo], 0) of
- mrNo:
- begin
- CanClose := False;
- end;
- mrYes:
- begin
- FormComps.StringGrid1.RowCount := 1;
- SetLength(FormComps.ArrComp, 0);
- FormMain.Enabled := True;
- end;
- else
- CanClose := False;
- end
- end;
- procedure TFormComps.FormCreate(Sender: TObject);
- begin
- StringGrid1.ColWidths[0] := 60;
- StringGrid1.Cells[0, 0] := ' №';
- StringGrid1.Cells[1, 0] := 'Название';
- StringGrid1.Cells[2, 0] := 'Производитель ';
- StringGrid1.Cells[3, 0] := 'Характеристика';
- StringGrid1.Cells[4, 0] := 'Гарантия';
- StringGrid1.Cells[5, 0] := 'Цена';
- end;
- procedure FillArr();
- var
- Line: Integer;
- begin
- Line := FormComps.StringGrid1.Row - 1;
- FormAddComp.EditNameOfComp.Text := FormComps.ArrComp[Line].NameComp;
- FormAddComp.EditNameOfManufacturer.Text := FormComps.ArrComp[Line].Manufacturer;
- FormAddComp.EditMainChar.Text := FormComps.ArrComp[Line].MainChar;
- FormAddComp.EditWarranty.Text :=
- FloatToStr(FormComps.ArrComp[Line].Warranty);
- FormAddComp.EditPrice.Text :=
- FloatToStr(FormComps.ArrComp[Line].Price);
- end;
- Function IsCorrectSizeFile(Address: String): Boolean;
- Var
- IsCorrect: Boolean;
- Begin
- AssignFile(FormComps.FileComp, Address);
- Reset(FormComps.FileComp);
- If (EoF(FormComps.FileComp)) Then
- Begin
- MessageDlg('Файл пустой', mtError, [mbOK], 0);
- IsCorrect := False;
- End
- Else
- IsCorrect := True;
- Close(FormComps.FileComp);
- IsCorrectSizeFile := IsCorrect;
- End;
- Function IsFileExist(Address: String): Boolean;
- Var
- IsCorrect: Boolean;
- Begin
- If FileExists(Address) Then
- IsCorrect := True
- Else
- Begin
- IsCorrect := False;
- MessageDlg('Указанного файла не существует', mtError, [mbOK], 0);
- End;
- IsFileExist := IsCorrect;
- End;
- Procedure ReadFile(Address: String);
- var
- Rows, I: Integer;
- Begin
- Rows := 1;
- I := 0;
- FormComps.StringGrid1.RowCount := 1;
- AssignFile(FormComps.FileComp, Address);
- Reset(FormComps.FileComp);
- while not(EoF(FormComps.FileComp)) do
- begin
- Inc(Rows);
- FormComps.StringGrid1.RowCount := Rows;
- FormComps.StringGrid1.Cells[0, Rows - 1] := ' ' + IntToStr(Rows - 1);
- SetLength(FormComps.ArrComp, I + 1);
- Read(FormComps.FileComp, FormComps.ArrComp[I]);
- FormComps.StringGrid1.Cells[1, I + 1] := FormComps.ArrComp[I].NameComp;
- FormComps.StringGrid1.Cells[2, I + 1] := FormComps.ArrComp[I].Manufacturer;
- FormComps.StringGrid1.Cells[3, I + 1] := FormComps.ArrComp[I].MainChar;
- FormComps.StringGrid1.Cells[4, I + 1] :=
- FloatToStr(FormComps.ArrComp[I].Warranty);
- FormComps.StringGrid1.Cells[5, I + 1] :=
- FloatToStr(FormComps.ArrComp[I].Price) + '$';
- Inc(I);
- end;
- CloseFile(FormComps.FileComp);
- End;
- procedure TFormComps.N5Click(Sender: TObject);
- begin
- ShowMessage
- ('Данная программа показывает данные о комплектующих компьютера.'
- + #10 + #10 + 'Кнопка "Очистить таблицу" удлалит данные всех строк.'
- + #10 + 'Двойное нажатие на строку позволяет изменить выделенный компонент.'
- + #10 + 'Кнопка "Убрать некоторые элементы" уберёт те элементы,'
- + #10 + 'гарантия которых больше 2 лет,остальные будут отсортированы по цене.');
- end;
- procedure TFormComps.NOpenFileClick(Sender: TObject);
- var
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := True;
- if OpenDialog1.Execute then
- begin
- IsCorrect := IsFileExist(OpenDialog1.FileName) and
- IsCorrectSizeFile(OpenDialog1.FileName);
- if IsCorrect then
- begin
- ReadFile(OpenDialog1.FileName);
- FormComps.ButtonClean.Enabled := True;
- FormComps.SortButton.Enabled := True;
- FormComps.NSaveFile.Enabled := True;
- FormComps.Show;
- FormMain.Enabled := False;
- end;
- end;
- until IsCorrect;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement