> App.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200414_array
{
    class App
    {

        public App()
        {
            Inventory inventory = new Inventory(5);
            inventory.AddItem(new Item("장검"));
            inventory.AddItem(new Item("궁"));
            inventory.AddItem(new Item("투구"));
            inventory.AddItem(new Item("장화"));
            inventory.AddItem(new Item("벨트"));
            // inventory.AddItem(new Item("장검6"));
           
            Console.WriteLine();

            inventory.PrintItemNames();
            Console.WriteLine();

            Console.Write("[궁] 검색: ");
            Item findedItem = inventory.FindItem("궁");
            if (findedItem == null) { Console.WriteLine("찾는 아이템 없음"); }

            Console.WriteLine();

            Console.Write("[장화] 꺼내기: ");
            Item getedItem = inventory.GetItem("장화");
            if (getedItem == null)
            {
                Console.WriteLine("꺼낼 아이템 없음");
            }
            else
            {
                Console.WriteLine($"{getedItem.name}을 꺼냈습니다.");
            }

            Console.WriteLine();
            inventory.PrintItemNames();

        }
    }
}

 

> Inventory.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200414_array
{
    class Inventory
    {
        public Item[] items;
        int itemIndex = 0;

        public Inventory(int capacity)
        {
            items = new Item[capacity];
            Console.WriteLine("인벤토리가 생성되었습니다.");
        }

        public void AddItem(Item item)
        {
            if (this.itemIndex < this.items.Length)
            {
                this.items[this.itemIndex] = item;
                this.itemIndex++;
            }
        }

        public Item GetItem(String itemName)
        {
            Item item = null;

            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i] != null)
                {
                    if (itemName == this.items[i].name)
                    {
                        item = this.items[i];
                        this.items[i] = null;
                    }
                }
            }
            return item;
        }

        public Item FindItem(String itemName)
        {
            Item item = null;
            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i] != null)
                {
                    if (itemName == this.items[i].name)
                    {
                        Console.WriteLine(this.items[i].name);
                        item = this.items[i];
                        break;
                    }
                }
            }
            return item;
        }

        public int GetCount()
        {
            int itemCount = 0;
            foreach (Item item in this.items)
            {
                if (item != null)
                {
                    itemCount++;
                }
            }
            return itemCount;
        }

        public void PrintItemNames()
        {
            foreach (Item item in this.items)
            {
                if (item == null)
                {
                    Console.WriteLine("[  ]");
                }
                else
                {
                    Console.WriteLine(item.name);
                }
            }

            /*
                 for (int i = 0; i < this.items.Length; i++)
                {
                    Console.WriteLine(this.items[i].name);
                }
            */
        }
    }
}

 

> Item.cs

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200414_array
{
    class Item
    {
        public string name;

        public Item(string name)
        {
            this.name = name;
            Console.WriteLine($"아이템 {this.name}이 생성되었습니다.");
        }
    }
}

+ Recent posts