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

在Java中创建一个简易的图书借阅管理工具,可以通过面向对象的方式实现。我们只需要几个核心类来模拟图书、用户和借阅操作,再配合基本的控制台交互即可完成基础功能。
1. 定义图书类(Book)
图书类用于表示系统中的每本图书,包含书名、作者、ISBN编号以及是否被借出的状态。
public class Book { private String title; private String author; private String isbn; private boolean isBorrowed;public Book(String title, String author, String isbn) { this.title = title; this.author = author; this.isbn = isbn; this.isBorrowed = false;}// Getterspublic 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;}@Overridepublic String toString() { return "《" + title + "》 by " + author + " (ISBN: " + isbn + ") - " + (isBorrowed ? "已借出" : "可借阅");}
}
2. 定义用户类(User)
用户类代表借阅者,可以记录其姓名和当前借阅的图书列表。
立即学习“Java免费学习笔记(深入)”;
慧中标AI标书
慧中标AI标书是一款AI智能辅助写标书工具。
120 查看详情
import java.util.ArrayList;import java.util.List;public class User {private String name;private List borrowedBooks;
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 getBorrowedBooks() { return new ArrayList(borrowedBooks);}
}
3. 图书管理系统主类(Library)
图书馆类负责管理所有图书和用户,并提供添加图书、查找图书、借阅和归还等功能。
import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Library {private List books;private List users;private Scanner scanner;
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 searchBooks(String keyword) { List 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 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 borrowed = user.getBorrowedBooks(); if (borrowed.isEmpty()) { System.out.println("你没有借任何书。"); return; } System.out.println("你借的书:"); for (int i = 0; i = 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中如何创建一个简易的图书借阅管理工具的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/571531.html
微信扫一扫
支付宝扫一扫