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