Link to home
Start Free TrialLog in
Avatar of greenfly
greenfly

asked on

B-Lists

Can you help me to finish my "Book For Sale" Program using pointers and link list?.

I have written this program in Pascal but I am having problems writing a Procedures for each of the following codes: Can you please provide four procedures for each of the problem below.

(I wrote two other procedures but they both kept freezing up my computer when run that I had to reboot, and so I got rid of them):
 

1) Display all information about a specific book when entered the year published, and returned a 'NOT FOR SALE' if book is not found in the list.

2) Repeat viewing all book that satisfy the search condition without having to go back to the main menu.

3) Books should be search and sorted in ascending order by year of publication.

4) Finally it should be possible to display a list of books sold, eg, all books selected by customers.

I have written most of the code already and it works, but I have to take out the code for the 4 Procedures above because I cant get them to work properly.

The screen menu should be

Books For Sale
---------------
1 -  Initialise the Book    
2 -  View Available Books    
3 -  Add New Book To List  
4 -  Display All Books Sold
5 -  Search List for Books  
0 -  Quit Program:

The working codes that I have written so far is shown below and must be written in pascal. I need the procedurs for the four things above to be added.

Program BookListData(input, output);

    Uses
       wincrt;
    const
       space= ' ';
       Type
       link             = ^booknode;
       booknode         = RECORD
       Pages, Year      : integer;
       Author           : string;
       Publisher, Name  : string;
       SalePrice, Size  : string;
       BriefDescription : string;
       next             : link;
       end;

     Var
       Book,tail        : link;
       Prev,head        : link;
       Selection        : 0..4;
       ch               : char;
       BookName         : string;
       YearOfBook       : integer;
       Answer           : char;
       count            : integer;

  Procedure Menu;{display the main menu}
  begin
   writeln;
   
   writeln(space:15,'============================');
   writeln(space:15,'| -- BOOK DISPLAY MENU --  |');
   writeln(space:15,'============================');
   writeln(space:15,'1 -  Initialise the Book   ');
   writeln(space:15,'2 -  View Available Books   ');
   writeln(space:15,'3 -  Add New Book To List  ');
   writeln(space:15,'4 -  Display All Books Sold ');
   writeln(space:15,'5 -  Search List for Books  ');
    writeln;
     writeln(space:15,'0 -  Quit Program: ');
     writeln;
    write(space:15,'Choose your Selection (1,2,3 or 0) >: ');
    readln(Selection);
   end; {of procedure main menu}

 Procedure InitialiseBook;
 {beginning of procedure initialise}
 begin
     count:=0;
       writeln;
        writeln(space:15,'The bookcase is empty but ready for books  ');
        writeln;
        write(space:15,'Press <Y> to return to Main Menu : ');
      readln(ch);
     writeln;
   Clrscr;
  end; {of procedure Initialise}
    {-----------------------------------------------------------}
 procedure Deletebook;
 var current, prev, P: link;
   begin
     if current^.next=book then
     begin
    book:=book^.next;
   prev^.next:=book;
  end;
end;

 Procedure View;{beginning of procedure display}
 var
     ch  :char;
 begin
   writeln;
    writeln(space:10,'!!!!--  LIST OF BOOKS FOR SALE -!!!!: ');
     writeln(space:10,'**************************************');
     if count=0 then writeln(space:10,'No Books for sale')
     else
     Begin
          Book:=Head;
          Prev:=Tail;
    writeln(space:10,'                           ');
   Repeat
   write(space:10,'NAME OF BOOK >: ', book^.Name);
   writeln;
   writeln;
   writeln(space:10,'AUTHOR       >: ', book^.Author);
   writeln;
   writeln(space:10,'PUBLISHER    >: ', book^.Publisher);
   writeln;
   writeln(space:10,'YEAR         >: ',  book^.Year);
   writeln;
   writeln(space:10,'PAGES        >: ', book^.pages);
   writeln;
   writeln(space:10,'SIZE         >: ', book^.size);
   writeln;
   writeln(space:10,'SALE PRICE   >: ', book^.SalePrice);
   writeln;
   writeln(space:10,'Brief Introduction: ', book^.briefdescription);
   writeln;
     writeln;writeln; writeln;writeln;writeln;
     write(space:10,'Do you wish to purchase this book (Y/N)>  ');
     write;
      read(ch);
      readln;
       IF UPCASE(CH)= 'Y' THEN
         begin
          if book=Head then Head:=Head^.next;
            prev^.next:=book^.next;
              book:=prev^.next;
              count:=count-1;
              Writeln(space:10,'!! book sold and deleted from list !!');
             end
            else
          Begin
             prev:=book;
             book:=book^.next;
            End;
          Writeln;
          writeln;
         write(SPACE:10,'Do you wish to view the next book (Y/N) >');
        read(ch);
       readln;
      Until (upcase(ch)='N') or (count=0);
      ClrScr;
     end;
   end;

  Procedure Add; {beginning of procedure add}
 Begin
 Repeat
  writeln;
   writeln(space:15,'!! ADD A NEW BOOK TO LIST !!');
    writeln(space:15,'****************************');
    writeln;
     new(Book);
     write(space:15,'Book Name      :  '  );
      readln(Book^.Name);
       writeln;
         write(space:15,'Author Name    :  ');
          readln(Book^.Author);
           writeln;
            write(space:15,'Publisher      :  ');
             readln(book^.Publisher);
             writeln;
              write(space:15,'Year           :  ');
              readln(Book^.Year);
              writeln;
              write(space:15, 'Pages          :  ');
              readln(Book^.Pages);
             writeln;
             write(space:15, 'Size           :  ');
            readln(book^.Size);
           writeln;
          write(space:15, 'Sale Price     :  ');
         readln(book^.SalePrice);
        writeln;
      write(space:15, 'Brief Description:  ');
     readln(book^.BriefDescription);
    if count=0 then
    Begin
       Head:=Book;
       Tail:=Head;
       Tail^.next:=nil;
       count:=count+1;
    End
    else
    Begin
       Tail^.next:=Book;
        Tail:=Book;
         Tail^.next:=Head;
         count:=count+1;
         End;
        Writeln;
        writeln(space:15,'!! New Book Successfully added to list !!');
       writeln;
      write(space:15,'Do you wish to add more books? (Y/N) >');
     read(answer);
     readln;
    ClrScr;
   Until Upcase(Answer)='N';
  ClrScr;
  END;
 
Procedure Search(Tail : link);{beginning of procedure search}
var
  found       : boolean;
  current     : link;
  ch          : char;
  YearOfBook  : integer;
  Year        : integer;
 begin
   writeln;
   writeln(space:15,'!! SEARCH FOR A BOOK !!');
   writeln(space:15,'**************************');
    write (space:10,'Please enter the year of the book (1900 - 2000 >: ');
     readln (YearOfBook);
     writeln;
      current  := Tail;
      found    := false;
       while (current <> nil) and (not found) do
        begin { of while }
         if (current^.Year) = YearOfBook then
         begin { of if }
         found  := true;
         Begin
        writeln    (space:15,'The Book Published in ', YearOfBook,' is found');
        writeln;
          end;
         {writeln    (space:15,'The Book Published in', YearOfBook,' is found in the book')}
         end  { of if }
         else
        begin
       current := current^.next;
      end;
      end; { of while }
     if  not found then
     writeln(space:15,'Sorry no book published in ',YearOfBook,'  for sale');
    writeln;
   write(space:15,'Press <Y> to return to main menu : ');
   readln(ch);
  Clrscr;
  end; { of procedure search }

  begin {Main Program or action block}
 
   repeat
      menu;
      CASE Selection OF
         1  : InitialiseBook;
         2  : View;
         3  : Add;
         {4  : Display;}
         5  : Search(Tail);


        END;

      UNTIL Selection = 0;
    writeln;
   writeln (space:15,'!! GOOD BYE - Thanks for using this Program !! ');
   end.{of main program}





ASKER CERTIFIED SOLUTION
Avatar of sumant032199
sumant032199
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of greenfly
greenfly

ASKER

Thanks a lot for your solution to my Program. You deserved full marks for your effort, Yes grade "A" it is. However there are two problem that I have encountered with your codes listed below, I hope you can help me to solve them. In the mean time I have accepted your solution.


1)I am fairly new to pascal programming so I do not know what is "BREAK". This is used in the 'Procedure Display'for deleted records. The program wont compile with 'BREAK'in the "IF" statement. The program compiles normally when I removed it, but when I view a book and purchased it, it continuosly asking me if I want to buy and view the next book after selecting NO to both option.  

2) The Program crashes when a book searched for is not found. After the prompt "Press <Y> to continue, pressing 'Y' causes the program to crash.

An urgent reply would be appreciated.

With  Many Thanks