首页 > Java > java教程 > 正文

Java中如何开发一个小型库存管理工具

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

java中如何开发一个小型库存管理工具

开发一个小型库存管理工具可以用Java实现,重点在于结构清晰、功能完整且易于扩展。下面是一个简单的实现思路和代码示例,包含商品信息管理、入库、出库和查询库存等基本功能。

1. 定义商品类(Product)

每个库存商品需要有基本信息,如编号、名称、数量和价格。

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参考手册 中文CHM版
Vuex参考手册 中文CHM版

Vuex是一个专门为Vue.js应用设计的状态管理模型 + 库。它为应用内的所有组件提供集中式存储服务,其中的规则确保状态只能按预期方式变更。它可以与 Vue 官方开发工具扩展(devtools extension) 集成,提供高级特征,比如 零配置时空旅行般(基于时间轴)调试,以及状态快照 导出/导入。本文给大家带来Vuex参考手册,需要的朋友们可以过来看看!

Vuex参考手册 中文CHM版 3
查看详情 Vuex参考手册 中文CHM版

2. 创建库存管理类(InventoryManager)

使用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);
        }
    }
}
登录后复制

}

3. 主程序入口(Main类)

提供简单的命令行交互界面,测试各项功能。

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();
}
登录后复制

}

4. 功能扩展建议

这个基础版本可以进一步增强:

  • 数据持久化:将库存信息保存到文件(如JSON或CSV)或数据库中
  • 异常处理:加入输入验证和空值检查
  • 图形界面:使用Swing或JavaFX提升用户体验
  • 日志记录:记录每次出入库操作
  • 库存预警:当数量低于阈值时提醒

基本上就这些。这个小型工具结构简单,适合学习Java面向对象编程和集合操作,也能作为实际项目的基础原型。

以上就是Java中如何开发一个小型库存管理工具的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号