图书馆系统-借书
Library system - Borrowing a book
我已经在图书馆系统上工作了几个星期,但在尝试为系统初始化“借阅”功能时遇到了问题。如果有人能帮助我,我将不胜感激。这是我目前拥有的“借用”功能代码。
图书馆class - 包括错误借用
私有列表集合;
public Library()
{
collection = new ArrayList<Book>();
}
public void addBook(Book book)
{
collection.add(book);
}
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 collection.get(i).toString();
}
}
return "\n No Books Avaliable "; //reachable only if no book found
}
public String toString()
{
String total = "\n ";
for (int i=0; i<collection.size(); i++)
{
Book b = collection.get(i);
total = total + b.toString();
}
return total;
}
public void borrowBook(String title) {
int found = 0;
for (Book b : collection) {
if (collection.getTitle().equals(title)) {
if (found == 0) {
found = 1;
}
if (!book.isBorrowed()) {
book.borrowed();
found = 2;
break;
};
}
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}
}
这是本书class
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 ";
}
}
改变
if (collection.getTitle().equals(title))
至
if (b.getTitle().equals(title))
现在您正在调用集合的 getTitle 方法,而不是您当前在迭代中使用的特定书籍。
您似乎还缺少 isBorrowed 方法。
将您的 borrowBook
方法更改为
public void borrowBook(String title)
{
int found = 0;
for (Book b : collection)
{
if (b.getTitle().equals(title))
{
if (found == 0)
{
found = 1;
}
if (!b.isBorrowed())
{
b.borrowed=true;
found = 2;
break;
}
}
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}
并将 boolean borrowed
变量和 boolean isBorrowed()
方法添加到 Book
class 其中 isBorrowed()
将只是 return [=16 的值=]变量。
我已经在图书馆系统上工作了几个星期,但在尝试为系统初始化“借阅”功能时遇到了问题。如果有人能帮助我,我将不胜感激。这是我目前拥有的“借用”功能代码。
图书馆class - 包括错误借用
私有列表集合;
public Library()
{
collection = new ArrayList<Book>();
}
public void addBook(Book book)
{
collection.add(book);
}
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 collection.get(i).toString();
}
}
return "\n No Books Avaliable "; //reachable only if no book found
}
public String toString()
{
String total = "\n ";
for (int i=0; i<collection.size(); i++)
{
Book b = collection.get(i);
total = total + b.toString();
}
return total;
}
public void borrowBook(String title) {
int found = 0;
for (Book b : collection) {
if (collection.getTitle().equals(title)) {
if (found == 0) {
found = 1;
}
if (!book.isBorrowed()) {
book.borrowed();
found = 2;
break;
};
}
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}
}
这是本书class
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 ";
}
}
改变
if (collection.getTitle().equals(title))
至
if (b.getTitle().equals(title))
现在您正在调用集合的 getTitle 方法,而不是您当前在迭代中使用的特定书籍。
您似乎还缺少 isBorrowed 方法。
将您的 borrowBook
方法更改为
public void borrowBook(String title)
{
int found = 0;
for (Book b : collection)
{
if (b.getTitle().equals(title))
{
if (found == 0)
{
found = 1;
}
if (!b.isBorrowed())
{
b.borrowed=true;
found = 2;
break;
}
}
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}
并将 boolean borrowed
变量和 boolean isBorrowed()
方法添加到 Book
class 其中 isBorrowed()
将只是 return [=16 的值=]变量。