首先设计商品数据模型,定义Product类包含id、name、price、quantity属性及对应getter/setter方法;接着创建InventoryManager类,使用HashMap管理商品,实现添加、删除、查询、更新和显示所有商品功能,若添加时商品已存在则合并库存,删除时判断是否存在,查询返回指定商品,更新时可部分修改信息,遍历values输出全部商品;然后编写Main类,利用Scanner实现命令行交互界面,提供6项操作选项,通过switch语句处理用户输入,循环执行直至选择退出,并在最后关闭scanner资源;最后提出扩展建议如数据持久化、库存预警、操作日志、图形界面和权限管理,强调Java面向对象特性适合系统开发,需注意输入校验与异常处理。

库存管理系统在企业运营中非常关键,Java 作为一门成熟、稳定的编程语言,非常适合开发这类系统。实现一个基础的库存管理系统,可以从设计数据模型、功能模块和交互方式入手。下面介绍如何用 Java 实现一个简单但实用的库存管理系统。
1. 设计商品和库存数据模型
库存管理的核心是商品信息和库存数量。先定义一个 Product 类来表示商品:
public class Product {
private int id;
private String name;
private double price;
private int quantity;
public Product(int id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
// Getter 和 Setter 方法
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
@Override
public String toString() {
return "ID: " + id + ", 名称: " + name + ", 价格: " + price + ", 库存: " + quantity;
}}
51shop 网上商城系统
51shop 由 PHP 语言开发, 使用快速的 MySQL 数据库保存数据 ,为中小型网站实现网上电子商务提供一个完美的解决方案.一、用户模块1. 用户注册:用户信息包括:用户ID、用户名、用户密码、性别、邮箱、省份、城市、 联系电话等信息,用户注册后不能立即使用,需由管理员激活账号,才可使用(此功能管理员可设置)2. 登录功能3. 资料修改:用户可修改除账号以后的所有资料4. 忘记密码:要求用
下载
2. 实现库存管理功能类
创建一个 InventoryManager 类,负责管理商品的增删改查和库存操作:
立即学习“Java免费学习笔记(深入)”;
import java.util.HashMap; import java.util.Map;public class InventoryManager { private Map
inventory; public InventoryManager() { inventory = new HashMap<>(); } // 添加商品 public void addProduct(Product product) { if (inventory.containsKey(product.getId())) { System.out.println("商品已存在,更新库存..."); Product existing = inventory.get(product.getId()); existing.setQuantity(existing.getQuantity() + product.getQuantity()); } else { inventory.put(product.getId(), product); System.out.println("商品添加成功:" + product.getName()); } } // 删除商品 public void removeProduct(int productId) { if (inventory.remove(productId) != null) { System.out.println("商品删除成功"); } else { System.out.println("商品不存在"); } } // 查询商品 public Product findProduct(int productId) { return inventory.get(productId); } // 更新商品信息 public void updateProduct(int productId, String name, double price, int quantity) { Product product = inventory.get(productId); if (product != null) { if (name != null && !name.trim().isEmpty()) product.setName(name); if (price > 0) product.setPrice(price); if (quantity >= 0) product.setQuantity(quantity); System.out.println("商品信息更新成功"); } else { System.out.println("商品未找到"); } } // 显示所有商品 public void displayAllProducts() { if (inventory.isEmpty()) { System.out.println("库存为空"); } else { System.out.println("当前库存列表:"); for (Product p : inventory.values()) { System.out.println(p); } } }}
3. 创建主程序进行交互
使用 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(); switch (choice) { case 1: System.out.print("输入ID: "); int id = scanner.nextInt(); System.out.print("输入名称: "); String name = scanner.next(); System.out.print("输入价格: "); double price = scanner.nextDouble(); System.out.print("输入数量: "); int qty = scanner.nextInt(); manager.addProduct(new Product(id, name, price, qty)); break; case 2: System.out.print("输入要删除的商品ID: "); int delId = scanner.nextInt(); manager.removeProduct(delId); break; case 3: System.out.print("输入商品ID: "); int findId = scanner.nextInt(); Product p = manager.findProduct(findId); if (p != null) { System.out.println("找到商品:" + p); } else { System.out.println("商品未找到"); } break; case 4: System.out.print("输入商品ID: "); int updateId = scanner.nextInt(); System.out.print("新名称(留空不修改): "); String newName = scanner.next(); if (newName.equals("null")) newName = null; System.out.print("新价格(0不修改): "); double newPrice = scanner.nextDouble(); System.out.print("新数量(-1不修改): "); int newQty = scanner.nextInt(); manager.updateProduct(updateId, newName, newPrice, newQty == -1 ? manager.findProduct(updateId).getQuantity() : newQty); break; case 5: manager.displayAllProducts(); break; case 6: running = false; System.out.println("退出系统"); break; default: System.out.println("无效选择,请重试"); } } scanner.close(); }}
4. 扩展建议
这个系统是基础版本,你可以根据需要扩展以下功能:
- 将数据持久化到文件或数据库(如 MySQL、SQLite)
- 增加库存预警功能(当库存低于某个值时提醒)
- 支持进货、销售记录日志
- 使用图形界面(Swing 或 JavaFX)提升用户体验
- 加入用户权限管理
基本上就这些。通过面向对象的设计,Java 能很好地组织库存系统的逻辑。从建模到功能实现再到交互,每一步都清晰可控。不复杂但容易忽略细节,比如输入校验和异常处理,实际开发中需要补全。









