【问题标题】:issue with returning on my library system返回我的图书馆系统的问题
【发布时间】:2015-06-03 07:31:31
【问题描述】:

我已在我的图书馆系统中添加了一个返回功能,该功能成功运行。问题是,当你还书后去取出书时,它不成功,并说“书已经借了”。我正在努力解决这个问题。

这是我的系统“借书”和“还书”的代码...

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; 

}
}

【问题讨论】:

  • 借书码在哪里?
  • 抱歉,到此为止。

标签: java return system bluej


【解决方案1】:

当你借书时,你应该设置借阅标志:

 if (!b.isBorrowed()) {
    b.borrowed = true;
    ..

当你还书时,你应该,

if (!b.isBorrowed()) {
     b.borrowed=false;//its returned now and can be borrowed

您可以使用图书馆中同一本书的副本数来扩展逻辑。

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多