我正在尝试使用 Display method() 打印产品详细信息,但它不起作用。创建了产品类并尝试通过从主访问来显示



我正试图通过在product类中创建一个类product和一个显示产品详细信息的方法Display((来打印产品详细信息。

我创建了一个Product数组,并尝试将产品保存在数组中,并尝试使用显示方法显示数组中的产品。

用户应选择是否要添加产品、显示产品或退出

根据选择需要执行操作。


class Product
{
private static int m_intProductCounter = 0;       // Initially Making Product Count = 0

public Product()                               // Constructor for class Product
{
m_intProductCounter++;                  // Whenever new object is created or Constructor is called Count Increases
}

public int ProductCount                         // Properity for Product Count
{
get { return m_intProductCounter; }
}

private int m_intProductid;               // Product Id Variable
public int ProductId                      // properity for Product Id
{
get { return m_intProductid; }
set
{
if (value > 0)
m_intProductid = value;
else
throw new Exception("Invalid Product Id! Please Check Product Id.");
}
}

private string m_strProductName;            //  Product Name Variable
public string ProductName                   //  Properity for Product Name
{
get { return m_strProductName; }
set
{
if (value.Length > 3)
m_strProductName = value;
else
throw new Exception("Invalid Product Name! Please Check Product Name.");
}
}

private DateTime m_dateManufacturedDate = DateTime.UtcNow;           // Product Manufactured date variable


//private DateTime m_dateExpiryDate;
public DateTime ExpiryDate                                      // Product Expiry Date Calculation
{
get { return m_dateManufacturedDate.AddYears(2); }
}

private int m_intProductQuantity;                  // Product Quantity variable
public int ProductQnty
{
get { return m_intProductQuantity; }             // Properity for Product Quantity
set
{
if (value > 0 && value <= 500)
m_intProductQuantity = value;
else if (value < 0)
throw new Exception("Invalid Quantity! Product Quantity cannot be Less Than 0");
else
throw new Exception("Invalid Quantity! Product Quantity Cannot be more than 500.");
}
}

private decimal m_decProductPrice;                     // Product Price variable
public decimal ProductPrice                         // Properity for Product Price
{
get { return m_decProductPrice; }
set
{
if (value > 0)
m_decProductPrice = value;
else
throw new Exception("Invalid Producr Price! Product price Cannot Be less than 0");
}
}

private decimal m_decDiscountPrice;                 // Product Discount variable
public decimal Discount                             // Properity for Product Discount
{
get { return m_decDiscountPrice; }
set
{
if (value <= 45 && value > 0)
m_decDiscountPrice = value;
else if (value < 0)
throw new Exception("Product Discount can be either 0 or morethan that, but can't be zero.");
else
throw new Exception("Product discount can't be more than 45");
}
}
public string Display()                                                  // Displaying Product Details.
{
StringBuilder display = new StringBuilder();
display.Append("Product ID = " + ProductId + "n");
display.Append("Product Name = " + ProductName + "n");
//disp.Append("Product Manufactured Date = " + );
display.Append("Product Expiry Date = " + ExpiryDate + "n");
display.Append("Product Price Per 1 Quantity = " + ProductPrice + "n");
display.Append("Product Quantity = " + ProductQnty + "n");
display.Append("Product Discount per 1 Quantity= " + Discount + "n");

return display.ToString();
}
}
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter 1 to ADD product.n Enter 2 to Display Product. n Enter 3 to Exit.");
string Instruction = Console.ReadLine();
Product[] products = new Product[3];
switch (Instruction)
{
case "1":
{
Product p1 = new Product();
Console.Write("Enter Producr Id : ");
p1.ProductId = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter Product Name : ");
p1.ProductName = Console.ReadLine();

Console.Write("Enter Product Quantity : ");
p1.ProductQnty = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter Product Price : ");
p1.ProductPrice = Convert.ToDecimal(Console.ReadLine());

Console.Write("Enter Product Discount in number without % symbol : ");
p1.Discount = Convert.ToDecimal(Console.ReadLine());


products.Append(p1);

Console.WriteLine("n n n" + "Product Added Succesfully n " + "ThankYou.");
Console.WriteLine("Total Products Entered = " + p1.ProductCount);


}
break;
case "2":
{
Console.WriteLine(products[0].Display());
break;
}
}
if (Instruction == "3")
break;
}
}

}

为什么要使用数组进行产品收集?在内部,当您总是为产品创建新阵列时(内部,当:Product[]products=new Product[3];(!

在这种情况下,使用List或任何动态集合,将您的主要方法更改为:

static void Main(string[] args)
{
var products = new List<Product>();
while (true)
{
Console.WriteLine("Enter 1 to ADD product.n Enter 2 to Display Product. n Enter 3 to Exit.");
string Instruction = Console.ReadLine();

switch (Instruction)
{
case "1":
{
var p1 = new Product();
Console.Write("Enter Producr Id : ");
p1.ProductId = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter Product Name : ");
p1.ProductName = Console.ReadLine();

Console.Write("Enter Product Quantity : ");
p1.ProductQnty = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter Product Price : ");
p1.ProductPrice = Convert.ToDecimal(Console.ReadLine());

Console.Write("Enter Product Discount in number without % symbol : ");
p1.Discount = Convert.ToDecimal(Console.ReadLine());

products.Add(p1);

Console.WriteLine("n n n" + "Product Added Succesfully n " + "ThankYou.");
Console.WriteLine("Total Products Entered = " + p1.ProductCount);


}
break;
case "2":
{
foreach (var item in products)
Console.WriteLine(item.Display());
break;
}
}
if (Instruction == "3")
break;
}
}

最新更新