2019-10-31 return을 제때 사용하지 않았을 때 Indexer사용해서 인벤토리 만들기

Console Programming/C# Console 2019. 10. 31. 13:48

Program.cs

namespace _1031
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

App.cs

namespace _1031
{
   /* class SampleCollection
    {
        // Declare an array to store the data elements.
        private T[] arr = new T[100];

        // Define the indexer to allow client code to use [] notation.
        public T this[int i]
        {
            get { return arr[i]; }
            set { arr[i] = value; }
        }
    } */     //인덱서의 사용 예제
    public class App
    {
        public App()
        {
            //var stringCollection = new SampleCollection();
            //stringCollection[0] = "Hello, World";
            //Console.WriteLine(stringCollection[0]);

            Inventory inventory = new Inventory(10);

            inventory.AddItem(new Item(100, "장검"));
            inventory.AddItem(new Item(200, "단검"));
            inventory.AddItem(new Item(300, "활"));
            inventory.AddItem(new Item(400, "도끼"));

            inventory.Print();


                       





        }
    }
}

Inventory.cs

using System;
namespace _1031
{
    public class Inventory where T : Item
    {
        private T[] items;
        public T this[int i]
        {
            get
            {
                return this.items[i];
            }
        }

        public Inventory(int capacity)
        {
            this.items = new T[capacity];
        }
        public void AddItem(T item)
        {
            //int cntNull = 0;

           for(int i=0; i<items.Length; i++)
           {
                if(this.items[i]==null)
                {
                    this.items[i] = item;                   
                }
                else
                {
                    //cntNull++;
                }
           }          
           
        }
        public T FindItem(string name)
        {
            //Item foundItem = items[0] as T;
            for(int i=0; i <items.Length; i++)
            {
                if(items[i].Name==name)
                {
                    return items[i];
                }
            }
            return null;
        }
        public void RemoveItem(string name)
        {
            
            for(int i=0; i < items.Length; i++)
            {
                if (this.FindItem(name) != null)
                {
                    items[i] = null;
                }
            }
        }

        public void Print()
        {
            for(int i=0; i < items.Length; i++)
            {
                if(items[i] !=null)
                {
                    Console.WriteLine($"Id: {items[i].Id}, Name: {items[i].Name}");
                }
            }
            Console.WriteLine("---------------------------------------------------\n");           
        }
    }
}

Item.cs

namespace _1031
{
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; private set; }
        public Item(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}

 

 

 

AddItem실행결과

 

 public void AddItem(T item)
        {
            //int cntNull = 0;

           for(int i=0; i<items.Length; i++)
           {
                if(this.items[i]==null)
                {
                    this.items[i] = item;                   
                }
                else
                {
                    //cntNull++;
                }
           }      

 

아이템을 더하고 난 뒤에 return을 넣지 않아서 모든 배열에 처음넣은 아이템이 들어갔다.

 

: