인벤토리에 아이템 넣고 빼기

Console(콘솔)/실습 2019. 10. 10. 15:28

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Syntax51

{

    class Program

    {

        static void Main(string[] args)

        {

            new App();

        }

    }

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Syntax51

{

    class App

    {

        public App()

        {

            var inventory = new Inventory();

            inventory.AddItem(new Item(100"장검"));

            inventory.AddItem(new Item(200"단검"));

            inventory.AddItem(new Item(300"대검"));

            inventory.AddItem(new Item(400"창"));

            inventory.AddItem(new Item(500"활"));

 

            inventory.FindItemByName("장검");           

            inventory.FindIndexofItemByName("단검");

            inventory.DisplayInventoryItemNames();

            inventory.RemoveItemByName("장검");

            inventory.DisplayInventoryItemNames();

            inventory.AddItem(new Item(600"도끼"));

            inventory.DisplayInventoryItemNames();

            inventory.AddItem(new Item(600"도끼"));

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Syntax51

{

    class Inventory

    {

        public Item[] arrItems;

        public int index=0;

        public Inventory()

        {

            this.arrItems = new Item[5];

        }

        public void AddItem(Item item)

        {

            for (int i = 0; i < arrItems.Length; i++)

            {

                if (arrItems[i] == null)

                {

                    this.arrItems[i] = item;

                    Console.WriteLine(item.name+"이 생성되었습니다.");

                    return;

                }               

            }

            Console.WriteLine("공간이부족합니다.");

            

        }

        public void RemoveItemByName(string name)

        {

 

            for (int i = 0; i < arrItems.Length; i++)

            {

                if (arrItems[i].name == name)

                {

                    arrItems[i] = null;

                    break;

                }

                else

                {

                    Console.WriteLine("해당상품이 없습니다.");

                }

            }

        }

        public void DisplayInventoryItemNames()

        {

            for (int i = 0; i < arrItems.Length; i++)

            {

                if (arrItems[i] != null)

                {

                    Console.Write(arrItems[i].name+" ");

                }                             

            }

            Console.WriteLine();

        }

        public int FindIndexofItemByName(string name)

        {

            for (int i = 0; i < arrItems.Length; i++)

            {

                if (arrItems[i].name != null&& arrItems[i].name == name)

                {

                    Console.WriteLine(arrItems[i].id);

                    return arrItems[i].id;

                }              

            }

            Console.WriteLine("해당 아이템을 찾을수 없습니다.");

            return 0;

 

        }

        public Item FindItemByName(string name)

        {

            for (int i = 0; i < arrItems.Length; i++)

            {

                if (arrItems[i] != null && name == arrItems[i].name)

                {

                    Console.WriteLine($"{arrItems[i].name}을 찾았습니다.");

                    return arrItems[i];

                }

                Console.WriteLine("아이템을 찾을 수 없습니다.");

                return null;

            }

            

            return null;

 

        }   

    }

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace Syntax51

{

    class Item

    {

        public int id;

        public string name;

        public Item(int id, string name)

        {

            this.id = id;

            this.name = name;

        }

    }

}

 

'Console(콘솔) > 실습' 카테고리의 다른 글

UIPanel 제작  (0) 2019.10.14
Class mathod  (0) 2019.10.02
class mathod  (0) 2019.10.02
Claas를 이용한 버거 만들기  (0) 2019.10.01
method문 사용  (0) 2019.09.30
: