帮我用java集合ArrayList写一个图书管理系统要求 : 1)具备对书籍信息的维护功能(添加、删除、修改) 2)具备对借阅人信息的维护功能(添加、删除、修改) 3)具备对借阅管理的维护功能(添加、删除、修改) 4)具备对图书的查询功能(按书号、书名、或借书时间、借书人等查询 ) 5)具备对相关信息的统计功能(借阅人次,个人借阅统计,最受欢迎的书,借阅最少的书等)
时间: 2023-12-15 10:04:16 浏览: 70
基于Java的图书借阅管理系统
好的,我可以为您提供一个简单的Java集合ArrayList实现的图书管理系统示例代码。代码如下:
```
import java.util.ArrayList;
import java.util.Scanner;
public class BookManagementSystem {
public static void main(String[] args) {
ArrayList<Book> bookList = new ArrayList<Book>();
ArrayList<Borrower> borrowerList = new ArrayList<Borrower>();
ArrayList<Borrowing> borrowingList = new ArrayList<Borrowing>();
Scanner input = new Scanner(System.in);
int menuOption = 0;
while (menuOption != 6) {
// 显示主菜单
System.out.println("欢迎使用图书管理系统");
System.out.println("1. 添加图书");
System.out.println("2. 删除图书");
System.out.println("3. 修改图书信息");
System.out.println("4. 查询图书");
System.out.println("5. 统计信息");
System.out.println("6. 退出");
System.out.print("请选择操作:");
menuOption = input.nextInt();
switch (menuOption) {
case 1:
// 添加图书
Book newBook = new Book();
System.out.print("请输入书号:");
newBook.setBookId(input.nextInt());
System.out.print("请输入书名:");
newBook.setBookName(input.next());
System.out.print("请输入作者:");
newBook.setAuthor(input.next());
System.out.print("请输入出版社:");
newBook.setPublisher(input.next());
System.out.print("请输入出版日期(YYYY-MM-DD):");
newBook.setPublishDate(input.next());
bookList.add(newBook);
System.out.println("添加成功!");
break;
case 2:
// 删除图书
System.out.print("请输入要删除的书号:");
int deleteBookId = input.nextInt();
boolean bookDeleted = false;
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getBookId() == deleteBookId) {
bookList.remove(i);
System.out.println("删除成功!");
bookDeleted = true;
break;
}
}
if (!bookDeleted) {
System.out.println("找不到该书号的图书!");
}
break;
case 3:
// 修改图书信息
System.out.print("请输入要修改的书号:");
int updateBookId = input.nextInt();
boolean bookUpdated = false;
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getBookId() == updateBookId) {
System.out.print("请输入新的书名:");
bookList.get(i).setBookName(input.next());
System.out.print("请输入新的作者:");
bookList.get(i).setAuthor(input.next());
System.out.print("请输入新的出版社:");
bookList.get(i).setPublisher(input.next());
System.out.print("请输入新的出版日期(YYYY-MM-DD):");
bookList.get(i).setPublishDate(input.next());
System.out.println("修改成功!");
bookUpdated = true;
break;
}
}
if (!bookUpdated) {
System.out.println("找不到该书号的图书!");
}
break;
case 4:
// 查询图书
System.out.println("1. 按书号查询");
System.out.println("2. 按书名查询");
System.out.println("3. 按借书时间查询");
System.out.println("4. 按借书人查询");
System.out.print("请选择查询方式:");
int searchOption = input.nextInt();
switch (searchOption) {
case 1:
// 按书号查询
System.out.print("请输入要查询的书号:");
int searchBookId = input.nextInt();
boolean bookFound = false;
for (Book book : bookList) {
if (book.getBookId() == searchBookId) {
System.out.println(book);
bookFound = true;
break;
}
}
if (!bookFound) {
System.out.println("找不到该书号的图书!");
}
break;
case 2:
// 按书名查询
System.out.print("请输入要查询的书名:");
String searchBookName = input.next();
boolean bookNameFound = false;
for (Book book : bookList) {
if (book.getBookName().equals(searchBookName)) {
System.out.println(book);
bookNameFound = true;
}
}
if (!bookNameFound) {
System.out.println("找不到该书名的图书!");
}
break;
case 3:
// 按借书时间查询
System.out.print("请输入要查询的借书时间(YYYY-MM-DD):");
String searchBorrowDate = input.next();
boolean bookBorrowFound = false;
for (Borrowing borrowing : borrowingList) {
if (borrowing.getBorrowDate().equals(searchBorrowDate)) {
System.out.println(borrowing.getBook());
bookBorrowFound = true;
}
}
if (!bookBorrowFound) {
System.out.println("找不到该借书时间的图书!");
}
break;
case 4:
// 按借书人查询
System.out.print("请输入要查询的借书人姓名:");
String searchBorrowerName = input.next();
boolean borrowerFound = false;
for (Borrowing borrowing : borrowingList) {
if (borrowing.getBorrower().getName().equals(searchBorrowerName)) {
System.out.println(borrowing.getBook());
borrowerFound = true;
}
}
if (!borrowerFound) {
System.out.println("找不到该借书人姓名的图书!");
}
break;
default:
System.out.println("无效的输入!");
break;
}
break;
case 5:
// 统计信息
System.out.println("1. 借阅人次统计");
System.out.println("2. 个人借阅统计");
System.out.println("3. 最受欢迎的书");
System.out.println("4. 借阅最少的书");
System.out.print("请选择统计方式:");
int statisticOption = input.nextInt();
switch (statisticOption) {
case 1:
// 借阅人次统计
System.out.println("借阅人次统计");
System.out.println("--------------------");
for (Borrower borrower : borrowerList) {
int borrowCount = 0;
for (Borrowing borrowing : borrowingList) {
if (borrowing.getBorrower().getId() == borrower.getId()) {
borrowCount++;
}
}
System.out.println(borrower.getName() + ": " + borrowCount + " 次");
}
break;
case 2:
// 个人借阅统计
System.out.print("请输入要查询的借阅人姓名:");
String searchBorrowerName = input.next();
Borrower searchBorrower = null;
for (Borrower borrower : borrowerList) {
if (borrower.getName().equals(searchBorrowerName)) {
searchBorrower = borrower;
break;
}
}
if (searchBorrower == null) {
System.out.println("找不到该借阅人姓名!");
} else {
System.out.println(searchBorrower.getName() + " 的借阅情况:");
System.out.println("--------------------");
for (Borrowing borrowing : borrowingList) {
if (borrowing.getBorrower().getId() == searchBorrower.getId()) {
System.out.println(borrowing.getBook().getBookName() + " 借阅时间:" + borrowing.getBorrowDate());
}
}
}
break;
case 3:
// 最受欢迎的书
System.out.println("最受欢迎的书");
System.out.println("--------------------");
int maxBorrowCount = 0;
ArrayList<Book> popularBooks = new ArrayList<Book>();
for (Book book : bookList) {
int borrowCount = 0;
for (Borrowing borrowing : borrowingList) {
if (borrowing.getBook().getBookId() == book.getBookId()) {
borrowCount++;
}
}
if (borrowCount > maxBorrowCount) {
maxBorrowCount = borrowCount;
popularBooks.clear();
popularBooks.add(book);
} else if (borrowCount == maxBorrowCount) {
popularBooks.add(book);
}
}
for (Book book : popularBooks) {
System.out.println(book.getBookName() + " 借阅次数:" + maxBorrowCount + " 次");
}
break;
case 4:
// 借阅最少的书
System.out.println("借阅最少的书");
System.out.println("--------------------");
int minBorrowCount = Integer.MAX_VALUE;
ArrayList<Book> leastBorrowedBooks = new ArrayList<Book>();
for (Book book : bookList) {
int borrowCount = 0;
for (Borrowing borrowing : borrowingList) {
if (borrowing.getBook().getBookId() == book.getBookId()) {
borrowCount++;
}
}
if (borrowCount < minBorrowCount) {
minBorrowCount = borrowCount;
leastBorrowedBooks.clear();
leastBorrowedBooks.add(book);
} else if (borrowCount == minBorrowCount) {
leastBorrowedBooks.add(book);
}
}
for (Book book : leastBorrowedBooks) {
System.out.println(book.getBookName() + " 借阅次数:" + minBorrowCount + " 次");
}
break;
default:
System.out.println("无效的输入!");
break;
}
break;
case 6:
// 退出
System.out.println("谢谢使用!");
break;
default:
System.out.println("无效的输入!");
break;
}
System.out.println();
}
input.close();
}
}
class Book {
private int bookId;
private String bookName;
private String author;
private String publisher;
private String publishDate;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
@Override
public String toString() {
return "书号:" + bookId + " 书名:" + bookName + " 作者:" + author + " 出版社:" + publisher + " 出版日期:" + publishDate;
}
}
class Borrower {
private int id;
private String name;
private String address;
private String phoneNumber;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
class Borrowing {
private Book book;
private Borrower borrower;
private String borrowDate;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Borrower getBorrower() {
return borrower;
}
public void setBorrower(Borrower borrower) {
this.borrower = borrower;
}
public String getBorrowDate() {
return borrowDate;
}
public void setBorrowDate(String borrowDate) {
this.borrowDate = borrowDate;
}
}
```
该示例代码中定义了三个类:Book、Borrower和Borrowing,分别表示图书、借阅人和借阅。使用ArrayList来存储图书、借阅人和借阅信息。主程序中循环显示主菜单,并根据用户输入执行相应的操作。其中,添加、删除、修改图书信息、添加、删除、修改借阅人信息和添加、删除、修改借阅信息的操作都是通过集合ArrayList来实现的。查询图书和统计信息的操作也使用了ArrayList来遍历数据。
阅读全文