C#

인벤토리2

s0002 2023. 1. 9. 18:01

인벤토리에 아이템 넣기

인벤토리에서 아이템 꺼내기

인벤토리에 들어 있는 아이템 개수 세기

인벤토리에 들어있는 아이템 목록 출력

인벤토리에 들어있는 아이템 정렬

 using System;

namespace Study08
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Study08
{
    class App
    {

        //생성자
        public App()
        {
            Inventory inventory = new Inventory(5);

            inventory.AddItem(new Item("장검"));
            inventory.AddItem(new Item("장검")); //이미 장검이 있습니다
            inventory.AddItem(new Item("단검"));

            int count = inventory.GetItemCount();
            Console.WriteLine("count:{0}", count); //2

            string searchName = "장검";
            
            Item item = inventory.GetItemByName(searchName);

            if (item == null)
                Console.WriteLine("{0}을(를) 찾을 수 없습니다", searchName);
            else
                Console.WriteLine("{0}을(를) 찾았습니다", searchName);

            //있다면 1, 없다면 2
            count = inventory.GetItemCount();  
            Console.WriteLine("count:{0}", count); //1

            inventory.PrintAllItems();

            inventory.Arrange();

            inventory.PrintAllItems();

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

            inventory.PrintAllItems();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Study08
{
    class Inventory
    {
        Item[] items;
        int size;
        int count;

        //생성자
        public Inventory(int size)
        {
            this.size = size;
            this.items = new Item[this.size];
        }

        int index = 0;
        public void AddItem(Item item)
        {
            if (items[index] == null)
            {
                for(int i = 0; i <= index; i++)
                {
                    if (items[i] != null && items[i].name == item.name)
                    {
                        Console.WriteLine("이미 {0}이(가) 있습니다", item.name);
                        break;
                    }
                    else
                    {
                        items[index] = item;
                        Console.WriteLine("인벤토리에 {0}을(를) 추가합니다", item.name);
                        index++;
                        break;
                    }
                 // 원래 이렇게 만드려고 했는데 null 처리 어떻게 해야될지 모르겠어서 다른 사람거 참고함
                 /*
                       if (items[i].name != item.name)
                       {
                           items[index] = item;
                           Console.WriteLine("인벤토리에 {0}을 추가합니다", item.name);
                           index++;
                           break;
                       }
                       else if ( items[i].name == item.name)
                       {
                           Console.WriteLine("이미 {0}이 있습니다", item.name);
                           break;
                       }
                    */
                }
            }  
        }

        public int GetItemCount()
        {
            count = 0;
            for(int i = 0; i < items.Length; i++)
            {
                if (items[i] != null) 
                    count++;
            }
            //배열이 가지고 있는 아이템의 개수
            return count;
        }

        Item returnItem;
        public Item GetItemByName(string searchName)
        {
            for(int i = 0; i < items.Length; i++)
            {
                if (items[i] == null)
                    returnItem = null;
                else if(items[i].name==searchName)
                {
                    returnItem = items[i];//반환 값에 저장
                    items[i] = null;
                    index--;//다음에 아이템 추가할 인덱스 바꿔야 함                    
                    break;
                }
            }
            return returnItem;
        }

        public void PrintAllItems()
        {
            Console.WriteLine();
            for(int i = 0; i < items.Length; i++)
            {
                if (items[i] == null)
                {
                    Console.WriteLine("[ ]");
                }
                else
                    Console.WriteLine("{0}", items[i].name);
            }
            Console.WriteLine();
        }

        public void Arrange()
        {
            Console.WriteLine("정렬중...");
            for(int i = 0; i < items.Length; i++)
            {
                if (items[i] == null)
                    continue;
                else if (items[i] != null)
                {
                    for (int j = 0; j < i; j++)
                    {
                        if (items[i - j - 1] == null)
                        {
                            items[i - j - 1] = items[i - j];
                            items[i - j] = null;
                        }
                    }
                }  
            }     
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Study08
{
    class Item
    {
        public string name;
        public Item(string itemName)
        {
            this.name = itemName;
        }
    }
}

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

2차원 배열  (0) 2023.01.10
List<T>를 이용한 인벤토리 만들기  (0) 2023.01.10
방향키 입력 받아 이동  (0) 2023.01.09
배열에서 찾으려는 숫자의 개수 구하기  (0) 2023.01.09
학생 관리  (0) 2023.01.09