C#Collections Shopping Cart

This is an exercise of objects and collections, the creation of objects, some basic functions of collections, such as adding, editing, deleting and so on.

 
The object is the goods of the online shop.Insus.NET Only two attributes are added to it, the Key and name ItemName of the object's ID, and two constructors. The last method is to override the ToString() method.

 

 class Item
    {
        private int _key;
        public int Key
        {
            get
            {
                return _key;
            }
           set
            {
                _key = value;
            }
        }

        private string _ItemName;

        public string ItemName
        {
            get { return _ItemName; }
            set { _ItemName = value; }
        }

        public Item()
        {

        }

        public Item(int key, string itemName)
        {
            this._key = key;
            this._ItemName = itemName;
        }

        public override string ToString()
        {
            return string.Format("ID: {0}; Name: {1}. ",_key,_ItemName);
        }
    }
Source Code

 

With the object, you can create your shopping cart Shopping Cart:

 

 

 class ShoppingCart
    {
        private SortedList<int, Item> _sl = new SortedList<int, Item>();

        public void Add(Item item) //Object Addition
        {
            this._sl.Add(item.Key, item);
        }

        public void Edit(Item item) //Edit Object
        {
            if (this._sl.ContainsKey(item.Key))
            {
                this._sl[item.Key] = item;
            }
        }

        public void Delete(Item item) //Delete Object
        {
            this._sl.Remove(item.Key);
        }

        public Item this[int key] //Indexer
        {
            get
            {
                if (!this._sl.ContainsKey(key))
                {
                    return null;
                }
                else
                {
                    return this._sl[key];
                }
            }
        }

        public virtual int Count //Number of objects in collection
        {
            get
            {
                return this._sl.Count;
            }
        }

        public virtual IEnumerable<Item> Items //Get all objects
        {
            get
            {
                return this._sl.Values;
            }
        }
    }
Source Code

 

Below is a collection shopping cart written on the console test:

 

 class Program
    {
        static void Main(string[] args)
        {
            ShoppingCart sc = new ShoppingCart();

            var item1 = new Collections.Item();
            item1.Key = 1;
            item1.ItemName = "Huawei V8";
            sc.Add(item1);

            var item2 = new Collections.Item();
            item2.Key = 2;
            item2.ItemName = "Huawei V9";
            sc.Add(item2);

            var item3 = new Collections.Item();
            item3.Key = 3;
            item3.ItemName = "Huawei V10";
            sc.Add(item3);

            Console.WriteLine("Using an indexer, output objects:");
            Console.WriteLine(sc[3].ToString());

            Console.WriteLine("Number of objects in the collection:");
            Console.WriteLine(sc.Count);

            Console.WriteLine("List all objects:");
            sc.Items.ForEach(delegate (Collections.Item item)
            {
                Console.WriteLine(item.ToString());
            });
        }
    }
Source Code


Press Ctrl + F5 to output the result:

 

Finally, the ability to edit Edit and delete Delete is demonstrated:

 

var item4 = new Collections.Item();
            item4.Key = 2;
            item4.ItemName = "Huawei Mate10";
            sc.Edit(item4);

            Console.WriteLine("List all objects after editing:");
            sc.Items.ForEach(delegate (Collections.Item item)
            {
                Console.WriteLine(item.ToString());
            });


            var item5 = new Collections.Item();
            item5.Key = 1;
            sc.Delete(item5);

            Console.WriteLine("List all objects after deletion:");
            sc.Items.ForEach(delegate (Collections.Item item)
            {
                Console.WriteLine(item.ToString());
            });
Source Code

 

Run to see the results:

Tags: C#

Posted on Wed, 27 May 2020 12:24:16 -0400 by Merdok