Java 图书馆系统,签入和签出
Java Library system, Checking In & Out
我们正在制作一个图书馆系统,该系统可以从文件中读取信息以签入、签出书籍、列出所有书籍并显示可用性。我在签入和签出的主文件中遇到问题。签入是通过 isbn 完成的。它在系统中找到这本书,并在末尾附加结帐日期 (MM/DD/YYYY)。没有办法添加书籍。签入和签出已经从 mainGUI 调用,所以我不必执行该部分,但是如何让签出方法将签出日期添加到列表中的书并使其删除签入方法的签出日期? (这里是空的)
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);
}
}
}
}
}
}
}}
您的图书由 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 匹配时,将结帐日期设置为当前日期。
我们正在制作一个图书馆系统,该系统可以从文件中读取信息以签入、签出书籍、列出所有书籍并显示可用性。我在签入和签出的主文件中遇到问题。签入是通过 isbn 完成的。它在系统中找到这本书,并在末尾附加结帐日期 (MM/DD/YYYY)。没有办法添加书籍。签入和签出已经从 mainGUI 调用,所以我不必执行该部分,但是如何让签出方法将签出日期添加到列表中的书并使其删除签入方法的签出日期? (这里是空的)
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);
}
}
}
}
}
}
}}
您的图书由 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 匹配时,将结帐日期设置为当前日期。