C#

인벤토리1

송현호 2023. 7. 24. 18:11
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    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 = "장검";
            inven.GetItemByName(searchItemName);
        }
    }
}

 

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

namespace Starcraft
{
    public class Item
    {
        public string Name { get; set; }
        //생성자 
        public Item(string name)
        {
            this.Name = name;
            Console.WriteLine("{0}이 생성되었습니다.", this.Name);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class Inventory
    {
        Item[] items;
        int count;
        public Inventory(int capacity)
        {
            //생성자
            this.items = new Item[capacity];
        }

        public void AddItem(Item item)
        {
            this.items[count] = item;
            count++;
        }

        public void PrintAllItems()
        {
            foreach (Item item in this.items)
            {
                if (item != null)
                    Console.WriteLine("{0}", item.Name);
            }
        }

        public Item GetItemByName(string searchItemName)
        {
            for (int i = 0; i < this.items.Length; i++)
            {
                if (items != null)
                {
                    if (items[i].Name == searchItemName)
                    {
                        Console.WriteLine("찾았다 {0}", items[i].Name);
                        items[i] = null;
                        break;
                    }
                }
            }
            return null;
        }
    }
}

 

'C#' 카테고리의 다른 글

2차원 배열  (0) 2023.07.25
2048  (0) 2023.07.25
마린 메딕  (0) 2023.07.24
마린 vs 저글링  (0) 2023.07.21
플레이어 몬스터 무기  (0) 2023.07.21