Right now I'm making a "find_book" predicate, with which you can find the designated book by user input.
book('C for Dummies', 'Chris Smith', 2000).
book('C++ for Dummies', 'Chris Smith', 2002).
book('Java for Dummies', 'Jason Rash', 1995).
book('JavaScript for Dummies', 'Jason Rash', 2005).
book('Prolog for Dummies', 'Pete Fagan', 1990).
main():-
chooseusertype(Usertype),
startas(Usertype),
find_book(Booktitle). % somehow read the user's input, and then pass that to find_book predicate
find_book(Booktitle, Result):-
write(‘Enter the book title: ’), nl,
read(Booktitle),
% go through the entire data of books defined
% find the book whose name matches the user input
format(‘The author of ~w is ~w ~n’, [Booktitle, Result]).
chooseusertype(X):-
write('Log in as a librarian or guest?: '),
read(X),
format('Your log in type: ~w', [X]).
startas('librarian'):-
write('Logged in as librarian'), nl,
write('Any update on the shelves?').
startas('guest'):-
write('Logged in as guest'), nl,
write('Let us help you find the book you are looking for!'), nl.
write('Enter the book title:'),
% somehow read the user's input, and then pass that to find_book predicate
and I don't know how to implement the part
% go through the entire data of books defined
% find the book whose name matches the user input
, though I know that in a Java-like language you could write like
for(int i=0; i<bookList.length;i++){
Book result = new Book();
if(bookList[i].name == Booktitle){
result = bookList[i];
}
return result.author;
}
If you write Prolog code that corresponds to the Java-like code written above, how does it look like?
[UPDATED]
I updated the predicate find_book
as
find_book(Booktitle, Result):-
write(‘Enter the book title: ’),
read(Booktitle),
book(Booktitle, _),
format(‘The author of ~w is ~w ~n’, [Booktitle, Result]).
, and startas('guest')
predicate as
startas('guest'):-
write('Logged in as guest'), nl,
write('Let us help you find the book you are looking for!'),
find_book(Booktitle).