%trace
database-book
book_record(integer,string,integer)
database-copy
copy_record(integer,integer,integer)
issued_copies(integer)
predicates
insert_details
get_book_details(integer,string,integer)
find_issued_copies(integer,integer)
increment_issued_count
find_available_copies(integer)
clauses
insert_details:-
retractall(book_record(_,_,_)),
retractall(copy_record(_,_,_)),
assert(book_record(1,ai,6)),
assert(book_record(2,nt,5)),
assert(book_record(3,ooad,2)),
assert(copy_record(1,1,1)),
assert(copy_record(1,2,2)),
assert(copy_record(1,3,3)),
assert(copy_record(3,1,1)).
get_book_details(BookNo,BookName,TotalCopies):-
book_record(BookNo,BookName,TotalCopies).
find_issued_copies(BookNo,IssuedCopies):-
retractall(issued_copies(_)),
assert(issued_copies(0)),
copy_record(BookNo,_,_),
increment_issued_count,
fail.
find_issued_copies(BookNo,IssuedCopies):-
issued_copies(IssuedCopies),
retractall(issued_copies(_)).
increment_issued_count:-
issued_copies(Count),
NewCount = Count + 1,
retract(issued_copies(Count)),
assert(issued_copies(NewCount)),
!.
find_available_copies(BookNo):-
get_book_details(BookNo,BookName,TotalCopies),
find_issued_copies(BookNo,IssuedCount),
AvailableCount = TotalCopies-IssuedCount,
write("[",AvailableCount,"] copies are available of ",BookName),
nl.
goal
clearwindow,
makewindow(1,2,3,"Book Copy Availibility",0,0,25,80),
insert_details,
find_available_copies(1),
find_available_copies(2),
find_available_copies(3).
Output
+----------------------------Book Copy Availibility---------------------------+
¦[3] copies are available of ai ¦
¦[5] copies are available of nt ¦
¦[1] copies are available of ooad ¦
¦ ¦
¦Press the SPACE bar ¦
¦ ¦
¦ ¦
+-----------------------------------------------------------------------------+