-1

当我调用我的 Counter Stock 方法时:

 FoodProducts FoodProd1 = new FoodProducts("FP001", "Meat", 15.99, 200, 100, "Australia");
 FoodProducts FoodProd2 = new FoodProducts("FP002", "Bread", 2.99, 150, 50, "Italy");

 Console.WriteLine("This is the Total Stock For the Store: {0}", CounterStock(ProductList)); **//Not Displaying the Total Stock of All items it equals to 0**

List<Products> ProductList = new List<Products>();

static int CounterStock(List<Products> ProductList)
{
        int CounterStoreStock = 0;
        foreach (Products Count in ProductList)
        {
            CounterStoreStock += Count.StockCount++;
        }
        return CounterStoreStock;
    }

我正在使用继承

class Products
{
    string id;
    string name;
    double price;
    int soldCount;
    int stockCount;

    public Products(string id, string name, double price, int soldCount, int stockCount)
    {
        this.id = id;
        this.name = name;
        this.price = price;
        this.soldCount = soldCount;
        this.stockCount = stockCount;
    }

    public string ID
    {
        get{return id;}
    }

    public string Name
    {
        get { return name; }
        set {name = value;}
    }

    public double Price
    {
        get {return price;}
        set {price = value;}
    }

    public int SoldCount
    {
        get {return soldCount;}
        set { soldCount = value; }
    }

    public int StockCount
    {
        get {return stockCount;}
        set  {stockCount = value;}
    }


    public virtual double Tax()
    {
        /*
        double tax;
        const double Rate = 0.05;
        tax = Price * Rate;
        */
        return 0;
    }

    // Print Method for the Products Class
    public virtual void Print()
    {
        Console.WriteLine("Product ID: {0}", this.id);
        Console.WriteLine("Product Name: {0}", this.name);
        Console.WriteLine("Prodcut Price: {0}", this.price);
        Console.WriteLine("Sold Counter: {0}", this.soldCount);
        Console.WriteLine("Stock Count: {0}", this.stockCount);
        Console.WriteLine();
        //Console.WriteLine(Tax());
    }
}

这是产品类别,如果您需要更多信息,请提出。谢谢,因为当我尝试计算所有库存的总和时,当前的输出总是一直为 0,我不太确定我做错了什么。

class FoodProducts : Products
{

    private string origin;

    public FoodProducts(string id, string name, double price, int soldCount, int stockCount, string origin)
        : base(id, name, price, soldCount, stockCount)
    {
        this.origin = origin;
    }

    private string Origin
    {
        get { return origin; }
        set { origin = value; }
    }

    public override void Print()
    {
        base.Print();
        Console.WriteLine("Origin: {0}", this.Origin);
        Console.WriteLine("------------------");
    }
4

2 回答 2

1

当您在第 4 行创建产品列表时,如何访问第 3 行的产品列表?用以下 4 行替换您的前 4 行代码:

FoodProducts FoodProd1 = new FoodProducts("FP001", "Meat", 15.99, 200, 100, "Australia");
FoodProducts FoodProd2 = new FoodProducts("FP002", "Bread", 2.99, 150, 50, "Italy");
List<Products> ProductList = new List<Products>();
ProductList.items.add(FoodProd1 );
ProductList.items.add(FoodProd2 );
Console.WriteLine("This is the Total Stock For the Store: {0}", CounterStock(ProductList)); 
于 2012-08-30T05:01:00.097 回答
0

您对以下行的使用是错误的:

        CounterStoreStock += Count.StockCount++;

++您使用的运算符是“post-fix”版本。在将当前值添加到Count.StockCount ,它会尝试更新 的值CounterStoreStock

你应该试试这个:

        CounterStoreStock += ++Count.StockCount;

这将在将值添加到Count.StockCount 之前递增CounterStoreStock

Count.StockCount但是,无论如何,您似乎并不想实际更改 的值。

所以,试试这个:

        CounterStoreStock += Count.StockCount;

作为替代方案,您可以尝试使用 LINQ 方法:

static int CounterStock(List<Products> ProductList)
{
    return ProductList.Sum(p => p.StockCount);
}
于 2012-08-30T05:24:13.377 回答