我已经为我的图书馆系统添加了一个返回功能,该功能已成功运行。问题在于,当你在归还一本书之后拿出一本书时,这是不成功的,并说“这本书已经借来了”。我正在努力解决这个问题。
这是我的系统“借书”和“归还书”的代码......
public String borrowBook(String titleBorrow)
{
int found = 0;
String bookFound = "\n";
for (Book b : collection)
{
if (b.getTitle().equals(titleBorrow))
{
if (found == 0)
{
found = 1;
}
if (!b.isBorrowed())
{
if (b.numcopies > 0)
{found = 2;
b.numcopies -=1;
break;
}else
{
found = 1;
}
}
}
}
if (found == 0) {
bookFound="Sorry, this book is not in our catalog.";
} else if (found == 1) {
bookFound="Sorry, this book is already borrowed.";
} else if (found == 2) {
bookFound="You successfully borrowed ";
}
return bookFound;
}
public String returnBook(String returnedBook)
{
int found = 0;
boolean borrowed = false;
String bookreturn = "\n";
for (Book b : collection)
{
if (b.getTitle().equals(returnedBook))
{
if (found == 0)
{
found = 1;
}
if (!b.isBorrowed())
{
b.borrowed=true;
found = 2;
b.numcopies +=1;
break;
}
}
}
if (found == 0) {
bookreturn="Sorry, this book is not in our catalog.";
} else if (found == 1) {
bookreturn="Please try again.";
} else if (found == 2) {
bookreturn="You successfully returned the book ";
}
return bookreturn;
}
}
答案 0 :(得分:1)
当你借书时,你应该设置借旗如:
if (!b.isBorrowed()) {
b.borrowed = true;
..
当你还书时,你应该,
if (!b.isBorrowed()) {
b.borrowed=false;//its returned now and can be borrowed
您可以使用图书馆中同一本书的副本数扩展逻辑。