我想在 C# 中使用对象数组显示一些产品
I want to display some products using an object array in C#
我有一个class
public class Product
{
private long id;
private String name;
private String internCode;
private String producer;//
public long Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }
public string InternCode { get => internCode; set => internCode = value; }
public string Producer { get => producer; set => producer = value; }
public void display()
{
Console.WriteLine("Products: " +Id+" "+Name + "[" +InternCode + "] " + Producer);
}
}
这是计划 class
public static void Main(string[] args)
{
Product prod1 = new Product();
Product prod2 = new Product();
Console.WriteLine("The Id for the first product is:");
prod1.Id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The name of the first product is:");
prod1.Name = Console.ReadLine();
Console.WriteLine("The Intern Code is:");
prod1.InternCode = Console.ReadLine();
Console.WriteLine("The producer is:");
prod1.Producer = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("The Id for the second product is:");
prod2.Id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The name for the second product is:");
prod2.Name = Console.ReadLine();
while (prod2.Name == prod1.Name)
{
Console.WriteLine("This product has already been introduced. Please introduce a new product:");
prod2.Name = Console.ReadLine();
}
Console.WriteLine("The intern code is:");
prod2.InternCode = Console.ReadLine();
Console.WriteLine("The producer is:");
prod2.Producer = Console.ReadLine();
Console.WriteLine("The products are:");
Console.WriteLine();
prod1.display();
Console.WriteLine();
prod2.display();
Console.WriteLine();
Console.ReadKey();
}
我想通过用户输入读取产品并使用对象数组 Product[] array1=new Product[] 显示它们,而不是使用 prod1 和 prod2 对象。
请给我任何例子或任何 link 来记录我如何解决 this.Thanks!
我建议您使用列表,因为您有一个添加新产品的方法。要检查产品是否存在,您可以使用 Linq 的任何 IEnumerable 扩展:
var products = new List<Product>();
...
while (!lastuserinput.Equals("exit"))
{
var productName = Console.ReadLine();
if (products.Any(product=>product.Name.Equals(productName))
{
Console.WriteLine("product already exists");
continue;
}
...
productList.Add(new Product
{
...
Name = productName,
...
};
...
}
public class Product
{
public long Id {get; set;}
public String Name {get; set;}
public String InternCode {get; set;}
public String producer {get; set;}
public overrides string ToString()
{
return $"Products: {Id} {Name}[{InternCode}] {Producer}";
}
}
public static void Main(string[] args)
{
var products = new List<Product>();
bool again = true;
while(again)
{
var product = new Product();
Console.WriteLine("The Id for the first product is:");
product.Id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The name of the first product is:");
product.Name = Console.ReadLine();
Console.WriteLine("The Intern Code is:");
product.InternCode = Console.ReadLine();
Console.WriteLine("The producer is:");
product.Producer = Console.ReadLine();
products.Add(product);
Console.WriteLine("\nEnter another product (y/n)?");
again = (Console.ReadKey(true).Key.ToString().ToLower() == "y");
}
Console.WriteLine("\nThe products are:");
foreach(var product in products)
{
Console.WriteLine(product);
}
Console.ReadKey(true);
}
我有一个class
public class Product
{
private long id;
private String name;
private String internCode;
private String producer;//
public long Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }
public string InternCode { get => internCode; set => internCode = value; }
public string Producer { get => producer; set => producer = value; }
public void display()
{
Console.WriteLine("Products: " +Id+" "+Name + "[" +InternCode + "] " + Producer);
}
}
这是计划 class
public static void Main(string[] args)
{
Product prod1 = new Product();
Product prod2 = new Product();
Console.WriteLine("The Id for the first product is:");
prod1.Id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The name of the first product is:");
prod1.Name = Console.ReadLine();
Console.WriteLine("The Intern Code is:");
prod1.InternCode = Console.ReadLine();
Console.WriteLine("The producer is:");
prod1.Producer = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("The Id for the second product is:");
prod2.Id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The name for the second product is:");
prod2.Name = Console.ReadLine();
while (prod2.Name == prod1.Name)
{
Console.WriteLine("This product has already been introduced. Please introduce a new product:");
prod2.Name = Console.ReadLine();
}
Console.WriteLine("The intern code is:");
prod2.InternCode = Console.ReadLine();
Console.WriteLine("The producer is:");
prod2.Producer = Console.ReadLine();
Console.WriteLine("The products are:");
Console.WriteLine();
prod1.display();
Console.WriteLine();
prod2.display();
Console.WriteLine();
Console.ReadKey();
}
我想通过用户输入读取产品并使用对象数组 Product[] array1=new Product[] 显示它们,而不是使用 prod1 和 prod2 对象。 请给我任何例子或任何 link 来记录我如何解决 this.Thanks!
我建议您使用列表,因为您有一个添加新产品的方法。要检查产品是否存在,您可以使用 Linq 的任何 IEnumerable 扩展:
var products = new List<Product>();
...
while (!lastuserinput.Equals("exit"))
{
var productName = Console.ReadLine();
if (products.Any(product=>product.Name.Equals(productName))
{
Console.WriteLine("product already exists");
continue;
}
...
productList.Add(new Product
{
...
Name = productName,
...
};
...
}
public class Product
{
public long Id {get; set;}
public String Name {get; set;}
public String InternCode {get; set;}
public String producer {get; set;}
public overrides string ToString()
{
return $"Products: {Id} {Name}[{InternCode}] {Producer}";
}
}
public static void Main(string[] args)
{
var products = new List<Product>();
bool again = true;
while(again)
{
var product = new Product();
Console.WriteLine("The Id for the first product is:");
product.Id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The name of the first product is:");
product.Name = Console.ReadLine();
Console.WriteLine("The Intern Code is:");
product.InternCode = Console.ReadLine();
Console.WriteLine("The producer is:");
product.Producer = Console.ReadLine();
products.Add(product);
Console.WriteLine("\nEnter another product (y/n)?");
again = (Console.ReadKey(true).Key.ToString().ToLower() == "y");
}
Console.WriteLine("\nThe products are:");
foreach(var product in products)
{
Console.WriteLine(product);
}
Console.ReadKey(true);
}