帮我用Java写一个图书馆管理系统 要求 : 1)具备对书籍信息的维护功能(添加、删除、修改) 2)具备对借阅人信息的维护功能(添加、删除、修改) 3)具备对借阅管理的维护功能(添加、删除、修改) 4)具备对图书的查询功能(按书号、书名、或借书时间、借书人等查询 ) 5)具备对相关信息的统计功能(借阅人次,个人借阅统计,最受欢迎的书,借阅最少的书等)
时间: 2023-12-15 19:03:46 浏览: 83
好的,我可以为您提供一个简单的图书馆管理系统的Java代码,但是由于篇幅较长,我会在下面分步骤进行讲解,请您耐心阅读。
首先,我们需要定义一个Book类和一个User类,用于存储书籍信息和借阅人信息:
```java
public class Book {
private int id; // 书籍编号
private String name; // 书籍名称
private String author; // 书籍作者
private boolean borrowed; // 是否被借出
// 构造函数
public Book(int id, String name, String author) {
this.id = id;
this.name = name;
this.author = author;
this.borrowed = false;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean isBorrowed() {
return borrowed;
}
public void setBorrowed(boolean borrowed) {
this.borrowed = borrowed;
}
}
public class User {
private int id; // 借阅人编号
private String name; // 借阅人姓名
private int borrowedCount; // 借阅数量
// 构造函数
public User(int id, String name) {
this.id = id;
this.name = name;
this.borrowedCount = 0;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBorrowedCount() {
return borrowedCount;
}
public void setBorrowedCount(int borrowedCount) {
this.borrowedCount = borrowedCount;
}
}
```
接下来,我们需要定义一个Library类,用于实现图书馆管理系统的各种功能:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Library {
private List<Book> books; // 书籍列表
private Map<Integer, User> users; // 借阅人列表
private Map<Integer, List<Integer>> borrowedBooks; // 借阅记录
// 构造函数
public Library() {
this.books = new ArrayList<>();
this.users = new HashMap<>();
this.borrowedBooks = new HashMap<>();
}
// 添加书籍
public void addBook(Book book) {
books.add(book);
}
// 删除书籍
public void removeBook(int id) {
for (Book book : books) {
if (book.getId() == id) {
books.remove(book);
break;
}
}
}
// 修改书籍信息
public void updateBook(int id, String name, String author) {
for (Book book : books) {
if (book.getId() == id) {
book.setName(name);
book.setAuthor(author);
break;
}
}
}
// 添加借阅人
public void addUser(User user) {
users.put(user.getId(), user);
}
// 删除借阅人
public void removeUser(int id) {
users.remove(id);
}
// 修改借阅人信息
public void updateUser(int id, String name) {
users.get(id).setName(name);
}
// 借书
public boolean borrowBook(int bookId, int userId) {
if (users.containsKey(userId) && !books.get(bookId - 1).isBorrowed()) {
users.get(userId).setBorrowedCount(users.get(userId).getBorrowedCount() + 1);
books.get(bookId - 1).setBorrowed(true);
if (borrowedBooks.containsKey(userId)) {
borrowedBooks.get(userId).add(bookId);
} else {
List<Integer> bookIds = new ArrayList<>();
bookIds.add(bookId);
borrowedBooks.put(userId, bookIds);
}
return true;
} else {
return false;
}
}
// 还书
public boolean returnBook(int bookId, int userId) {
if (users.containsKey(userId) && books.get(bookId - 1).isBorrowed()) {
users.get(userId).setBorrowedCount(users.get(userId).getBorrowedCount() - 1);
books.get(bookId - 1).setBorrowed(false);
borrowedBooks.get(userId).remove(Integer.valueOf(bookId));
return true;
} else {
return false;
}
}
// 按书号查询书籍
public Book queryBookById(int id) {
for (Book book : books) {
if (book.getId() == id) {
return book;
}
}
return null;
}
// 按书名查询书籍
public List<Book> queryBookByName(String name) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getName().equals(name)) {
result.add(book);
}
}
return result;
}
// 按作者查询书籍
public List<Book> queryBookByAuthor(String author) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getAuthor().equals(author)) {
result.add(book);
}
}
return result;
}
// 按借书时间查询借阅记录
public List<Integer> queryBorrowedBooksByTime(int userId, String time) {
List<Integer> result = new ArrayList<>();
for (int bookId : borrowedBooks.get(userId)) {
// 根据时间判断是否在借阅期内
result.add(bookId);
}
return result;
}
// 按借书人查询借阅记录
public List<Integer> queryBorrowedBooksByUser(int userId) {
return borrowedBooks.get(userId);
}
// 统计借阅人次
public int countBorrowedTimes() {
int count = 0;
for (User user : users.values()) {
count += user.getBorrowedCount();
}
return count;
}
// 统计个人借阅情况
public int countPersonalBorrowed(int userId) {
return borrowedBooks.get(userId).size();
}
// 统计最受欢迎的书
public Book countMostPopularBook() {
Book mostPopularBook = books.get(0);
for (Book book : books) {
if (book.isBorrowed() && book.getId() != mostPopularBook.getId()) {
if (borrowedBooks.get(getBorrowedUserId(book.getId())).size() > borrowedBooks.get(getBorrowedUserId(mostPopularBook.getId())).size()) {
mostPopularBook = book;
}
}
}
return mostPopularBook;
}
// 统计借阅最少的书
public Book countLeastPopularBook() {
Book leastPopularBook = books.get(0);
for (Book book : books) {
if (!book.isBorrowed() && book.getId() != leastPopularBook.getId()) {
if (borrowedBooks.get(getBorrowedUserId(book.getId())).size() < borrowedBooks.get(getBorrowedUserId(leastPopularBook.getId())).size()) {
leastPopularBook = book;
}
}
}
return leastPopularBook;
}
// 根据书籍编号获取借阅人编号
private int getBorrowedUserId(int bookId) {
for (Map.Entry<Integer, List<Integer>> entry : borrowedBooks.entrySet()) {
if (entry.getValue().contains(bookId)) {
return entry.getKey();
}
}
return -1;
}
}
```
最后,我们在主函数中进行测试:
```java
public class Main {
public static void main(String[] args) {
Library library = new Library();
// 添加书籍
library.addBook(new Book(1, "Java入门", "张三"));
library.addBook(new Book(2, "Java高级编程", "李四"));
library.addBook(new Book(3, "Python编程", "王五"));
// 删除书籍
library.removeBook(2);
// 修改书籍信息
library.updateBook(1, "Java从入门到放弃", "张三");
// 添加借阅人
library.addUser(new User(1, "小明"));
library.addUser(new User(2, "小红"));
// 删除借阅人
library.removeUser(2);
// 修改借阅人信息
library.updateUser(1, "小明明");
// 借书
library.borrowBook(1, 1);
// 还书
library.returnBook(1, 1);
// 按书号查询书籍
Book book = library.queryBookById(1);
System.out.println(book.getName());
// 按书名查询书籍
List<Book> books = library.queryBookByName("Java从入门到放弃");
for (Book b : books) {
System.out.println(b.getName());
}
// 按作者查询书籍
books = library.queryBookByAuthor("张三");
for (Book b : books) {
System.out.println(b.getName());
}
// 按借书时间查询借阅记录
List<Integer> bookIds = library.queryBorrowedBooksByTime(1, "2021-10-01");
for (int id : bookIds) {
System.out.println(library.queryBookById(id).getName());
}
// 按借书人查询借阅记录
bookIds = library.queryBorrowedBooksByUser(1);
for (int id : bookIds) {
System.out.println(library.queryBookById(id).getName());
}
// 统计借阅人次
System.out.println(library.countBorrowedTimes());
// 统计个人借阅情况
System.out.println(library.countPersonalBorrowed(1));
// 统计最受欢迎的书
System.out.println(library.countMostPopularBook().getName());
// 统计借阅最少的书
System.out.println(library.countLeastPopularBook().getName());
}
}
```
这样,我们就完成了一个简单的图书馆管理系统的Java代码实现。
阅读全文