Java Library系统,Check In&退房

时间:2015-04-19 23:43:52

标签: java methods arraylist append checkout

我们正在制作一个从文件中读取的图书馆系统,以检查书籍的输入,输出,列出所有内容并显示可用性。我在主文件中遇到检查和结帐时遇到问题。签到是通过isbn完成的。它在系统中找到该书并在结尾追加结账日期(MM / DD / YYYY)。没有办法添加书籍。已经从mainGUI调用了签入和签出,因此我不必执行该部分,但是如何使checkout方法将checkoutdate添加到列表中的book并使其删除checkoutdate以用于签入方法? (他们在这里空了)

package swe;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import swe.BookGUI.LoginGUI;


public class Main {

ArrayList<Book> bookList = new ArrayList<>(); // Initialize the list of books
BookFileHandler reader;

/*
/ Initial actions to be taken by the program upon start.
*/ 
public void init() throws FileNotFoundException 
{
    populateBookList();
}
/*
/ Used to populate the bookList with books from the book file. 
*/
private void populateBookList() throws FileNotFoundException 
{
    reader = new BookFileHandler(new File("Books.txt"));
    Book tmp;
    tmp = reader.getNextBook();
    while(tmp != null)
    {
        bookList.add(tmp);
        tmp = reader.getNextBook();
    }
}

public static void main(String[] args) {
    LoginGUI login = new LoginGUI();
}

public ArrayList<Book> getBookList()
{
    return bookList;
}

public void checkIn(String ISBN)
{ String title, author, checkoutDate; 
  int isbn;

}

public void checkIn(Book b)
{

}

public void checkOut(String ISBN)
{

}

public void checkOut(Book b)
{

}

public Book findByAuthor(String author)
{
    for(Book x : bookList)
        if(x.getAuthor().equalsIgnoreCase(author))
            return x;
    return null;
}

public Book findByTitle(String title)
{
    for(Book x : bookList)
        if(x.getName().equalsIgnoreCase(title))
            return x;
    return null;
}

public Book findByISBN(String ISBN)
{
    for(Book x : bookList)
        if(x.getIsbn().equalsIgnoreCase(ISBN))
            return x;
    return null;
}

public void showBookInfo(Book b)
{
    if(b != null)
    {
        String info = "ISBN: " + b.getIsbn() + "\nTitle: " + b.getName() + "\nAuthor: " + b.getAuthor() + "\nCheckout date (null if not checked out): " + b.getCheckoutDate();
        JOptionPane.showMessageDialog(null, info,"Book info", JOptionPane.INFORMATION_MESSAGE);
    }
}

public void listBooks()
{
    String list = "";
    for(Book b : bookList)
    {
        list += "ISBN: " + b.getIsbn() + "\nTitle: " + b.getName() + "\nAuthor: " + b.getAuthor() + "\nCheckout date (null if not checked out): " + b.getCheckoutDate();
    }
    JOptionPane.showMessageDialog(null, list ,"Book list", JOptionPane.INFORMATION_MESSAGE);
}

public void sortByAuthor()
{
    ArrayList<Book> tmp = new ArrayList<Book>();
    for(Book b : bookList)
    {
        if(!tmp.isEmpty())
        {
            for(int x =0; x < tmp.size(); x++)
            {
                if(b.getAuthor().compareTo(tmp.get(x).getAuthor()) > 0)
                {
                    if(tmp.get(x+1) == null)
                    {
                        tmp.add(b);
                    }
                    else
                    {
                        if(b.getAuthor().compareTo(tmp.get(x+1).getAuthor()) < 0)
                        {
                            tmp.add(x, b);
                        }
                    }
                }
            }
        }
    }
}}    

1 个答案:

答案 0 :(得分:0)

您的图书由ISBN映射,因此要删除每个图书的结帐日期,并删除ISBN匹配的日期:

public void checkIn(String ISBN)
    for(Book b : booklist)
    {
       if(b.getIsbn().equals(ISBN))
       {
           b.setCheckoutDate("");
           break;
       }
    }
}

或者使用Java 8,您可以用

替换for-each循环
booklist.stream()
    .filter(book -> book.getIsbn().equals(ISBN))
    .forEach(book -> book.setCheckoutDate(""));

如果您愿意。

修改

根据您的评论,如果您想要在取出图书时添加结帐日期,则应用相同的算法。在每本书上循环,然后在ISBN匹配时,将结帐日期设置为当前日期。