PROGRAM LibraryDB (Input,Output);

CONST
  MaxBooks = 1000;
  MaxCopies = 3;

TYPE
  Date = Record
     Day : 1..31;
     Month : 1..12;
     Year : 1994..2010;
    End;

  Checkout = Record
     Card : Integer;
     Due : Date;
    End;

  BookRec = Record
     Dewey : Real;
     Title : String;
     Author : String;
     Year : Integer;
     Checkedout: Array [1..MaxCopies] Of Checkout;
    End;

  LibArray = Array [1..MaxBooks] Of BookRec;


VAR
  Library : LibArray;
  Libsize : Integer;
  Choice : Char;


(****************************************************************)
PROCEDURE PrintBook (Book : BookRec);

Var
  I : Integer;

Begin
  Writeln('--------------------------------------------');
  Writeln('Call #: ',Book.Dewey:7:3);
  Writeln('Title: ',Book.Title:30);
  Writeln('Author: ',Book.Author:30);
  Writeln('Year: ',Book.Year:4);
  FOR I := 1 To MaxCopies DO
    IF Book.CheckedOut[I].Card <> 0 THEN
     Begin
      Write('Copy #',I:1,' checked out to cardholder #',
              Book.CheckedOut[I].Card:1);
      Writeln('  Due: ');
     End;
End;
  



(****************************************************************)
(* Returns index in array where book is or should be *)

FUNCTION FindPlace (Lib : LibArray; Size : Integer; CallNum : Real)
   : Integer;

Var 
  I : Integer;

Begin
  I := 1;
  WHILE (I <= Size) And (Lib[I].Dewey < CallNum) DO
    I := I + 1;

  FindPlace := I
End;


(****************************************************************)
PROCEDURE DelBook (Var Lib : LibArray; Var Size : Integer);

Var
  CallNum : Real;
  I, Index : Integer;

Begin
  Writeln('Enter call number of book to delete: ');
  Readln(CallNum);  
  Index := FindPlace(Lib,Size,CallNum);
  IF Index <= Size THEN
    IF Lib[Index].Dewey = CallNum THEN
     Begin
      FOR I := Index To Size-1 Do
        Lib[I] := Lib[I+1];
      Size := Size - 1;
     End
    ELSE 
      Writeln('Book is in not in Library')
  ELSE
    Writeln('Book is in not in Library');
End;





(****************************************************************)
PROCEDURE AddBook (Var Lib : LibArray; Var Size : Integer);

Var
  I, Index : integer;
  NewBook : BookRec;

Begin
  (*REPEAT*)
    Writeln('Enter Dewey number of book: ');
    Readln(NewBook.Dewey);
    Writeln('Enter title of book: ');
    Readln(NewBook.Title);
    Writeln('Enter author of book: ');
    Readln(NewBook.Author);
    Writeln('Enter year of publication: ');
    Readln(NewBook.Year);
    FOR I := 1 To MaxCopies DO
      NewBook.Checkedout[I].Card := 0;
(*  PrintBook(NewBook);
    Writeln('Is this data correct? (Y or N) ');
    Readln(Correct);
    IF (Correct='n') Or (Correct='N') THEN
      Writeln('OK.  Re-enter data please.');
  UNTIL (Correct='y') Or (Correct='Y');
*)
  Index := FindPlace(Lib,Size,NewBook.Dewey);
  IF Index <= Size THEN
    FOR I := Size Downto Index DO
      Lib[I+1] := Lib[I];
  Lib[Index] := NewBook;
  Size := Size + 1;
End;



(****************************************************************)
PROCEDURE Print (Lib : LibArray; Size : Integer);

Var
  I : Integer;

Begin
  FOR I := 1 To Size DO
    PrintBook(Lib[I]);
End;



(****************************************************************)
PROCEDURE Search (Lib : LibArray; Size : integer);

Var
  I : Integer;
  Choice : Char;
  Callnum : Real;
  Author, Title : String;
  Match : Boolean;

Begin
  Writeln('############################################');
  Writeln('A  Author    T Title    D Dewey call number');
  Readln(Choice);
  CASE Choice Of
    'a','A' : 
      Begin
        Writeln('Author: ');
        Readln(Author);
        Match := False;      
        FOR I := 1 To Size Do
          IF Lib[I].Author = Author THEN
           Begin
            Match := True;
            PrintBook(Lib[I]);
           End;
        IF NOT Match THEN
          Writeln('None found');
      End;
    't','T' : 
      Begin
        Writeln('Title: ');
        Readln(Title);     
        Match := False; 
        FOR I := 1 To Size Do
          IF Lib[I].Title = Title THEN
           Begin
             Match := True;
             PrintBook(Lib[I]);
           End;
        IF NOT Match THEN
          Writeln('None found');
      End;
    'd','D' : 
      Begin
       Writeln('Call number: ');
       Readln(CallNum);
       I := FindPlace(Lib,Size,CallNum);
       IF I <= Size THEN
         IF Lib[I].Dewey = CallNum THEN
           PrintBook(Lib[I])
         ELSE
           Writeln('Book not found')
       ELSE
         Writeln('Book not found');
      End;
     Otherwise  Writeln('Invalid entry');
    End;  (* Case *)

End;

      




BEGIN
  LibSize := 0;

  REPEAT
    Writeln('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
    Writeln('A  Add book         D  Delete book');
    Writeln('P  Print books      S  Search books');
    Writeln('Q  Quit');
    Readln(Choice);
    CASE Choice Of
      'a','A' : AddBook(Library,LibSize);
      'd','D' : DelBook(Library,LibSize);
      'p','P' : Print(Library,LibSize);
      's','S' : Search(Library,LibSize);
      (*'c','C' : Checkout(Library,LibSize);*)
      Otherwise Writeln('Invalid input.  Again please');
    End;
  UNTIL (Choice='q') Or (Choice='Q');
END.

