答案:图书管理系统通过Book类和Library类实现增删查借功能,使用ArrayList管理图书,提供菜单交互。

图书管理系统是学习Java编程时常见的实践项目,能帮助掌握面向对象编程、集合操作和基础的控制流程。以下是实现一个简单图书管理系统的思路与代码示例。
1. 定义图书类(Book)
每本书包含基本信息如书名、作者、ISBN编号和是否借出的状态。
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;
}
// Getter 和 Setter 方法
public String getTitle() { return title; }
public String getAuthor() { return author; }
public String getIsbn() { return isbn; }
public boolean isBorrowed() { return isBorrowed; }
public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; }
@Override
public String toString() {
return "书名:" + title + " | 作者:" + author + " | ISBN:" + isbn +
" | 状态:" + (isBorrowed ? "已借出" : "可借阅");
}}
2. 图书管理功能类(Library)
使用ArrayList存储图书,并提供增删查借等操作。
立即学习“Java免费学习笔记(深入)”;
BJXShop网上购物系统是一个高效、稳定、安全的电子商店销售平台,经过近三年市场的考验,在中国网购系统中属领先水平;完善的订单管理、销售统计系统;网站模版可DIY、亦可导入导出;会员、商品种类和价格均实现无限等级;管理员权限可细分;整合了多种在线支付接口;强有力搜索引擎支持... 程序更新:此版本是伴江行官方商业版程序,已经终止销售,现于免费给大家使用。比其以前的免费版功能增加了:1,整合了论坛
import java.util.ArrayList; import java.util.Scanner;class Library { private ArrayList
books = new ArrayList<>(); private Scanner scanner = new Scanner(System.in); // 添加图书 public void addBook() { System.out.print("输入书名:"); String title = scanner.nextLine(); System.out.print("输入作者:"); String author = scanner.nextLine(); System.out.print("输入ISBN:"); String isbn = scanner.nextLine(); books.add(new Book(title, author, isbn)); System.out.println("图书添加成功!"); } // 查找图书(按书名或作者) public void searchBook() { System.out.print("输入要查找的关键词(书名或作者):"); String keyword = scanner.nextLine().toLowerCase(); boolean found = false; for (Book book : books) { if (book.getTitle().toLowerCase().contains(keyword) || book.getAuthor().toLowerCase().contains(keyword)) { System.out.println(book); found = true; } } if (!found) { System.out.println("未找到相关图书。"); } } // 显示所有图书 public void displayAllBooks() { if (books.isEmpty()) { System.out.println("图书馆暂无图书。"); return; } for (Book book : books) { System.out.println(book); } } // 借阅图书 public void borrowBook() { System.out.print("输入要借阅的ISBN:"); String isbn = scanner.nextLine(); for (Book book : books) { if (book.getIsbn().equals(isbn)) { if (!book.isBorrowed()) { book.setBorrowed(true); System.out.println("成功借阅:" + book.getTitle()); } else { System.out.println("该书已被借出。"); } return; } } System.out.println("未找到该ISBN的图书。"); } // 归还图书 public void returnBook() { System.out.print("输入要归还的ISBN:"); String isbn = scanner.nextLine(); for (Book book : books) { if (book.getIsbn().equals(isbn)) { if (book.isBorrowed()) { book.setBorrowed(false); System.out.println("成功归还:" + book.getTitle()); } else { System.out.println("该书本未被借出。"); } return; } } System.out.println("未找到该ISBN的图书。"); }}
3. 主程序入口(Main)
提供菜单驱动的交互界面,用户可通过选择数字执行对应功能。
public class Main {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
int choice;
do {
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.println("6. 退出");
System.out.print("请选择操作:");
choice = scanner.nextInt();
scanner.nextLine(); // 消费换行符
switch (choice) {
case 1: library.addBook(); break;
case 2: library.searchBook(); break;
case 3: library.displayAllBooks(); break;
case 4: library.borrowBook(); break;
case 5: library.returnBook(); break;
case 6: System.out.println("退出系统。"); break;
default: System.out.println("无效选择,请重试。");
}
} while (choice != 6);
scanner.close();
}}
基本上就这些。通过这个结构,你可以轻松扩展功能,比如加入文件读写保存数据、用户权限管理或图形界面。核心是理解类的设计与集合的使用。不复杂但容易忽略细节,比如Scanner的换行处理和字符串匹配的大小写问题。建议运行后多测试边界情况。









