Java初级项目如何实现图书管理系统
Java的核心目标是掌握基本语法、面向对象编程和简单的控制台交互。该系统不需要数据库或图形界面可以通过集合存储数据来满足学习需求。以下是如何从功能设计到代码结构逐步实现的。1. 明确基本功能基本的图书管理系统通常包括以下操作添加图书输入标题、作者、ISBN编号等信息查看所有书籍列出当前系统中的所有书籍支持模糊搜索根据书名或作者查询图书删除图书通过ISBN或书名删除指定书籍退出系统程序运行结束2. 设计图书类Book首先定义一个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 ? 已借出 : 可借阅); } }3. 创建图书管理BookManager使用ArrayList保存图书对象提供附加删除功能。import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class BookManager { private ListBook 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的书没有找到。); } else { System.out.println(ISBN的书没有找到。); } } }4. 主程序入口和菜单循环在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(请重新选择无效选项。); return; } default - System.out.println(请重新选择无效选项。); } } } }基本上就是这些。本项目涵盖类别设计、集合使用、控制流和用户交互适合Java初学者练习。后续功能可以逐步扩展如添加文件读写保存数据、增加借阅功能、使用Hashmap优化查询效率等。不复杂但容易忽略细节如空指针或输入异常处理。写作时要多注意边界。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2448426.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!