Solved

B-Lists

Posted on 2000-02-27
2
243 Views
Last Modified: 2011-08-18
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}





0
Comment
Question by:greenfly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
sumant032199 earned 200 total points
ID: 2562902
This is the best solution I can offer to you. Are my efforts worth A grade?

Program BookListData(input, output);

    Uses
       crt;
    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;
       delhead,del      : link;
       Selection        : 0..5;
       ch               : char;
       BookName         : string;
       Year             : 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(space:15,'6 -  Sort the List of Books  ');
    writeln;
     writeln(space:15,'0 -  Quit Program: ');
     writeln;
    write(space:15,'Choose your Selection (1..6 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 delhead=nil then
          begin
                 delhead:=book;
                 del:=book;
          end
          else
          begin
                 del^.next:=book;
                 del:=del^.next;
          end;

          if book=Head then Head:=Head^.next;
              prev^.next:=book^.next;
              book:=prev^.next;
              count:=count-1;
              del^.next:=nil;
              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;
       if upcase(ch)='N' then break;
       if count=0 then break;
      Until book = nil;
      writeln('!!!!--  NO MORE BOOKS -!!!!: ');
      readln;
      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;
        book^.next:=nil;
        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;yearofbook : integer);{beginning of procedure search}
var
  found       : boolean;
  current     : link;
  ch          : char;
  Year        : integer;
 begin
   writeln;
   writeln(space:15,'!! SEARCH FOR A BOOK !!');
   writeln(space:15,'**************************');
   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');
        write(space:10,'NAME OF BOOK >: ', current^.Name);
   writeln;
   writeln;
   writeln(space:10,'AUTHOR       >: ', current^.Author);
   writeln;
   writeln(space:10,'PUBLISHER    >: ', current^.Publisher);
   writeln;
   writeln(space:10,'YEAR         >: ',  current^.Year);
   writeln;
   writeln(space:10,'PAGES        >: ', current^.pages);
   writeln;
   writeln(space:10,'SIZE         >: ', current^.size);
   writeln;
   writeln(space:10,'SALE PRICE   >: ', current^.SalePrice);
   writeln;
   writeln(space:10,'Brief Introduction: ', current^.briefdescription);
   writeln;

        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
     begin
     writeln(space:15,'Sorry no book published in ',YearOfBook,'  for sale');
     writeln('NOT FOR SALE');
     end;
    writeln;
   write(space:15,'Press <Y> to continue search : ');
   readln(ch);
   Clrscr;
   if (upcase(ch)='Y') and (current^.next <> nil) then Search(current^.next,yearofbook);
  end; { of procedure search }

procedure Display;
 var
     ch  :char;
 begin
   writeln;
    writeln(space:10,'!!!!--  LIST OF DELETED BOOKS -!!!!: ');
     writeln(space:10,'**************************************');
     if delhead=nil then writeln(space:10,'No Books were deleted')
     else
     Begin
          del:=delhead;
   repeat
   write(space:10,'NAME OF BOOK >: ', del^.Name);
   writeln;
   writeln;
   writeln(space:10,'AUTHOR       >: ', del^.Author);
   writeln;
   writeln(space:10,'PUBLISHER    >: ', del^.Publisher);
   writeln;
   writeln(space:10,'YEAR         >: ',  del^.Year);
   writeln;
   writeln(space:10,'PAGES        >: ', del^.pages);
   writeln;
   writeln(space:10,'SIZE         >: ', del^.size);
   writeln;
   writeln(space:10,'SALE PRICE   >: ', del^.SalePrice);
   writeln;
   writeln(space:10,'Brief Introduction: ', del^.briefdescription);
   writeln;
   write('Do you want to view next book? ');
   read(ch);
   readln;
   del:=del^.next;
   if del=nil then break;
   Until upcase(ch)='N';
   write('End of list of deleted books.');
   readln;
   ClrScr;
     end;
     writeln;
end;
procedure sort;
var node1,node2 : link;
    m,n : integer;
procedure swap(n1,n2 : link);
var temp: booknode;
begin

       temp:=n1^;

       n1^.Pages:=n2^.Pages;
       n1^.Year:=n2^.Year;
       n1^.Author:=n2^.Author;
       n1^.Publisher:=n2^.Publisher;
       n1^.Name:=n2^.Name;
       n1^.SalePrice:=n2^.SalePrice;
       n1^.Size:=n2^.Size;
       n1^.BriefDescription:=n2^.BriefDescription;

       n2^.Pages:=temp.Pages;
       n2^.Year:=temp.Year;
       n2^.Author:=temp.Author;
       n2^.Publisher:=temp.Publisher;
       n2^.Name:=temp.Name;
       n2^.SalePrice:=temp.SalePrice;
       n2^.Size:=temp.Size;
       n2^.BriefDescription:=temp.BriefDescription;

end;
begin
      writeln;
      write('Sorting the list taking yearofbook as primary key.');
      node1:=head;

      for m:=1 to count-1 do
      begin
           node2:=node1^.next;
           for n:=m+1 to count do
           begin
                if node1^.year > node2^.year then
                begin
                      swap(node1,node2);
                end;
                node2:=node2^.next;
                write('.');
           end;
           node1:=node1^.next;
      end;
      writeln;
      writeln('Sorting complete.');
      readln;
      clrscr;
end;

  begin {Main Program or action block}

   repeat
      menu;
      CASE Selection OF
         1  : InitialiseBook;
         2  : View;
         3  : Add;
         4  : Display;
         5  :
         begin
         writeln;
         write('                  Enter year of  book: ');
         readln(year);
         Search(Head,year);
         end;
         6: sort;
        END;

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


0
 

Author Comment

by:greenfly
ID: 2563380
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
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Determine Drive Types 2 478
using a list of objects, Delphi 3 616
Encryption of Passwords for Delphi Xe2 1 747
Delphi Skype API 2 1,881
Determining the an SCCM package name from the Package ID
Configuring Remote Assistance for use with SCCM
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question