unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    btn2: TButton;
    btn1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

Type TFactory=Record
      Nman:integer;
      Number:Integer;
      Name:string[50];
      end;
     var
     FileFac: file of TFactory;
     FileFacT:TextFile;
     Filename,FileNameT:string;
     zap:Integer;
     station:array[1..30] of TFactory;
var
  Form1: TForm1;




implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Clear;
Edit2.Clear;
Edit3.Clear;
Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Title :='Создать новый файл';
 if OpenDialog1.Execute then
   begin
    FileName:= OpenDialog1.FileName;
    AssignFile(FileFac, FileName);
     Rewrite(FileFac);
   end;

end;

procedure TForm1.btn1Click(Sender: TObject);

begin
  zap:=zap+1;
  with station[zap] do
  begin
  nman:=StrToInt(Edit1.text);
  number:=StrToInt(Edit2.text);
  name:=Edit3.Text;
  Memo1.Lines.Add('zap : '+InttoStr(zap)+'  NumberOfStation : '+InttoStr(nman)+'  NumberOfProduct : '+InttoStr(number)+'  NameOfProduct : '+name);
  end;
  write(FileFac,station[zap]);
  Edit1.Clear;   Edit2.Clear;   Edit3.Clear
end;

procedure TForm1.Button4Click(Sender: TObject);
var i:Integer;
begin
If SaveDialog1.Execute then
begin
  FilenameT:=SaveDialog1.Filename;
  AssignFile(FileFacT,FilenameT);
  Rewrite(FileFacT);
end;
  For i:=1 to zap do
  with station[i] do
  Writeln(FileFacT,i:3,Nman:6,Number:6,Name);
  Closefile(FileFacT);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
 CloseFile(FileFac);
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
 OpenDialog1.Title :='Open File';
 if OpenDialog1.Execute then
   begin
    FileName:= OpenDialog1.FileName;
    AssignFile(FileFac, FileName);
    Reset(FileFac);
    end;
    zap:=0;
  while not eof(filefac) do begin
   zap:=zap+1;
     Read(filefac,station[zap]);
         with station[zap] do
         Memo1.Lines.Add('zap : '+InttoStr(zap)+'  NumberOfStation : '+InttoStr(nman)+'  NumberOfProduct : '+InttoStr(number)+'  NameOfProduct : '+name);
  end;

end;

procedure TForm1.Button3Click(Sender: TObject);
var p,R,i:Integer;
begin
 If Edit1.Text='' then
  ShowMessage('введите в поле Number of Station номер цеха')
  else begin
 Repeat
   R:=0;
   for i:=1 to zap-1 do begin
      if station[i].Nman=strtoInt(Edit1.Text) then
      if station[i].Number<station[i+1].Number then
        begin
          p:=station[i+1].Number;
          station[i+1].Number:=station[i].Number;
          station[i].Number:=p;
          R:=R+1;
          Memo1.Lines.Add('p='+InttoStr(p));
        end;

   end;
   if (R<>0)and (i=zap-1) then
   i:=1;
 until R=0;
 Memo1.Clear;
for i:=1 to zap do
  with station[i] do
  If Nman=strtoInt(Edit1.Text) then
  Memo1.Lines.Add('NumberOfStation : '+InttoStr(nman)+'  NumberOfProduct : '+InttoStr(number)+'  NameOfProduct : '+name);

end;
end;

end.
