答案:该图书管理系统包含添加、查看、查询、删除图书功能,通过Book类封装属性,BookManager类用ArrayList实现增删查操作,主类提供菜单循环交互,适合Java基础学习。

想用Java做一个初级的图书管理系统,核心目标是掌握基础语法、面向对象编程和简单的控制台交互。这个系统不需要数据库或图形界面,用集合类存储数据就能满足学习需求。下面从功能设计到代码结构一步步说明如何实现。
一个基础的图书管理系统通常包含以下操作:
先定义一个Book类来封装图书的基本属性和行为。
public class Book {
private String isbn;
private String title;
private String author;
private boolean isBorrowed; // 是否被借出(可扩展)
public Book(String isbn, String title, String author) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.isBorrowed = false;
}
// Getter 和 Setter 方法
public String getIsbn() { return isbn; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
public boolean isBorrowed() { return isBorrowed; }
public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; }
@Override
public String toString() {
return "ISBN: " + isbn + ", 书名: " + title + ", 作者: " + author + ", 状态: " + (isBorrowed ? "已借出" : "可借阅");
}
}
使用ArrayList保存图书对象,提供增删查功能。
立即学习“Java免费学习笔记(深入)”;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookManager {
private List<Book> books = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public void addBook() {
System.out.print("请输入ISBN: ");
String isbn = scanner.nextLine();
System.out.print("请输入书名: ");
String title = scanner.nextLine();
System.out.print("请输入作者: ");
String author = scanner.nextLine();
books.add(new Book(isbn, title, author));
System.out.println("图书添加成功!");
}
public void viewAllBooks() {
if (books.isEmpty()) {
System.out.println("暂无图书信息。");
} else {
for (Book book : books) {
System.out.println(book);
}
}
}
public void searchBooks() {
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 deleteBook() {
System.out.print("请输入要删除的图书ISBN: ");
String isbn = scanner.nextLine();
boolean removed = books.removeIf(book -> book.getIsbn().equals(isbn));
if (removed) {
System.out.println("删除成功!");
} else {
System.out.println("未找到该ISBN的图书。");
}
}
}
在main方法中创建管理器实例,显示菜单并处理用户选择。
public class Main {
public static void main(String[] args) {
BookManager manager = new BookManager();
Scanner scanner = new Scanner(System.in);
int choice;
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("请选择操作: ");
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("请输入有效数字!");
continue;
}
switch (choice) {
case 1 -> manager.addBook();
case 2 -> manager.viewAllBooks();
case 3 -> manager.searchBooks();
case 4 -> manager.deleteBook();
case 5 -> {
System.out.println("系统已退出。");
return;
}
default -> System.out.println("无效选项,请重新选择。");
}
}
}
}
基本上就这些。这个项目涵盖了类的设计、集合使用、控制流和用户交互,适合Java初学者练手。后续可以逐步扩展功能,比如加入文件读写保存数据、增加借阅功能、使用HashMap优化查询效率等。不复杂但容易忽略细节,比如空指针或输入异常处理,写的时候多注意边界情况就行。
以上就是Java初级项目如何实现图书管理系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号