帮我用Java写一个图书管理系统。要求:1.通过从键盘输入具备对书籍信息的维护功能(添加、删除、修改)2.通过从键盘输入具备对借阅人的信息维护功能(添加,删除修改)3.通过从键盘输入具备对借阅管理的维护功能(添加、删除、修改)4.具备对图书的查询功能(按书号、书名、借书时间、借书人等查询)5具备相关信息的统计功能(借阅人次、个人借阅统计、最受欢迎的书,借阅最少的书等)
时间: 2023-12-15 10:03:46 浏览: 65
好的,以下是用Java实现的图书管理系统代码示例:
Book.java
```java
public class Book {
private String id;
private String name;
private String author;
private String publisher;
private String isbn;
private String publishTime;
private double price;
private int totalNum;
private int borrowedNum;
public Book() {}
public Book(String id, String name, String author, String publisher, String isbn, String publishTime, double price, int totalNum, int borrowedNum) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
this.publishTime = publishTime;
this.price = price;
this.totalNum = totalNum;
this.borrowedNum = borrowedNum;
}
// getter and setter methods
public String getId() {
return id;
}
public void setId(String 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 String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getPublishTime() {
return publishTime;
}
public void setPublishTime(String publishTime) {
this.publishTime = publishTime;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getTotalNum() {
return totalNum;
}
public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}
public int getBorrowedNum() {
return borrowedNum;
}
public void setBorrowedNum(int borrowedNum) {
this.borrowedNum = borrowedNum;
}
// toString method
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
", isbn='" + isbn + '\'' +
", publishTime='" + publishTime + '\'' +
", price=" + price +
", totalNum=" + totalNum +
", borrowedNum=" + borrowedNum +
'}';
}
}
```
Borrower.java
```java
public class Borrower {
private String id;
private String name;
private String gender;
private String contact;
public Borrower() {}
public Borrower(String id, String name, String gender, String contact) {
this.id = id;
this.name = name;
this.gender = gender;
this.contact = contact;
}
// getter and setter methods
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
// toString method
@Override
public String toString() {
return "Borrower{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", contact='" + contact + '\'' +
'}';
}
}
```
BorrowRecord.java
```java
public class BorrowRecord {
private String id;
private String borrowTime;
private String returnTime;
private String borrowerId;
private String bookId;
public BorrowRecord() {}
public BorrowRecord(String id, String borrowTime, String returnTime, String borrowerId, String bookId) {
this.id = id;
this.borrowTime = borrowTime;
this.returnTime = returnTime;
this.borrowerId = borrowerId;
this.bookId = bookId;
}
// getter and setter methods
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBorrowTime() {
return borrowTime;
}
public void setBorrowTime(String borrowTime) {
this.borrowTime = borrowTime;
}
public String getReturnTime() {
return returnTime;
}
public void setReturnTime(String returnTime) {
this.returnTime = returnTime;
}
public String getBorrowerId() {
return borrowerId;
}
public void setBorrowerId(String borrowerId) {
this.borrowerId = borrowerId;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
// toString method
@Override
public String toString() {
return "BorrowRecord{" +
"id='" + id + '\'' +
", borrowTime='" + borrowTime + '\'' +
", returnTime='" + returnTime + '\'' +
", borrowerId='" + borrowerId + '\'' +
", bookId='" + bookId + '\'' +
'}';
}
}
```
LibrarySystem.java
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LibrarySystem {
private List<Book> books;
private List<Borrower> borrowers;
private List<BorrowRecord> borrowRecords;
private Scanner scanner;
public LibrarySystem() {
this.books = new ArrayList<>();
this.borrowers = new ArrayList<>();
this.borrowRecords = new ArrayList<>();
this.scanner = new Scanner(System.in);
}
public void start() {
boolean isQuit = false;
while (!isQuit) {
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.println("7. 添加借阅记录");
System.out.println("8. 删除借阅记录");
System.out.println("9. 修改借阅记录");
System.out.println("10. 按书号查询图书信息");
System.out.println("11. 按书名查询图书信息");
System.out.println("12. 按借书时间查询图书信息");
System.out.println("13. 按借书人查询图书信息");
System.out.println("14. 统计借阅人次");
System.out.println("15. 统计个人借阅情况");
System.out.println("16. 统计最受欢迎的书");
System.out.println("17. 统计借阅最少的书");
System.out.println("18. 退出系统");
int choice = scanner.nextInt();
scanner.nextLine(); // consume the newline character
switch (choice) {
case 1:
addBook();
break;
case 2:
deleteBook();
break;
case 3:
modifyBook();
break;
case 4:
addBorrower();
break;
case 5:
deleteBorrower();
break;
case 6:
modifyBorrower();
break;
case 7:
addBorrowRecord();
break;
case 8:
deleteBorrowRecord();
break;
case 9:
modifyBorrowRecord();
break;
case 10:
searchBookById();
break;
case 11:
searchBookByName();
break;
case 12:
searchBookByBorrowTime();
break;
case 13:
searchBookByBorrower();
break;
case 14:
countBorrowTimes();
break;
case 15:
countPersonalBorrow();
break;
case 16:
countPopularBook();
break;
case 17:
countLeastPopularBook();
break;
case 18:
isQuit = true;
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
private void addBook() {
System.out.println("请输入书籍信息:");
System.out.print("书号:");
String id = scanner.nextLine();
System.out.print("书名:");
String name = scanner.nextLine();
System.out.print("作者:");
String author = scanner.nextLine();
System.out.print("出版社:");
String publisher = scanner.nextLine();
System.out.print("ISBN:");
String isbn = scanner.nextLine();
System.out.print("出版时间:");
String publishTime = scanner.nextLine();
System.out.print("价格:");
double price = scanner.nextDouble();
System.out.print("库存数量:");
int totalNum = scanner.nextInt();
System.out.print("借阅数量:");
int borrowedNum = scanner.nextInt();
scanner.nextLine(); // consume the newline character
Book book = new Book(id, name, author, publisher, isbn, publishTime, price, totalNum, borrowedNum);
books.add(book);
System.out.println("添加成功!");
}
private void deleteBook() {
System.out.println("请输入要删除的书籍ID或书名:");
String input = scanner.nextLine();
boolean isDeleted = false;
for (Book book : books) {
if (book.getId().equals(input) || book.getName().equals(input)) {
books.remove(book);
isDeleted = true;
break;
}
}
if (isDeleted) {
System.out.println("删除成功!");
} else {
System.out.println("未找到该书籍!");
}
}
private void modifyBook() {
System.out.println("请输入要修改的书籍ID或书名:");
String input = scanner.nextLine();
boolean isModified = false;
for (Book book : books) {
if (book.getId().equals(input) || book.getName().equals(input)) {
System.out.println("请输入修改后的书籍信息:");
System.out.print("书号:");
book.setId(scanner.nextLine());
System.out.print("书名:");
book.setName(scanner.nextLine());
System.out.print("作者:");
book.setAuthor(scanner.nextLine());
System.out.print("出版社:");
book.setPublisher(scanner.nextLine());
System.out.print("ISBN:");
book.setIsbn(scanner.nextLine());
System.out.print("出版时间:");
book.setPublishTime(scanner.nextLine());
System.out.print("价格:");
book.setPrice(scanner.nextDouble());
System.out.print("库存数量:");
book.setTotalNum(scanner.nextInt());
System.out.print("借阅数量:");
book.setBorrowedNum(scanner.nextInt());
scanner.nextLine(); // consume the newline character
isModified = true;
break;
}
}
if (isModified) {
System.out.println("修改成功!");
} else {
System.out.println("未找到该书籍!");
}
}
private void addBorrower() {
System.out.println("请输入借阅人信息:");
System.out.print("借阅人ID:");
String id = scanner.nextLine();
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("性别:");
String gender = scanner.nextLine();
System.out.print("联系方式:");
String contact = scanner.nextLine();
Borrower borrower = new Borrower(id, name, gender, contact);
borrowers.add(borrower);
System.out.println("添加成功!");
}
private void deleteBorrower() {
System.out.println("请输入要删除的借阅人ID或姓名:");
String input = scanner.nextLine();
boolean isDeleted = false;
for (Borrower borrower : borrowers) {
if (borrower.getId().equals(input) || borrower.getName().equals(input)) {
borrowers.remove(borrower);
isDeleted = true;
break;
}
}
if (isDeleted) {
System.out.println("删除成功!");
} else {
System.out.println("未找到该借阅人!");
}
}
private void modifyBorrower() {
System.out.println("请输入要修改的借阅人ID或姓名:");
String input = scanner.nextLine();
boolean isModified = false;
for (Borrower borrower : borrowers) {
if (borrower.getId().equals(input) || borrower.getName().equals(input)) {
System.out.println("请输入修改后的借阅人信息:");
System.out.print("借阅人ID:");
borrower.setId(scanner.nextLine());
System.out.print("姓名:");
borrower.setName(scanner.nextLine());
System.out.print("性别:");
borrower.setGender(scanner.nextLine());
System.out.print("联系方式:");
borrower.setContact(scanner.nextLine());
isModified
阅读全文