通过面向对象设计实现图书库存管理工具,包含Book类存储图书信息,InventoryManager类用HashMap管理图书增删改查,Main类提供控制台菜单交互,支持添加、查询、更新、删除和查看图书功能。
开发一个图书库存管理工具在Java中可以通过面向对象的方式实现,重点在于设计合理的类结构、数据存储方式以及用户交互逻辑。下面是一个简单但实用的实现思路和步骤。
每本书的基本信息可以用一个Book类来表示,包含书名、作者、ISBN、出版年份和库存数量等属性。
示例代码:
public class Book {
private String title;
private String author;
private String isbn;
private int year;
private int stock;
public Book(String title, String author, String isbn, int year, int stock) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.year = year;
this.stock = stock;
}
// Getter 和 Setter 方法
public String getTitle() { return title; }
public String getIsbn() { return isbn; }
public i
nt getStock() { return stock; }
public void setStock(int stock) { this.stock = stock; }
@Override
public String toString() {
return "书名: " + title + ", 作者: " + author +
", ISBN: " + isbn + ", 年份: " + year + ", 库存: " + stock;
}}
这个类负责管理所有图书的增删改查操作,使用HashMap以ISBN为键存储图书,便于快速查找。
import java.util.HashMap; import java.util.Map;public class InventoryManager { private Map
books; public InventoryManager() { books = new HashMap<>(); } // 添加图书 public void addBook(Book book) { if (books.containsKey(book.getIsbn())) { Book existing = books.get(book.getIsbn()); existing.setStock(existing.getStock() + book.getStock()); } else { books.put(book.getIsbn(), book); } } // 删除图书 public boolean removeBook(String isbn) { return books.remove(isbn) != null; } // 查询图书 public Book findBook(String isbn) { return books.get(isbn); } // 更新库存 public boolean updateStock(String isbn, int newStock) { Book book = books.get(isbn); if (book != null) { book.setStock(newStock); return true; } return false; } // 显示所有图书 public void listAllBooks() { if (books.isEmpty()) { System.out.println("库存为空。"); } else { books.values().forEach(System.out::println); } }}
使用Scanner从控制台读取用户输入,提供菜单式操作。
import java.util.Scanner;public class Main { public static void main(String[] args) { InventoryManager manager = new InventoryManager(); Scanner scanner = new Scanner(System.in); boolean running = true;
while (running) { 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("请选择操作: "); int choice = scanner.nextInt(); scanner.nextLine(); // 消费换行符 switch (choice) { case 1: System.out.print("书名: "); String title = scanner.nextLine(); System.out.print("作者: "); String author = scanner.nextLine(); System.out.print("ISBN: "); String isbn = scanner.nextLine(); System.out.print("年份: "); int year = scanner.nextInt(); System.out.print("库存数量: "); int stock = scanner.nextInt(); scanner.nextLine(); Book newBook = new Book(title, author, isbn, year, stock); manager.addBook(newBook); System.out.println("图书已添加。"); break; case 2: System.out.print("输入ISBN查询: "); String searchIsbn = scanner.nextLine(); Book found = manager.findBook(searchIsbn); if (found != null) { System.out.println(found); } else { System.out.println("未找到该图书。"); } break; case 3: System.out.print("输入ISBN: "); String updateIsbn = scanner.nextLine(); System.out.print("新库存数量: "); int newStock = scanner.nextInt(); scanner.nextLine(); if (manager.updateStock(updateIsbn, newStock)) { System.out.println("库存已更新。"); } else { System.out.println("未找到该图书。"); } break; case 4: System.out.print("输入要删除的ISBN: "); String delIsbn = scanner.nextLine(); if (manager.removeBook(delIsbn)) { System.out.println("图书已删除。"); } else { System.out.println("未找到该图书。"); } break; case 5: manager.listAllBooks(); break; case 6: running = false; System.out.println("系统已退出。"); break; default: System.out.println("无效选择,请重试。"); } } scanner.close(); }}
当前版本是基于内存的,适合学习和演示。实际项目中可考虑以下改进:
基本上就这些,不复杂但容易忽略细节。