ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 03/15 배열 선언, 인스턴스 생성, 할당, 출력 연습 7-2
    C#/수업내용 2021. 3. 15. 18:20

    인벤토리에 아이템 갯수 추가

     

     

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study03
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Main");
                new App();
            }
        }
    }
    

     

    App.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study03
    {
        public class App
        {
            public App()
            {
                Inventory inventory = new Inventory(10);
                int selectNum = 0;
                while(true)
                {
                    Console.WriteLine("조작을 선택해주세요. 아이템 넣기(1), 아이템 빼기(2), 아이템 검색(3), 아이템 목록 출력(4)");
                    int.TryParse(Console.ReadLine(), out selectNum);                
                    switch(selectNum)
                    {
                        case 1:
                            {
                                string itemName;
                                Console.WriteLine("아이템의 이름을 입력해주세요.");
                                itemName = Console.ReadLine();
                                inventory.AddItem(new Item(itemName, SetItemType()));
                                break;
                            }
                        case 2:
                            {
                                string itemName;
                                Console.WriteLine("아이템의 이름을 입력해주세요.");
                                itemName = Console.ReadLine();
                                inventory.GetItem(itemName);
                                break;
                            }                        
                        case 3:
                            {
                                string itemName;
                                Console.WriteLine("아이템의 이름을 입력해주세요.");
                                itemName = Console.ReadLine();
                                inventory.FindItem(itemName);
                                break;
                            }                        
                        case 4:
                            {
                                inventory.Print();
                                break;
                            }                        
                        default:
                            break;
                    }
                }
            }
            public static Item.eItemType SetItemType()
            {
                int selectNum;
                Item.eItemType itemType = 0;
                Console.WriteLine("아이템 타입을 입력해주세요. 1 : Weapon, 2 : Armor, 3 : Accessories, 4 : Portion");
                int.TryParse(Console.ReadLine(), out selectNum);
                switch(selectNum)
                {
                    case 1:
                        itemType = Item.eItemType.Weapon;
                        break;
                    case 2:
                        itemType = Item.eItemType.Armor;
                        break;
                    case 3:
                        itemType = Item.eItemType.Accessories;
                        break;
                    case 4:
                        itemType = Item.eItemType.Portion;
                        break;
                    default:
                        break;
                }
                return itemType;
            }
            
        }
    }
    

     

    Inventory.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study03
    {
        public class Inventory
        {
            private Item[] items;
            private int capacity;
            public Inventory()
            {
                
            }
            public Inventory(int capacity)
            {
                this.capacity = capacity;
                this.items = new Item[capacity];
            }
            public void AddItem(Item item)
            {
                int idx = 0;
                for (; idx < capacity; idx++)
                {                
                    if (items[idx] == null )
                    {
                        items[idx] = item;
                        Console.WriteLine("{0} 아이템을 넣었습니다.", item.GetName());
                        items[idx].SetItemCount(1);
                        break;
                    }
                    else if(items[idx].GetName() == item.GetName())
                    {
                        Console.WriteLine("{0} 아이템을 넣었습니다.", item.GetName());
                        items[idx].SetItemCount(items[idx].GetItemCount() + 1);
                        break;
                    }
                }
    
                if(idx == capacity)
                {
                    Console.WriteLine("인벤토리에 공간이 부족합니다.");
                }            
            }
            public Item GetItem(string itemName)
            {
                Item item = null;
                for (int i = 0; i < capacity; i++)
                {
                    if (items[i] != null && items[i].GetName() == itemName)
                    {
                        item = items[i];
                        Console.WriteLine("{0} 아이템을 뺐습니다.", item.GetName());
                        if(items[i].GetItemCount() > 1)
                        {
                            items[i].SetItemCount(items[i].GetItemCount() - 1);
                        }
                        else
                        {
                            items[i] = null;
                        }                    
                        break;
                    }
                }
    
                if(item == null)
                {
                    Console.WriteLine("해당하는 아이템이 없습니다.");
                }
                return item;
            }
            public bool FindItem(string itemName)
            {
                bool findItem = false;
                for (int i = 0; i < capacity; i++)
                {
                    if (items[i] != null && items[i].GetName() == itemName)
                    {
                        Console.WriteLine("{0} 아이템을 찾았습니다.", itemName);
                        findItem = true;
                        break;
                    }
                }
                
                if(!findItem)
                {
                    Console.WriteLine("{0} 아이템을 찾지 못했습니다.", itemName);
                }
                return findItem;
            }
            public void Print()
            {
                Item[] tmp = new Item[capacity];
                int idx = 0;
                foreach(Item item in items)
                {
                    if(item != null)
                    {
                        tmp[idx] = item;
                        idx++;
                    }
                }
                items = tmp;
    
                int index = 1;
                foreach(Item item in items)
                {
                    if(item!=null)
                    {
                        Console.WriteLine("{0}. {1} {2} {3}",index, item.GetName(), item.GetItemType(), item.GetItemCount());
                    }
                    else
                    {
                        Console.WriteLine("{0}.", index);
                    }
                    index++;
                }
            }
    
        }
    }
    

     

    Item.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study03
    {
        public class Item
        {
            public enum eItemType
            {
                Weapon,
                Armor,
                Accessories,
                Portion
            }
            private string name;
            private eItemType itemType;
            private int itemCount;
            public Item()
            {
    
            }
            public Item(string name, eItemType itemType)
            {
                this.name = name;
                this.itemType = itemType;
            }
    
            public string GetName()
            {
                return this.name;
            }
            public void SetName(string name)
            {
                this.name = name;
            }
            public eItemType GetItemType()
            {
                return this.itemType;
            }
            public void SetItemType(eItemType itemType)
            {
                this.itemType = itemType;
            }
            public int GetItemCount()
            {
                return this.itemCount;
            }
            public void SetItemCount(int itemCount)
            {
                this.itemCount = itemCount;
            }
        }
    }
    

    Study03.exe
    0.01MB

Designed by Tistory.