在尝试计算总计时不显示总库存C#

时间:2012-08-30 04:51:51

标签: c# inheritance

当我调用我的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("------------------");
    }

2 个答案:

答案 0 :(得分: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)); 

答案 1 :(得分:0)

您使用以下行是错误的:

        CounterStoreStock += Count.StockCount++;

您使用的++运算符是&#34; post-fix&#34;版。它尝试在Count.StockCount 更新其当前值CounterStoreStock之后更新 CounterStoreStock += ++Count.StockCount; 的值。

你应该试试这个:

Count.StockCount

这会增加CounterStoreStock 之前它会将值添加到Count.StockCount

但是,您实际上并不想要实际更改 CounterStoreStock += Count.StockCount; 的值。

所以,试试这个:

static int CounterStock(List<Products> ProductList)
{
    return ProductList.Sum(p => p.StockCount);
}

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

{{1}}