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

库存管理系统在企业运营中非常关键,Java 作为一门成熟、稳定的编程语言,非常适合开发这类系统。实现一个基础的库存管理系统,可以从设计数据模型、功能模块和交互方式入手。下面介绍如何用 Java 实现一个简单但实用的库存管理系统。
库存管理的核心是商品信息和库存数量。先定义一个 Product 类来表示商品:
public class Product {
private int id;
private String name;
private double price;
private int quantity;
<pre class='brush:java;toolbar:false;'>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;
}}
一个类似淘宝助理、ebay助理的客户端程序,用来方便的在本地处理商店数据,并能够在本地商店、网上商店和第三方平台之间实现数据上传下载功能的工具。功能说明如下:1.连接本地商店:您可以使用ShopEx助理连接一个本地安装的商店系统,这样就可以使用助理对本地商店的商品数据进行编辑等操作,并且数据也将存放在本地商店数据库中。默认是选择“本地未安装商店”,本地还未安
0
创建一个 InventoryManager 类,负责管理商品的增删改查和库存操作:
立即学习“Java免费学习笔记(深入)”;
import java.util.HashMap;
import java.util.Map;
<p>public class InventoryManager {
private Map<Integer, Product> inventory;</p><pre class='brush:java;toolbar:false;'>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);
}
}
}}
使用 Scanner 实现简单的命令行交互界面,方便用户操作:
import java.util.Scanner;
<p>public class Main {
public static void main(String[] args) {
InventoryManager manager = new InventoryManager();
Scanner scanner = new Scanner(System.in);
boolean running = true;</p><pre class='brush:java;toolbar:false;'> 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();
}}
这个系统是基础版本,你可以根据需要扩展以下功能:
基本上就这些。通过面向对象的设计,Java 能很好地组织库存系统的逻辑。从建模到功能实现再到交互,每一步都清晰可控。不复杂但容易忽略细节,比如输入校验和异常处理,实际开发中需要补全。
以上就是如何使用Java实现库存管理系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号