我想在C#中使用对象数组显示一些产品

时间:2019-03-11 21:09:11

标签: c# arrayobject

我上课

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);
    }
}

这是程序类

 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对象显示它们。 请给我任何示例或任何链接以记录我如何解决此问题。谢谢!

2 个答案:

答案 0 :(得分:2)

我建议您使用列表,因为您有添加方法来添加新产品。要检查产品是否存在,可以使用Linq的IEnumerable的Any扩展:

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,
        ...
    };
...
}

答案 1 :(得分:0)

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);            
}