수업 012 (04.21 2020) List<> add, remove, enum

2020. 4. 21. 11:19
728x90

1. App.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._013_2
{
    class App
    {
        public App()
        {
            List<Item> herbBags = new List<Item>();
            HerbBag herbBag = new HerbBag();
 
            Item item1 = new Item("물망초"eItemType.Herb);
            Item item2 = new Item("장검"eItemType.Weapon);
 
            herbBag.AddItem(item1);
            herbBag.AddItem(item2);
            herbBag.RemoveItem("물망초");
        }
    }
}
 
 

 

 

2. HerbBag.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._013_2
{
    class HerbBag
    {
        public int sellPrice;
        public List<Item> itemList;
 
        // 생성자
        public HerbBag()
        {
            this.sellPrice = 2500;
            this.itemList = new List<Item>();
        }
 
        public void AddItem(Item item)
        {
            if (itemList.Count >= 12)
            {
                return;
            }
            else
            {
                if (item.itemType == eItemType.Herb)
                {
                    this.itemList.Add(item);
                    Console.WriteLine("{0}을 넣었습니다. \n"item.name);
                }
                else
                {
                    Console.WriteLine("{0}을 넣을 수 없습니다.\n"item.name);
                }
            }
        }
 
        public void RemoveItem(string name)
        {
            for(int i = 0; i < itemList.Count; i++)
            {
                if (itemList[i].name == name)
                {
                    Console.WriteLine("{0}를 가방에서 뺐습니다. \n", itemList[i].name);
                    this.itemList.Remove(itemList[i]);
                }
 
                else
                {
                    Console.WriteLine("{0}을 뺄 수 없습니다. \n", itemList[i].name);
                }
            }
        }
    }
}
 
 

 

3. Item.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._013_2
{
    public enum eItemType
    {
        Herb,
        Potion,
        Weapon
    }
    class Item
    {
        public string name;
        public eItemType itemType;
 
        // 생성자
        public Item(string name, eItemType itemType)
        {
            this.name = name;
            this.itemType = itemType;
        }
    }
}
 
 
 

 

4. Result

728x90

BELATED ARTICLES

more