You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

库存管理系统开发疑问:如何结合Inventory类与ProductManagement类

如何将Inventory类与ProductManagement类结合实现库存销售逻辑

嘿,我来帮你搞定这个问题!核心思路是让Inventory类专门承担库存管理的职责(比如存储商品、查找商品、更新库存数量),而ProductManagement类则依赖Inventory来完成销售操作,不再自己遍历产品列表。咱们一步步来实现:

1. 补全基础Product类

首先把你的Product类补全,确保它包含库存相关的属性:

public class Product
{
    public int Id { get; set; } // 用属性替代字段,更符合C#规范
    public int Price { get; set; }
    public int QuantityInStock { get; set; }
    public string Name { get; set; } // 新增商品名称,方便识别
}

2. 定义Inventory类(核心库存管理者)

这个类负责维护所有库存商品,提供高效的查找和更新方法:

public class Inventory
{
    // 用字典存储产品,通过ID快速查找,比遍历列表效率高很多
    private Dictionary<int, Product> _products = new Dictionary<int, Product>();

    // 添加商品到库存
    public void AddProduct(Product product)
    {
        if (!_products.ContainsKey(product.Id))
        {
            _products.Add(product.Id, product);
        }
    }

    // 根据ID查找商品
    public Product GetProductById(int productId)
    {
        _products.TryGetValue(productId, out var product);
        return product;
    }

    // 更新商品库存数量
    public bool UpdateProductQuantity(int productId, int quantityToDeduct)
    {
        var product = GetProductById(productId);
        if (product == null || product.QuantityInStock < quantityToDeduct)
        {
            return false; // 商品不存在或库存不足,更新失败
        }

        product.QuantityInStock -= quantityToDeduct;
        return true;
    }
}

3. 改造ProductManagement类,依赖Inventory

现在让ProductManagement类持有Inventory的实例,销售时调用Inventory的方法来处理库存:

public class ProductManagement
{
    private readonly Inventory _inventory;

    // 通过构造函数注入Inventory,实现依赖解耦
    public ProductManagement(Inventory inventory)
    {
        _inventory = inventory;
    }

    // 销售商品的核心方法
    public bool SellProduct(int productId, int quantityToSell)
    {
        // 1. 从库存中查找商品
        var product = _inventory.GetProductById(productId);
        if (product == null)
        {
            Console.WriteLine($"商品ID {productId} 不存在!");
            return false;
        }

        // 2. 检查库存是否足够
        if (product.QuantityInStock < quantityToSell)
        {
            Console.WriteLine($"商品 {product.Name} 库存不足,当前库存:{product.QuantityInStock},需售出:{quantityToSell}");
            return false;
        }

        // 3. 更新库存数量
        bool updateSuccess = _inventory.UpdateProductQuantity(productId, quantityToSell);
        if (updateSuccess)
        {
            Console.WriteLine($"销售成功!商品 {product.Name} 剩余库存:{product.QuantityInStock}");
            // 这里还可以添加记录销售日志、计算营收等扩展逻辑
        }

        return updateSuccess;
    }
}

4. 调用示例

最后咱们来测试一下整个流程:

class Program
{
    static void Main(string[] args)
    {
        // 初始化库存
        var inventory = new Inventory();
        inventory.AddProduct(new Product { Id = 1, Name = "笔记本电脑", Price = 5000, QuantityInStock = 10 });
        inventory.AddProduct(new Product { Id = 2, Name = "无线鼠标", Price = 80, QuantityInStock = 50 });

        // 初始化产品管理类
        var productManagement = new ProductManagement(inventory);

        // 执行销售操作
        productManagement.SellProduct(1, 2); // 销售2台笔记本
        productManagement.SellProduct(2, 60); // 尝试销售60个鼠标(库存不足)
        productManagement.SellProduct(3, 1); // 尝试销售不存在的商品
    }
}

这样改造后,ProductManagement类不需要自己遍历产品列表,所有库存相关的操作都交给Inventory类处理,代码职责更清晰,也更容易维护和扩展。

内容的提问来源于stack exchange,提问作者J.Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:40:36