program lib;
uses crt;
type books=record
     number:integer;
     name:string;
     avtor:string;
          srok:record
          d:byte;
          m:byte;
          end;
     status:string;       {SDANA ILI NET(true or false)}
     end;
var
   book_file : file of books;
         rec : books;
    finder_d : byte;
    finder_m : byte;
   file_name : string;
         key : integer;
        exit : boolean;
           x : string;

{CREATE NAME OF FILE}

procedure name_of_file;
begin
     write('Enter name of file of data of book: ');
     readln(file_name);
end;

{ADD RECORD IN THE FILE}

procedure add_record;
begin
     writeln('Record N ',filepos(book_file)+1);
     with rec do
          begin
               write('Inventory number: ');
               readln(number);
               write('Name of the book: ');
               readln(name);
               write('Author of the book: ');
               readln(avtor);
               write('Srok vozvrata: ');
               readln(srok.d, srok.m);
               write('Status of the book: ');
               readln(status);
               write(book_file,rec);
          end;
end;

{CREATE NEW NULL FILE}

procedure create_new_nul_file;
begin
     name_of_file;
     assign(book_file,file_name);
     rewrite(book_file);
end;

{CREATE NEW FILE}

procedure create_new_book_file;
var
   i,n:integer;
begin
     name_of_file;
     assign(book_file,file_name);
     rewrite(book_file);
     writeln('Create records of file ',file_name);
     write('Enter count records: ');
     readln(n);
     for i:=1 to n do add_record;
     writeln('FILE CREATED');
     writeln('File of data have ',filesize(book_file),' records');
     close(book_file);
end;

{OUTPUT TEMP RECORD}

procedure output_record;
begin
     read(book_file,rec);
     with rec do
          begin
               write('N ',filepos(book_file),' : ');
               writeln('Name of book: ',name);
               writeln('Author of book: ',avtor);
               writeln('Srok vozvrata: ',srok.d,'.',srok.m);
               writeln('Status of book: ',status);
          end;
end;

{OUTPUT ALL RECORDS}

procedure output_all_records;
begin
     name_of_file;
     assign(book_file,file_name);
     {$I-}
     reset(book_file);
     {$I+}
     if IOresult = 0 then
        begin
             seek(book_file,0);
             writeln('OUTPUT INFORMATION ABOUT BOOK ','"',file_name,'"');
             while (not eof(book_file)) do output_record;
        end
     else
         writeln('File '+file_name+' is not');
end;

{REWRITE PARTS OF FILE}

procedure update_records;
var
   number_of_record:integer;
begin
     name_of_file;
     assign(book_file,file_name);
     {$I-}
     reset(book_file);
     {$I+}
     if IOresult = 0 then
        begin
             writeln('Enter number of rewrite record :');
             readln(number_of_record);
             seek(book_file,number_of_record-1);
             writeln('Zna4enie of this record: ');
             output_record;
             seek(book_file,number_of_record-1);
             writeln('Enter new zna4enie ',number_of_record,' record');
             add_record;
             close(book_file);
        end
     else
         writeln('Файла с именем '+file_name+' не существует');
end;

{ADD RECORDS IN THE END OF FILE}

procedure add_records_in_the_end;
begin
     name_of_file;
     assign(book_file,file_name);
     {$I-}
     reset(book_file);
     {$I+}
     if IOresult = 0 then
        begin
             seek(book_file,filesize(book_file));
             add_record;
             writeln('Data is wrote. So ',filesize(book_file),' records');
             close(book_file);
        end
     else
         writeln('File with name '+file_name+' is not');
end;

{SEARCH PROSROCHENNYX BOOKS}

procedure find_book;
var
   book_file2:file of books;
   finder:integer;
   flag:boolean;
   counter:integer;
   d:char;
begin
     name_of_file;
     assign(book_file,file_name);
     writeln('Enter name of file for prosro4ennyx book: ');
     readln(x);
     assign(book_file2,x);
     rewrite(book_file2);
     {$I-}
     reset(book_file);
     {$I+}
     if IOresult = 0 then
        begin
             write('Enter Tekywyu daty: ');
             readln(finder_d, finder_m);
             flag:=false;
             counter:=0;
             while (not eof(book_file)) do
                   begin
                        read(book_file,rec);
                        with rec do
                             if ((srok.d<finder_d) and (srok.m<finder_m)) or
                                ((srok.d>=finder_d) and (srok.m<finder_m)) then
                                begin
                                     flag:=true;
                                     inc(counter);
                                     write('N ',filepos(book_file),' : ');
                                     writeln('Name of book: ',name);
                                     writeln('Author of book: ',avtor);
                                     writeln('Srok vozvrata: ',srok.d,'.',srok.m);
                                     writeln('Status of book: ',status);
                                     write(book_file2,rec);
                                     status:='y';
                                     srok.d:=finder_d;
                                     srok.m:=finder_m+1;
                                end;
                   end;
           {  seek(book_file,0);
              while (not eof(book_file)) do
                   begin
                        read(book_file,rec);
                        with rec do
                             if (srok.d<finder) and (srok.m<finder) then
                                begin
                                     status:='y';
                                     srok:=finder;
                                     srok.m:=srok.m+1;
                                     write(book_file,rec);
                                end;
                   end;}

             if flag then
                begin
                     writeln('Finded ',counter,' records');
 {  writeln('Внести данные записи в новый файл?');
  write('Yes or No');
    readln(d);
 if (d='y') or (d='Y') then
     begin
    create_new_nul_file;
 while (not eof(book_file)) do
   begin
   read(book_file,rec);
   with rec do
    if srok>finder then
           begin            write('N ',filepos(book_file),' : ');
   writeln('Название книги: ',name);
     writeln('Автор книги: ',avtor);
  writeln('Срок возврата: ',srok);
   writeln('Автор книги: ',avtor);
  writeln('Книга сдана: ',status);
    write(file_name,rec);
       end;
      close(name_file);
       end   }
                        end
             else
                 writeln('NO FIND RECORDS');
             close(book_file);
        end
     else
         writeln('File with name '+file_name+' is not');
end;

{=============MAIN PROGRAM==============}

begin
     exit:=false;
     clrscr;
     repeat
           writeln('       DATABASE OF BOOK');
           writeln;
           writeln('1 - CREATE NEW FILE');
           writeln('2 - VIEW INFORMATION ABOUT BOOKS');
           writeln('3 - REWRITE OLD RECORD');
           writeln('4 - ADD NEW RECORDS');
           writeln('5 - SEARCH PROSRO$ENNYX BOOK');
           writeln('6 - EXIT');
           write('Your choose: ');
           readln(key);
           case key of
                1:create_new_book_file;
                2:output_all_records;
                3:update_records;
                4:add_records_in_the_end;
                5:find_book;
                6:exit:=true;
           end;
           writeln('Executed. <ENTER>');
           readln;
           clrscr;
     until exit;
end.