Recent

Author Topic: help for my library management program  (Read 14834 times)

kevin73

  • New Member
  • *
  • Posts: 27
help for my library management program
« on: July 23, 2016, 04:20:48 am »
Hello ,
I was asked to write a program to manage a library that is :
1-to add a author .
2- to consult a book through his ISBN number .
3- delete a book.
4 - to delete an author if he hasn't any book

i began with :
Code: Pascal  [Select][+][-]
  1. type book = record
  2.         num_isbn:string[20];
  3.         title:string[20];
  4.         end;
  5.  
  6. bookee = array[1..99]of book;
  7.  
next

Code: Pascal  [Select][+][-]
  1. type person = record
  2.         num_id:integer;
  3.         name:string[15];
  4.         nation:string[20];
  5.         n:integer;
  6.         list_ouvr:bookee;
  7.         end;

for my file and my variables
Code: Pascal  [Select][+][-]
  1.      fp=file of person;
  2.  
  3. var  author:array[1..99] of person;
  4.      r:fp;
  5.  

i created a procedure to create my libray
Code: Pascal  [Select][+][-]
  1. procedure add(var f:fp);
  2. var r,i,j:integer;
  3. begin
  4.      assign(f,'autheur.dat');
  5.      rewrite(f);
  6.      writeln('how many of records');
  7.      readln(r);
  8.      for i:=1 to r do
  9.          begin
  10.            writeln(i, ' Num ID');
  11.            readln(author[i].num_id);
  12.  
  13.            writeln(i, ' Name');
  14.            readln(author[i].name);
  15.  
  16.            writeln(i, ' Nation');
  17.            readln(author[i].nation);
  18.  
  19.            writeln('how many books?');
  20.            readln(author[i].n);
  21.  
  22.                     for j:=1 to author[i].n do
  23.                         begin
  24.                           writeln('give his ISBN number ');
  25.                           readln(author[i].list_ouvr[j].num_isbn);
  26.  
  27.                           writeln('give the title ');
  28.                           readln(author[i].list_ouvr[j].title);
  29.                           write(f,author[i]);
  30.                         end;
  31.  
  32.          end;
  33.          close(f);
  34. end;        
  35.  

my procedure to display
Code: Pascal  [Select][+][-]
  1. procedure see(var f:fp);
  2. var j:integer;
  3.      isbn:string[20];
  4. begin
  5.      reset(f);
  6.      j:=1;
  7.      while not (EOF(f)) do
  8.            begin
  9.              read(f,author[j]);
  10.              writeln(j,' .Name:', author[j].name );
  11.              writeln('Number :', author[j].list_ouvr[j].num_isbn ,' title :', author[j].list_ouvr[j].title);
  12.              inc(j);
  13.            end;
  14.      close(f);
  15.      readln;
  16. end;
  17.  

but I face a problem to remove the work without affecting the name of the author and I would avoid using temporary files to delete. Someone could help me ?
thank you

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: help for my library management program
« Reply #1 on: July 23, 2016, 08:59:43 am »
It would be less error-prone to use a database (TBufDataset, sqlite, TDBF are local databases all available in Lazarus out-of-the-box) with already proven routines for addition, deletion and editing of records than to roll your own database engine, however simple, unless you really need to do that as a learning exercise.

kevin73

  • New Member
  • *
  • Posts: 27
Re: help for my library management program
« Reply #2 on: July 23, 2016, 09:22:03 am »
Someone has  idea?

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: help for my library management program
« Reply #3 on: July 23, 2016, 12:02:16 pm »
... I would avoid using temporary files to delete. Someone could help me ?
thank you

Usually, I will use a temporary file. But without using a temporary file is still possible. I will give the drafts both with and without using temporary file. You have to code it yourself.

Deleting authors if he hasn't any book using temporary file:
1. Open the source file
2. Get the total record count of the source file
3. Close the source file and exit if (total record count <= 0)
4. Create a temporary file
5. For i := 1 to total record count
6.      Read the source file
7.      If (book count >= 1) then save it to temporary file
8. End of For
9. Close temporary file
10. Close source file
11. Delete source file
12. Rename temporary file to source file

Deleting authors if he hasn't any book without using temporary file:
1. Open the file
2. Get the total record count of the file
3. Close the file and exit if (total record count <= 0)
4. Prepare memory as much as total record count
5. Variable AuthorsCount := 0
6. For i := 1 to total record count
7.      Read the file
8.      If (book count >= 1) then save it memory and Inc(AuthorsCount)
9. End of For
10. Close the file
11. Delete the file
12. Create a new file
13. For i := 1 to AuthorsCount
14.      Save the memory [ i ] to file
15. End of For
16. Close the file

You can choose one from those above. But you still have to write module for deleting a book.

Good luck and happy weekend.
« Last Edit: July 23, 2016, 01:09:37 pm by Handoko »

kevin73

  • New Member
  • *
  • Posts: 27
Re: help for my library management program
« Reply #4 on: July 24, 2016, 07:18:05 am »
here's what I did to add to book

First I ask the author identification number then I search the file with a "while" loop if I find a recording, so I incremented the number of book he had before, and I ' saves the new book, but to position +1 of the number of book he had

Code: Pascal  [Select][+][-]
  1. procedure add_book(var f:fp);
  2. var
  3.      id,e,j,d:integer;
  4. begin
  5.      writeln('write the id number of the author: ');
  6.      readln(id);
  7.      assign (f, 'auteur.dat');
  8.      reset(f);
  9.      j:=1;
  10.      while not (EOF(f)) do
  11.            begin
  12.  
  13.              read(f,author[j]);
  14.              if ( author[j].num_id = id) then
  15.                 begin
  16.  
  17.                           author[j].n := author[j].n +1;
  18.                           e:= author[j].n;
  19.  
  20.                           writeln(' write the id number of new ebook');
  21.                           readln(author[j].list_ouvr[e].num_isbn);
  22.  
  23.                           writeln(' write  the title');
  24.                           readln(author[j].list_ouvr[e].title);
  25.                          
  26.                           write(f,author[j]);
  27.                           inc(j);
  28.                end;
  29.            end;
  30.      if j = 1 then writeln(' Nothing ');
  31.      close(f);
  32. end;
  33.  
  34.  

but I do not understand why the number of books are not displayed correctly when I appear with the procedure "see"
normally if he had 2 ebooks when I add one book that should be 3
« Last Edit: July 24, 2016, 07:25:08 am by kevin73 »

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: help for my library management program
« Reply #5 on: July 24, 2016, 07:33:28 am »
Because you didn't provide the whole source code for me to test, I just can eye checking what you've provided.

Can you please attach the auteur.dat file with some data you have inputted?

kevin73

  • New Member
  • *
  • Posts: 27
Re: help for my library management program
« Reply #6 on: July 24, 2016, 07:37:03 am »
ok let me 2 min

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: help for my library management program
« Reply #7 on: July 24, 2016, 07:47:27 am »
I'm away, will be back some hours later.

kevin73

  • New Member
  • *
  • Posts: 27
Re: help for my library management program
« Reply #8 on: July 24, 2016, 07:49:25 am »
my file
« Last Edit: July 24, 2016, 11:07:31 am by kevin73 »

bytebites

  • Hero Member
  • *****
  • Posts: 639
Re: help for my library management program
« Reply #9 on: July 24, 2016, 08:33:28 am »
You need another loop to iterate the books.

kevin73

  • New Member
  • *
  • Posts: 27
Re: help for my library management program
« Reply #10 on: July 24, 2016, 08:38:28 am »
you could be a little more specific. What line? or

bytebites

  • Hero Member
  • *****
  • Posts: 639
Re: help for my library management program
« Reply #11 on: July 24, 2016, 08:58:06 am »
Code: Pascal  [Select][+][-]
  1. procedure see(var f:fp);
  2. var j:integer;
  3.      isbn:string[20];
  4. begin
  5.      reset(f);
  6.      j:=1;
  7.      while not (EOF(f)) do
  8.            begin
  9.              read(f,author[j]);
  10.              writeln(j,' .Name:', author[j].name );
  11.              writeln('Number :', author[j].list_ouvr[j].num_isbn ,' title :', author[j].list_ouvr[j].title);
  12.              inc(j);
  13.            end;
  14.      close(f);
  15.      readln;
  16. end;
  17.  

j variable is for author in line 11.
now it prints only one book form each author.
« Last Edit: July 24, 2016, 09:00:46 am by bytebites »

kevin73

  • New Member
  • *
  • Posts: 27
Re: help for my library management program
« Reply #12 on: July 24, 2016, 09:35:10 am »
so correct ?
Code: Pascal  [Select][+][-]
  1.  
  2.     procedure see(var f:fp);
  3.     var j,d:integer;
  4.          isbn:string[20];
  5.     begin
  6.          reset(f);
  7.          j:=1;
  8.          while not (EOF(f)) do
  9.                begin
  10.                  read(f,author[j]);
  11.                  for d:=1 to author[j].n do
  12.                  writeln(j,' .Name:', author[j].name );
  13.                  writeln('Number :', author[j].list_ouvr[j].num_isbn ,' title :', author[j].list_ouvr[j].title);
  14.                  inc(j);
  15.                end;
  16.                end;
  17.          close(f);
  18.          readln;
  19.     end;
  20.      
  21.  
  22.  
  23.  

bytebites

  • Hero Member
  • *****
  • Posts: 639
Re: help for my library management program
« Reply #13 on: July 24, 2016, 10:04:09 am »
Now it prints author name several times and one book (perhaps)

kevin73

  • New Member
  • *
  • Posts: 27
Re: help for my library management program
« Reply #14 on: July 24, 2016, 10:06:12 am »
Yes but i don't know how to fix it

 

TinyPortal © 2005-2018