库存管理系统的结构



在我当前的程序结构中出售产品时,它通过产品列表循环,我不使用库存类。

我的意图是出售产品时,它将循环通过我当前的库存找到该项目并执行适当的功能 - 即更新库存的数量,但是我不确定如何将库存类别用于我的product Management类。希望这有点有道理,谢谢:)

 class Product
{
    public int _id;
    public int _price;
    public int _quantity;
    public Product(int id, int price, int quantity)
    {
        _id = id;
        _price = price;
        _quantity = quantity;
    }
}

class Inventory
{
    private List<Product> _products = new List<Product>();
    public Inventory(List<Product> products)
    {
        _products = products;
    }
}

class ProductManagement : IProduct
{
    public List<Product> inventory = new List<Product>();
    public void addProduct(Product product)
    {
        inventory.Add(product);
    }
    public void productSold(Product product)
    {
        inventory.Remove(product);
        foreach(Product p in inventory)
        {
            if(p._id == product._id)
            {
                p._quantity = p._quantity - 1;
            }
        }   
    }
}

查看这是否是您要寻找的:

class ProductManagement : IProduct
{
    public Inventory inventory = new Inventory();
    public void addProduct(Product product)
    {
        inventory.AddProduct(product);
    }
    public void productSold(Product product)
    {
        //TODO: consider accepting quantitySold as a parameter, as you could sell more than 1 quantity of the same product
        var quantitySold = 1;
        inventory.UpdateProduct(product._id, quantitySold);
    }
}
class Inventory
{
    private List<Product> _products = new List<Product>();
    public Inventory()
    {
    }
    public void AddProduct(Product product)
    {
        //TODO: add checks if product already exists
        _products.Add(product);
    }
    public void UpdateProduct(int id, int quantity)
    {
        //TODO: check if id is valid
        var product = _products.Single(x => x._id == id);
        product._quantity -= quantity;
    }
}

我建议您浏览C#编码约定:https://learn.microsoft.com/en-us/dotnet/csharp/csharp/programmpramming-guide/inside-a-program/coding-conventions-conventions-conventions<>

最新更新