수업 012 (04.21 2020) List<> HerbBag

2020. 4. 22. 00:13
728x90

1. App.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._013_3
{
    class App
    {
        //생성자 
        public App()
        {
            //약초가방 생성 
            HerbsBag bag = new HerbsBag();
            bag.AddItem(new Item(1000"iiii""물망초"Item.eItemType.Herbs));
            bag.AddItem(new Item(2000"gggg""장검"Item.eItemType.Weapon));
 
            Item item1 = bag.GetItem("물망초");
            Item item2 = bag.GetItem(1000);
 
            bool haveItem1 = bag.HaveItemByName("물망초");
            Console.WriteLine(haveItem1);
 
            bool haveItem2 = bag.HaveItemByName("1000");
            Console.WriteLine(haveItem2);
            Console.WriteLine();
 
            List<Item> dropItemList = new List<Item>();
            dropItemList.Add(new Item(1000"95713e12""물망초"Item.eItemType.Herbs));
            dropItemList.Add(new Item(1000"1b327423""평온초"Item.eItemType.Herbs));
            dropItemList.Add(new Item(1000"eb45a453""평은엽수 덤블"Item.eItemType.Herbs));
                                     // id      guid
            Console.WriteLine("바닥에 있는 아이템 목록");
            Console.WriteLine("=======================");
            bag.AddItem(dropItemList);
            Console.WriteLine("=======================");
 
            List<Item> itemList1 = bag.GetAllItems();
            // 확인하기
            foreach (Item item in itemList1)
            {
                Console.WriteLine(item.name);
            }
            Console.WriteLine();
 
            List<Item> itemList2 = bag.GetItemsExceptByName("물망초 \n");
            // 확인하기
            foreach (Item item in itemList2)
            {
                Console.WriteLine(item.name);
            }
 
            Console.WriteLine();
            int count = bag.GetItemCountById(1000);
        }
    }
}
 
 

 

 

2. HerBag.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._013_3
{
    class HerbsBag
    {
        public int sellPrice;   //판매 가격 
        public List<Item> itemList; //아이템들 
        public int maxItemCount = 12;   //최대 갯수 
 
        //생성자 
        public HerbsBag()
        {
            this.sellPrice = 2500;
            this.itemList = new List<Item>();
        }
 
        //아이템 넣기
        public void AddItem(Item item)
        {
            //아이템 타입이 약초인가?
            if (item.itemType == Item.eItemType.Herbs)
            {
                //리스트에 추가 
                this.itemList.Add(item);
                Console.WriteLine("{0}이 추가 되었습니다. ({1}/{2})"item.name, this.itemList.Count, this.maxItemCount);
            }
            else
            {
                //약초만 넣을수 있습니다 출력 
                Console.WriteLine("약초만 넣을수 있습니다");
            }
        }
 
        public void AddItem(List<Item> dropItemList)
        {
            for (int i = 0; i < dropItemList.Count; i++)
            {
                if (dropItemList[i].itemType == Item.eItemType.Herbs)
                {
                    this.itemList.Add(dropItemList[i]);
                    Console.WriteLine("{0}이 추가 되었습니다.", dropItemList[i].name);
                }
            }
        }
 
        //아이템 빼기 
        public Item GetItem(string name)
        {
 
            Item foundItem = null;
 
            foreach (Item item in this.itemList)
            {
                if (item.name == name)
                {
                    foundItem = item;
                    break;
                }
            }
 
            if (foundItem != null)
            {
                this.itemList.Remove(foundItem);
            }
 
            return foundItem;
        }
 
 
        public Item GetItem(int id)
        {
            Item foundItem = null;
 
            foreach (Item item in this.itemList)
            {
                if (item.id == id)
                {
                    foundItem = item;
                    break;
                }
            }
            // Item foundItem = this.itemList.Find(x => x.id == id); 변경가능
 
            if (foundItem != null)
            {
                this.itemList.Remove(foundItem);
            }
            return foundItem;
        }
 
        public List<Item> GetAllItems()
        {
            return this.itemList;
        }
 
        public List<Item> GetItemsExceptByName(string name)
        {
            List<Item> newList = new List<Item>();
 
            foreach (Item item in itemList)
            {
                if (item.name != name)
                {
                    newList.Add(item);
                }
            }
            return newList;
        }
 
        public int GetItemCountById(int id)
        {
            int itemCount = 0;
 
            foreach (Item item in itemList)
            {
                if (item.id == id)
                {
                    itemCount++;
                }
            }
            return itemList.Count;
        }
 
        public void PrintAllItems(string name)
        {
            if (this.itemList.Count <= 0)
            {
                Console.WriteLine("가방에 아무것도 없습니다.");
            }
            else
            {
                foreach (var item in this.itemList)
                {
                    Console.WriteLine(item.name);
                }
            }
 
        }
 
        public bool HaveItemByName(string name)
        {
            bool isExist = false;
 
            foreach (Item item in itemList)
            {
                if (item.name == name)
                {
                    isExist = true;
                    break;
                }
            }
            return isExist;
        }
 
        public bool HaveItemByName(int id)
        {
            bool isExist = false;
 
            foreach (Item item in this.itemList)
            {
                if (item.id == id)
                {
                    isExist = true;
                    break;
                }
            }
            return isExist;
        }
    }
}
 

 

 

3. Item.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._013_3
{
    class Item
    {
        public enum eItemType
        {
            Herbs, Weapon
        }
 
        public string name;         //이름 
        public eItemType itemType;  //아이템 타입 
        public int id;
        public string guid;
 
        //생성자 
        public Item(int id, string guid, string name, eItemType itemType)
        {
            this.id = id;
            this.name = name;
            this.itemType = itemType;
        }
    }
}
 
 

 

4. Result

728x90

BELATED ARTICLES

more