C#프로그래밍

인벤토리 연습 2

노재두내 2023. 7. 25. 12:48
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class App
    {
        //생성자
        public App()
        {
            Inventory inven = new Inventory(5);
            inven.AddItem(new Item("장검"));
            inven.AddItem(new Item("단검"));
            inven.AddItem(new Item("활"));
            inven.PrintAllItems();

            string searchItemName = "단검";
            Item item = inven.GetItemByName(searchItemName);
            if (item != null)
            {
                Console.WriteLine("{0}을 꺼냈습니다.", item.name);
            }
            else
            {
                Console.WriteLine("{0}을 찾지 못했습니다.", searchItemName);
            }
            inven.PrintAllItems();

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    
    internal class Inventory
    {
        private int capactiy;//최대 수용량
        Item[] items; //아이템 배열 
        int index;
        public Inventory(int capacity)
        {
            this.capactiy = capacity;
            items = new Item[this.capactiy];
        }
        //아이템 추가
        public void AddItem(Item item)
        {
            int maxIndex = this.capactiy - 1;
            if (this.index > maxIndex)
            {
                Console.WriteLine("저장공간이 부족합니다.");
                return;
            }
            this.items[index] = item;
            index++;
        }
        //아이템 이름으로 검색해서 가져오기
        public Item GetItemByName(string searchItemName)
        {
            Console.WriteLine("searchItemName: {0}", searchItemName);

            Item foundItem = null;
            for (int i = 0; i < this.items.Length; i++)
            {
                Item item = this.items[i];
                if (item != null)
                {
                    if (item.name == searchItemName)
                    {
                        this.items[i] = null;
                        foundItem = item;
                        break;
                    }
                }
            }
            return foundItem;
        }
        //모든 아이템 출력
        public void PrintAllItems()
        {
            for(int i = 0; i < this.items.Length; i++)
            {
                Item item = this.items[i];
                if (item == null)
                {
                    Console.WriteLine("[   ]");
                }
                else
                {
                    Console.WriteLine("{0}", item.name);
                }
                
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class Item
    {
        public string name;
        public Item(string name)
        {
            this.name = name;
        }
    }
}