该图书借阅系统通过Book、User和Library三个类实现核心功能,支持图书的添加、查询、借阅与归还,用户可在控制台进行交互操作,适合Java面向对象编程学习。

在Java中创建一个简易的图书借阅管理工具,可以通过面向对象的方式实现。我们只需要几个核心类来模拟图书、用户和借阅操作,再配合基本的控制台交互即可完成基础功能。
图书类用于表示系统中的每本图书,包含书名、作者、ISBN编号以及是否被借出的状态。
public class Book {
private String title;
private String author;
private String isbn;
private boolean isBorrowed;
<pre class='brush:java;toolbar:false;'>public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.isBorrowed = false;
}
// Getters
public String getTitle() { return title; }
public String getAuthor() { return author; }
public String getIsbn() { return isbn; }
public boolean isBorrowed() { return isBorrowed; }
// 借出图书
public void borrow() {
if (!isBorrowed) {
isBorrowed = true;
} else {
System.out.println("该书已被借出!");
}
}
// 归还图书
public void returnBook() {
isBorrowed = false;
}
@Override
public String toString() {
return "《" + title + "》 by " + author + " (ISBN: " + isbn + ") - " +
(isBorrowed ? "已借出" : "可借阅");
}}
用户类代表借阅者,可以记录其姓名和当前借阅的图书列表。
立即学习“Java免费学习笔记(深入)”;
import java.util.ArrayList;
import java.util.List;
<p>public class User {
private String name;
private List<Book> borrowedBooks;</p><pre class='brush:java;toolbar:false;'>public User(String name) {
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public String getName() { return name; }
public void borrowBook(Book book) {
if (book != null && !book.isBorrowed()) {
book.borrow();
borrowedBooks.add(book);
System.out.println(name + " 成功借阅: " + book.getTitle());
} else {
System.out.println("无法借阅此书!");
}
}
public void returnBook(Book book) {
if (borrowedBooks.remove(book)) {
book.returnBook();
System.out.println(name + " 已归还: " + book.getTitle());
} else {
System.out.println("你没有借这本书!");
}
}
public List<Book> getBorrowedBooks() {
return new ArrayList<>(borrowedBooks);
}}
图书馆类负责管理所有图书和用户,并提供添加图书、查找图书、借阅和归还等功能。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
<p>public class Library {
private List<Book> books;
private List<User> users;
private Scanner scanner;</p><pre class='brush:java;toolbar:false;'>public Library() {
books = new ArrayList<>();
users = new ArrayList<>();
scanner = new Scanner(System.in);
initializeData(); // 初始化一些测试数据
}
// 初始化测试数据
private void initializeData() {
books.add(new Book("Java编程思想", "Bruce Eckel", "978-7-121-02345-6"));
books.add(new Book("Effective Java", "Joshua Bloch", "978-7-111-21382-6"));
books.add(new Book("深入理解Java虚拟机", "周志明", "978-7-111-42157-5"));
users.add(new User("张三"));
users.add(new User("李四"));
}
// 添加新书
public void addBook(String title, String author, String isbn) {
books.add(new Book(title, author, isbn));
System.out.println("图书添加成功:" + title);
}
// 查找图书(按标题或作者)
public List<Book> searchBooks(String keyword) {
List<Book> result = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
result.add(book);
}
}
return result;
}
// 显示所有图书
public void displayAllBooks() {
System.out.println("\n=== 所有图书 ===");
for (Book book : books) {
System.out.println(book);
}
}
// 主菜单
public void start() {
while (true) {
System.out.println("\n--- 图书借阅管理系统 ---");
System.out.println("1. 查看所有图书");
System.out.println("2. 搜索图书");
System.out.println("3. 借阅图书");
System.out.println("4. 归还图书");
System.out.println("5. 退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
scanner.nextLine(); // 消费换行符
switch (choice) {
case 1:
displayAllBooks();
break;
case 2:
searchBook();
break;
case 3:
borrowBook();
break;
case 4:
returnBook();
break;
case 5:
System.out.println("感谢使用!");
return;
default:
System.out.println("无效选择,请重试。");
}
}
}
private void searchBook() {
System.out.print("请输入书名或作者关键词:");
String keyword = scanner.nextLine();
List<Book> results = searchBooks(keyword);
if (results.isEmpty()) {
System.out.println("未找到相关图书。");
} else {
System.out.println("搜索结果:");
for (Book book : results) {
System.out.println(book);
}
}
}
private void borrowBook() {
System.out.print("输入用户名(张三/李四):");
String userName = scanner.nextLine();
User user = users.stream()
.filter(u -> u.getName().equals(userName))
.findFirst()
.orElse(null);
if (user == null) {
System.out.println("用户不存在!");
return;
}
System.out.print("输入要借阅的书名:");
String title = scanner.nextLine();
Book book = books.stream()
.filter(b -> b.getTitle().equals(title) && !b.isBorrowed())
.findFirst()
.orElse(null);
if (book == null) {
System.out.println("图书不可借阅(可能不存在或已被借出)");
} else {
user.borrowBook(book);
}
}
private void returnBook() {
System.out.print("输入用户名:");
String userName = scanner.nextLine();
User user = users.stream()
.filter(u -> u.getName().equals(userName))
.findFirst()
.orElse(null);
if (user == null) {
System.out.println("用户不存在!");
return;
}
List<Book> borrowed = user.getBorrowedBooks();
if (borrowed.isEmpty()) {
System.out.println("你没有借任何书。");
return;
}
System.out.println("你借的书:");
for (int i = 0; i < borrowed.size(); i++) {
System.out.println((i + 1) + ". " + borrowed.get(i).getTitle());
}
System.out.print("选择要归还的书编号:");
int index = scanner.nextInt() - 1;
if (index >= 0 && index < borrowed.size()) {
user.returnBook(borrowed.get(index));
} else {
System.out.println("无效编号!");
}
}
public static void main(String[] args) {
Library library = new Library();
library.start();
}}
基本上就这些。这个简易系统实现了图书的增删查、用户借阅与归还的基本流程。虽然没有用数据库,但通过内存集合模拟了核心逻辑,适合学习Java基础语法、类设计和集合操作。后续可扩展加入文件存储、图形界面(Swing/JavaFX)或Spring Boot做Web版。
以上就是Java中如何创建一个简易的图书借阅管理工具的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号