需要在ArrayList中提取产品的价格和名称

时间:2020-07-17 19:18:12

标签: c# arrays arraylist

当我想提取c#中的ArrayList中包含的产品价格时遇到问题。

    namespace Proyecto_Nahuel
{
    class Program
    {

        ArrayList producto = new ArrayList();
        int numero = 5;
        int numero_precio = 5;
        int precio;
        public void productos()
        {

            foreach (string str in producto)
            {
                Console.WriteLine(str);
            }
        }
        public void inicio()
        {
            producto.Add("|Categoria |" + " Producto | " + " Precio\n");
            producto.Add("1- |Golosina |" + " Chupetin | " + 50 + "\n");
            producto.Add("2- |Bebida |" + " Coca Cola | " + 30 + "\n");
            producto.Add("3- |Snack's |" + " Doritos | " + 45 + "\n");
            producto.Add("4- |Galletas |" + " Chocolina | " + 39 + "\n");
        }
        public void Agregar_Producto()
        {
            
            Console.Clear();
            Console.WriteLine("Ingrese la categoria del producto a agregar(Golosina / Bebida / Snack's / Galletas");
            string Categoria_Nueva = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Ingrese el Nombre del producto: ");
            string Producto_Nuevo = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("Ingrese el Precio del Nuevo Producto: ");
            int Precio_Nuevo = int.Parse(Console.ReadLine());
            producto.Add($"{numero}- |{Categoria_Nueva} |" + $" {Producto_Nuevo} | " + $"{Precio_Nuevo}\n");
            numero += 1;

        }
        public void Comprar_Productos()
        {
            Console.Clear();
            foreach (string str in producto)
            {
                Console.WriteLine(str);
            }
            Console.WriteLine("Ingrese El numero del Producto que desea comprar");
            int Numero_producto = int.Parse(Console.ReadLine());
            Console.Clear();
            Console.WriteLine("Producto Elegido: " + producto[(Numero_producto)]);
            Console.WriteLine("Cuantas unidades Desea?");
            int unidades = int.Parse(Console.ReadLine());

        }

您可以看到用户输入的要存储在列表中的新产品的值存储在Precio_New变量中。 我需要的是,当调用方法Comprar_Products时,我应该能够将客户当时选择的产品价格保存在变量中,以便以后能够计算出要支付的价格!谢谢

1 个答案:

答案 0 :(得分:0)

我可以建议使用对象列表吗?使用代码将更加简洁方便。

这是您的产品:

public class Producto 
{
    public int Numero { get; set; }
    public string Categoria { get; set; }
    public string Nombre { get; set; }
    public int Precio { get; set; }
}

然后,您可以在代码中创建list个产品,以后再添加。

var productos = new List<Producto>()
{
    new Producto(){ Numero = 1, Categoria = "Golosina", Nombre = "Chupetin", Precio = 50 },
    new Producto(){ Numero = 2, Categoria = "Bebida", Nombre = "Coca Cola", Precio = 30 }
};

productos.Add(new Producto(){ Numero = 3, Categoria = "Snack's", Nombre = "Doritos ", Precio = 45});

如果要显示如上,请使用此代码。 $符号为string interpolation

  foreach(var producto in productos)
      Console.WriteLine($"{producto.Numero} - {producto.Categoria} | {producto.Nombre} | {producto.Precio}");

如果要在列表中搜索给定的产品,只需使用linq扩展名(WhereSingleOrDefault):

    var seleccion = "Chupetin";
    var productoSeleccionado = productos.Where(p => p.Nombre == seleccion).SingleOrDefault();
    var seleccionPrecio = productoSeleccionado.Precio;

我将由您自行决定将这些代码与您希望应用程序的工作方式结合起来。我在此答案上也有链接,可以将您定向到官方C#文档。乍一看,这将是不胜枚举的,但是通过大量的实践和失败,您将能够轻松编写应用程序代码。