【问题标题】:Need to Extract The price And name of a product in a ArrayList需要在 ArrayList 中提取产品的价格和名称
【发布时间】:2020-07-17 19:18:12
【问题描述】:

当我想在 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 方法时,我应该能够将客户当时选择的产品价格保存在一个变量中,以便以后能够计算要支付的价格!谢谢

【问题讨论】:

  • 请注意ArrayList 文档中的蓝色框,其中指出:“我们不建议您使用ArrayList 类进行新开发。相反,我们建议您使用使用通用的List<T> 类。"
  • 我会选择 Product 类而不是字符串。这样会更直接,更容易处理。
  • ...不时检查文档很重要的众多原因之一,尤其是在您刚开始时。知识的错觉是一件危险的事情

标签: c# arrays arraylist


【解决方案1】:

我可以建议使用对象列表吗?使用代码会更干净,更容易。

这是您的产品:

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# 文档。一开始会让人不知所措,但经过大量练习和失败后,您将能够轻松编写应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2015-02-25
    • 1970-01-01
    • 2013-04-29
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多