在我目前的程序结构中,当产品销售时,它会遍历产品列表,我没有使用我的库存类。
我的意图是当产品被销售时,它将遍历我当前的库存找到该项目并执行相应的功能 - 即更新库存数量,但我不太确定如何使用我的库存类别产品管理类。希望这有点意义,谢谢:)
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;
}
}
}
}
答案 0 :(得分:-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://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions