我的图书馆系统项目的搜索功能出现问题。到目前为止,我的代码允许您查看该图书是“可用”还是“不可用”。我想让我的系统也能够显示书中的所有信息,包括副本数量,ISBN等。 这是我用来搜索图书是否可用的代码。
public String searchTitle(String titleSearch) {
if (titleSearch == null)
return "\n No Books Avaliable ";
for (int i = 0; i < collection.size(); i++){
if (titleSearch.equalsIgnoreCase(collection.get(i).getTitle())) {
return "\n Book Avaliable";
}
}
return "\n No Books Avaliable "; //reachable only if no book found
}
这也是我在Book
课程中使用的代码:
public Book(int isbn, String author, String title, String genre, int numcopies) {
this.isbn = isbn;
this.author = author;
this.title = title;
this.genre = genre;
this.numcopies = numcopies;
}
public int getISBN() {
return isbn;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String toString() {
return "\nISBN: " + isbn + "\nAuthor: " + author + "\nTitle: " + title +
"\nGenre: " + genre + "\nNumber Of Copies " + numcopies +"\n ";
}
答案 0 :(得分:2)
如果我理解正确的话;
if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
return "\n Book Avaliable";
}
在这部分代码中,您可以轻松地将结果返回到book类中的string方法。这就像返回字符串“\ n Book Avaliable”。
请记住,您可以调用字符串函数,如collection.get(i).toString()
或者只是代码就像;
if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
return collection.get(i).toString();
}