帮我用java写一个图书馆管理系统 要求 : 1)具备对书籍信息的维护功能(添加、删除、修改) 2)具备对借阅人信息的维护功能(添加、删除、修改) 3)具备对借阅管理的维护功能(添加、删除、修改) 4)具备对图书的查询功能(按书号、书名、或借书时间、借书人等查询 ) 5)具备对相关信息的统计功能(借阅人次,个人借阅统计,最受欢迎的书,借阅最少的书等)
时间: 2023-12-15 09:03:46 浏览: 159
好的,这是一个比较复杂的项目,需要一些时间和精力来完成。我可以给你一些实现思路和代码示例,希望能对你有所帮助。
首先,我们需要定义一些数据结构来存储图书馆的信息,比如书籍、借阅人、借阅记录等。这里我定义了三个类:Book、Borrower、BorrowRecord。
```
public class Book {
private String bookId; // 书籍编号
private String bookName; // 书籍名称
private String author; // 作者
private String publisher; // 出版社
private int totalNum; // 总数量
private int remainNum; // 剩余数量
// getter and setter methods
}
public class Borrower {
private String borrowerId; // 借阅人编号
private String borrowerName; // 借阅人姓名
private String phone; // 电话号码
// getter and setter methods
}
public class BorrowRecord {
private Book book; // 借阅的书籍
private Borrower borrower; // 借阅人
private Date borrowDate; // 借阅日期
private Date returnDate; // 归还日期
// getter and setter methods
}
```
接下来,我们需要实现对这些信息的维护功能。这里我定义了一个Library类,包含了一些方法来添加、删除、修改和查询图书、借阅人、借阅记录等信息。
```
public class Library {
private List<Book> books; // 书籍列表
private List<Borrower> borrowers; // 借阅人列表
private List<BorrowRecord> borrowRecords; // 借阅记录列表
public Library() {
books = new ArrayList<>();
borrowers = new ArrayList<>();
borrowRecords = new ArrayList<>();
}
// 添加书籍
public void addBook(Book book) {
books.add(book);
}
// 删除书籍
public void removeBook(Book book) {
books.remove(book);
}
// 修改书籍
public void updateBook(Book book) {
// 找到对应的书籍,并更新信息
for (int i = 0; i < books.size(); i++) {
if (books.get(i).getBookId().equals(book.getBookId())) {
books.set(i, book);
break;
}
}
}
// 添加借阅人
public void addBorrower(Borrower borrower) {
borrowers.add(borrower);
}
// 删除借阅人
public void removeBorrower(Borrower borrower) {
borrowers.remove(borrower);
}
// 修改借阅人
public void updateBorrower(Borrower borrower) {
// 找到对应的借阅人,并更新信息
for (int i = 0; i < borrowers.size(); i++) {
if (borrowers.get(i).getBorrowerId().equals(borrower.getBorrowerId())) {
borrowers.set(i, borrower);
break;
}
}
}
// 添加借阅记录
public void addBorrowRecord(BorrowRecord borrowRecord) {
borrowRecords.add(borrowRecord);
}
// 删除借阅记录
public void removeBorrowRecord(BorrowRecord borrowRecord) {
borrowRecords.remove(borrowRecord);
}
// 修改借阅记录
public void updateBorrowRecord(BorrowRecord borrowRecord) {
// 找到对应的借阅记录,并更新信息
for (int i = 0; i < borrowRecords.size(); i++) {
if (borrowRecords.get(i).getBook().getBookId().equals(borrowRecord.getBook().getBookId())
&& borrowRecords.get(i).getBorrower().getBorrowerId().equals(borrowRecord.getBorrower().getBorrowerId())) {
borrowRecords.set(i, borrowRecord);
break;
}
}
}
// 按书籍编号查询书籍
public Book findBookById(String bookId) {
for (Book book : books) {
if (book.getBookId().equals(bookId)) {
return book;
}
}
return null;
}
// 按书籍名称查询书籍
public List<Book> findBookByName(String bookName) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getBookName().contains(bookName)) {
result.add(book);
}
}
return result;
}
// 按借阅人姓名查询借阅人
public List<Borrower> findBorrowerByName(String borrowerName) {
List<Borrower> result = new ArrayList<>();
for (Borrower borrower : borrowers) {
if (borrower.getBorrowerName().contains(borrowerName)) {
result.add(borrower);
}
}
return result;
}
// 按借阅时间查询借阅记录
public List<BorrowRecord> findBorrowRecordByDate(Date borrowDate, Date returnDate) {
List<BorrowRecord> result = new ArrayList<>();
for (BorrowRecord borrowRecord : borrowRecords) {
if (borrowRecord.getBorrowDate().compareTo(borrowDate) >= 0 && borrowRecord.getReturnDate().compareTo(returnDate) <= 0) {
result.add(borrowRecord);
}
}
return result;
}
// 按借阅人查询借阅记录
public List<BorrowRecord> findBorrowRecordByBorrower(Borrower borrower) {
List<BorrowRecord> result = new ArrayList<>();
for (BorrowRecord borrowRecord : borrowRecords) {
if (borrowRecord.getBorrower().getBorrowerId().equals(borrower.getBorrowerId())) {
result.add(borrowRecord);
}
}
return result;
}
// 统计借阅人次
public int countBorrowTimes() {
return borrowRecords.size();
}
// 统计个人借阅次数
public int countBorrowTimesByBorrower(Borrower borrower) {
int count = 0;
for (BorrowRecord borrowRecord : borrowRecords) {
if (borrowRecord.getBorrower().getBorrowerId().equals(borrower.getBorrowerId())) {
count++;
}
}
return count;
}
// 统计最受欢迎的书
public Book getMostPopularBook() {
Book result = null;
int maxBorrowTimes = 0;
for (Book book : books) {
int borrowTimes = 0;
for (BorrowRecord borrowRecord : borrowRecords) {
if (borrowRecord.getBook().getBookId().equals(book.getBookId())) {
borrowTimes++;
}
}
if (borrowTimes > maxBorrowTimes) {
maxBorrowTimes = borrowTimes;
result = book;
}
}
return result;
}
// 统计借阅最少的书
public Book getLeastBorrowedBook() {
Book result = null;
int minBorrowTimes = Integer.MAX_VALUE;
for (Book book : books) {
int borrowTimes = 0;
for (BorrowRecord borrowRecord : borrowRecords) {
if (borrowRecord.getBook().getBookId().equals(book.getBookId())) {
borrowTimes++;
}
}
if (borrowTimes < minBorrowTimes) {
minBorrowTimes = borrowTimes;
result = book;
}
}
return result;
}
}
```
最后,我们可以写一个Demo来演示这个图书馆管理系统的功能。
```
public class LibraryDemo {
public static void main(String[] args) {
// 创建图书馆对象
Library library = new Library();
// 添加书籍
Book book1 = new Book();
book1.setBookId("001");
book1.setBookName("Java编程思想");
book1.setAuthor("Bruce Eckel");
book1.setPublisher("机械工业出版社");
book1.setTotalNum(5);
book1.setRemainNum(5);
library.addBook(book1);
Book book2 = new Book();
book2.setBookId("002");
book2.setBookName("Effective Java");
book2.setAuthor("Joshua Bloch");
book2.setPublisher("机械工业出版社");
book2.setTotalNum(3);
book2.setRemainNum(3);
library.addBook(book2);
// 添加借阅人
Borrower borrower1 = new Borrower();
borrower1.setBorrowerId("001");
borrower1.setBorrowerName("张三");
borrower1.setPhone("13888888888");
library.addBorrower(borrower1);
Borrower borrower2 = new Borrower();
borrower2.setBorrowerId("002");
borrower2.setBorrowerName("李四");
borrower2.setPhone("13999999999");
library.addBorrower(borrower2);
// 添加借阅记录
BorrowRecord borrowRecord1 = new BorrowRecord();
borrowRecord1.setBook(book1);
borrowRecord1.setBorrower(borrower1);
borrowRecord1.setBorrowDate(new Date());
borrowRecord1.setReturnDate(null);
library.addBorrowRecord(borrowRecord1);
BorrowRecord borrowRecord2 = new BorrowRecord();
borrowRecord2.setBook(book1);
borrowRecord2.setBorrower(borrower2);
borrowRecord2.setBorrowDate(new Date());
borrowRecord2.setReturnDate(null);
library.addBorrowRecord(borrowRecord2);
// 查询书籍
Book book = library.findBookById("001");
System.out.println(book.getBookName());
List<Book> books = library.findBookByName("Java");
for (Book b : books) {
System.out.println(b.getBookName());
}
// 查询借阅人
List<Borrower> borrowers = library.findBorrowerByName("张");
for (Borrower borrower : borrowers) {
System.out.println(borrower.getBorrowerName());
}
// 查询借阅记录
List<BorrowRecord> borrowRecords = library.findBorrowRecordByDate(new Date(), new Date());
for (BorrowRecord borrowRecord : borrowRecords) {
System.out.println(borrowRecord.getBorrower().getBorrowerName() + "借阅了" + borrowRecord.getBook().getBookName());
}
// 统计借阅人次
System.out.println(library.countBorrowTimes());
// 统计个人借阅次数
System.out.println(library.countBorrowTimesByBorrower(borrower1));
// 统计最受欢迎的书
System.out.println(library.getMostPopularBook().getBookName());
// 统计借阅最少的书
System.out.println(library.getLeastBorrowedBook().getBookName());
}
}
```
这个Demo只是一个简单的例子,实际上还有很多细节需要考虑,比如图书的借阅和归还、借阅人的罚款和信用等等。希望这个示例能够帮助你开发出一个完整的图书馆管理系统。
阅读全文