该库存管理工具基于Java实现,包含商品信息管理、入库、出库和查询功能。1. 定义Product类封装商品属性与方法;2. 使用InventoryManager类结合HashMap进行库存操作管理;3. 主程序通过命令行交互提供增删改查界面;4. 支持后续扩展如持久化、异常处理和图形界面等。

开发一个小型库存管理工具可以用Java实现,重点在于结构清晰、功能完整且易于扩展。下面是一个简单的实现思路和代码示例,包含商品信息管理、入库、出库和查询库存等基本功能。
每个库存商品需要有基本信息,如编号、名称、数量和价格。
public class Product {
private String id;
private String name;
private int quantity;
private double price;
<pre class='brush:java;toolbar:false;'>public Product(String id, String name, int quantity, double price) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
// Getter 和 Setter 方法
public String getId() { return id; }
public String getName() { return name; }
public int getQuantity() { return quantity; }
public double getPrice() { return price; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public void setPrice(double price) { this.price = price; }
@Override
public String toString() {
return "ID: " + id + ", 名称: " + name + ", 数量: " + quantity + ", 价格: " + price;
}}
Vuex是一个专门为Vue.js应用设计的状态管理模型 + 库。它为应用内的所有组件提供集中式存储服务,其中的规则确保状态只能按预期方式变更。它可以与 Vue 官方开发工具扩展(devtools extension) 集成,提供高级特征,比如 零配置时空旅行般(基于时间轴)调试,以及状态快照 导出/导入。本文给大家带来Vuex参考手册,需要的朋友们可以过来看看!
3
使用HashMap存储商品,以商品ID为键,便于快速查找。
立即学习“Java免费学习笔记(深入)”;
import java.util.HashMap;
import java.util.Map;
<p>public class InventoryManager {
private Map<String, Product> inventory;</p><pre class='brush:java;toolbar:false;'>public InventoryManager() {
inventory = new HashMap<>();
}
// 添加商品
public void addProduct(Product product) {
inventory.put(product.getId(), product);
System.out.println("已添加商品: " + product.getName());
}
// 入库(增加库存)
public void inbound(String productId, int amount) {
Product product = inventory.get(productId);
if (product != null) {
product.setQuantity(product.getQuantity() + amount);
System.out.println("商品 " + product.getName() + " 入库 " + amount + " 件,当前库存: " + product.getQuantity());
} else {
System.out.println("商品不存在!");
}
}
// 出库(减少库存)
public void outbound(String productId, int amount) {
Product product = inventory.get(productId);
if (product != null) {
if (product.getQuantity() >= amount) {
product.setQuantity(product.getQuantity() - amount);
System.out.println("商品 " + product.getName() + " 出库 " + amount + " 件,剩余库存: " + product.getQuantity());
} else {
System.out.println("库存不足!");
}
} else {
System.out.println("商品不存在!");
}
}
// 查询商品信息
public void searchProduct(String productId) {
Product product = inventory.get(productId);
if (product != null) {
System.out.println(product);
} else {
System.out.println("未找到该商品");
}
}
// 显示所有商品
public void showAllProducts() {
if (inventory.isEmpty()) {
System.out.println("库存为空");
} else {
System.out.println("当前库存列表:");
for (Product p : inventory.values()) {
System.out.println(p);
}
}
}}
提供简单的命令行交互界面,测试各项功能。
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();
scanner.nextLine(); // 消费换行
switch (choice) {
case 1:
System.out.print("输入商品ID: ");
String id = scanner.nextLine();
System.out.print("输入商品名称: ");
String name = scanner.nextLine();
System.out.print("输入数量: ");
int qty = scanner.nextInt();
System.out.print("输入价格: ");
double price = scanner.nextDouble();
manager.addProduct(new Product(id, name, qty, price));
break;
case 2:
System.out.print("输入商品ID: ");
String inId = scanner.nextLine();
System.out.print("输入入库数量: ");
int inQty = scanner.nextInt();
manager.inbound(inId, inQty);
break;
case 3:
System.out.print("输入商品ID: ");
String outId = scanner.nextLine();
System.out.print("输入出库数量: ");
int outQty = scanner.nextInt();
manager.outbound(outId, outQty);
break;
case 4:
System.out.print("输入商品ID: ");
String searchId = scanner.nextLine();
manager.searchProduct(searchId);
break;
case 5:
manager.showAllProducts();
break;
case 6:
running = false;
System.out.println("系统退出。");
break;
default:
System.out.println("无效选择,请重试。");
}
}
scanner.close();
}}
这个基础版本可以进一步增强:
基本上就这些。这个小型工具结构简单,适合学习Java面向对象编程和集合操作,也能作为实际项目的基础原型。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号