How to Display an item inside my ArrayList C# console

时间:2015-08-07 02:26:17

标签: c# arraylist console

Okay I'm in process to learn console by using C#. I know the basic of ArrayList which is Add and Delete but I don't know how to display the object. Here is the example of my coding that shows I add some item in my ArrayList. I want to display every details of the item by inserting an item code.

ArrayList arr = new ArrayList();

Console.WriteLine("\n\tADD NEW ITEM\n\nPlease enter item details");
Console.Write("Item code: ");
arr.Add = Console.ReadLine();
Console.Write("Item description: ");
arr.Add = Console.ReadLine();
Console.Write("Price: RM");
arr.Add = double.Parse(Console.ReadLine());
Console.Write("Quantity: ");
arr.Add = int.Parse(Console.ReadLine());

Console.WriteLine("FIND ITEM");
Console.Write("Item code: ");

Okay, I'm lost and have no idea by this last code of mine.

3 个答案:

答案 0 :(得分:1)

在这种情况下,您应该创建一个代表您的项目的类。像这样:

public class Item
{
    public int Code { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; } 
    public int Quantity { get; set; }
}

使用键入的列表来保存项目。它像ArrayList一样工作,但是输入。这是代码:

List<Item> listOfItems = new List<Item>();
//we will create 10 items.
for (int i = 0; i < 10; i++)
{
    //instantiate the object
    Item item = new Item();
    Console.WriteLine("\n\tADD NEW ITEM\n\nPlease enter item details");
    Console.Write("Item code: ");
    item.Code = Console.ReadLine();
    Console.Write("Item description: ");
    item.Description = Console.ReadLine();
    Console.Write("Price: RM");
    item.Price = decimal.Parse(Console.ReadLine());
    Console.Write("Quantity: ");
    item.Quantity = int.Parse(Console.ReadLine());

    //we must add this item to our list
    listOfItems.Add(item);
}

寻找物品:

Console.WriteLine("FIND ITEM");
Console.Write("Item code: ");
int selectedCode = int.Parse(Console.ReadLine());

//use LINQ to search for the item
Item selectedItem = listOfItems.Where(i => i.Code == selectedCode).FirstOrDefault();

//check if an item was found
if (selectedItem != null)
{
   Console.WriteLine("Item found!");
   //print the item
   Console.WriteLine("Code: {0}", selectedItem.Code);
   Console.WriteLine("Description: {0}", selectedItem.Description);
   Console.WriteLine("Price: {0}", selectedItem.Price.ToString("c"));
   Console.WriteLine("Quanitty: {0}", selectedItem.Quantity);
}

未经测试的代码,但应该可以正常工作!

答案 1 :(得分:0)

Iterate through your array with foreach?

foreach ( Object obj in arr )
      Console.Write( "{0}", obj );
      Console.WriteLine();
}

答案 2 :(得分:0)

在这里,您将每个item属性作为元素添加到arraylist中。而是创建一个具有所有项属性的类,然后添加回数组列表。然后我们可以通过数组列表过滤搜索。

static class  Program
{
    static void Main(string[] args)
    {
        ArrayList mainList = new ArrayList();

        string enterMore="yes";
        while (enterMore == "yes" || enterMore == "YES")
        {
            ItemObject item = new ItemObject();
            Console.Write("Item code: ");
            item.itemCode = Console.ReadLine();
            Console.Write("Item description: ");
            item.description = Console.ReadLine();
            Console.Write("Price: RM");
            item.price = double.Parse(Console.ReadLine());
            Console.Write("Quantity: ");
            item.quantity = int.Parse(Console.ReadLine());

            mainList.Add(item);

            Console.Write("{Type 'yes' to enter more} Want to enter more?");
            enterMore = Console.ReadLine();

        }



        string searchMore = "yes";
        while (searchMore == "yes" || searchMore == "YES")
        {

            Console.Write("Enter item code to SEARCH?  ");
            string itemCode = Console.ReadLine();

            //Arraylist stores evarything as an object type. so we are casting object into itemObject
            ArrayList searchResults = new ArrayList(mainList.Cast<ItemObject>()
                                             .Where(d => d.itemCode == itemCode)
                                             .ToList());


            Console.Write("Search Results: ");
            foreach(Object item in searchResults){
                ItemObject obj = (ItemObject)item;
                Console.WriteLine(obj.itemCode);
                Console.WriteLine(obj.description);
                Console.WriteLine();
            }

            Console.Write("{Type 'yes' to search more} Want to search more?");
            searchMore = Console.ReadLine();

        }



    }

}
class ItemObject
{
    public string itemCode { get; set; }
    public string description { get; set; }
    public double price { get; set; }
    public float quantity { get; set; }
}