본문 바로가기
C#/수업과제

[C# 7일차] 배열 Inventory 과제

by 왹져박사 2023. 1. 10.
728x90

App Class

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

namespace Day7
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            Inventory inventory = new Inventory(5);
            inventory.PrintAllItems();

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

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

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


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

            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
            Console.WriteLine();

            inventory.PrintAllItems();

            inventory.Arrange();

            inventory.PrintAllItems();

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

        }

    }

}

 

Item Class

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

namespace Day7
{
    class Item
    {
        public string name;
        //생성자
        public Item(string name)
        {
            this.name = name;
        }
    }
}

 

Inventory Class

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

namespace Day7
{
    class Inventory
    {
        Item[] inventory;
        int index = 0;
        int maxIndex;

        //생성자
        public Inventory(int maxIndex)
        {
            this.maxIndex = maxIndex;
            this.inventory = new Item[this.maxIndex];     //5
        }

        //아이템 추가
        public void AddItem(Item item)
        {
            Console.WriteLine();

            bool contains = false;

            for (int i = 0; i < inventory.Length; i++)
            {
                if (inventory[i] != null && inventory[i].name == item.name)
                {
                    contains = true;
                    break;
                }
            }

            if (contains)
            {
                Console.WriteLine("{0}은 이미 있습니다. ", item.name);
            }
            else
            {
                Console.WriteLine("{0}이 추가되었습니다. ", item.name);
                inventory[index] = item;
                index++;
            }


            Console.WriteLine();
        }

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

        public Item GetItemByName(string name)
        {
            //배열을 순회하며 아이템이 있을 경우 
            //아이템의 이름과 searchName이 같으면 해당 아이템을 반환
            //+배열의 요소를 비워줘야 함
            //빈공간이 있다면 정렬


            for (int i = 0; i < this.inventory.Length; i++)
            {
                if (inventory[i] != null && inventory[i].name == name)
                {
                    Console.WriteLine();
                    inventory[i] = null;
                    return inventory[i];

                }
                else
                {
                    Console.WriteLine("{0} 아이템이 존재하지 않습니다. ", name);
                    Console.WriteLine();
                }
            }
            return inventory[index];
        }

        //정렬
        public void Arrange()
        {
            Console.WriteLine();
            Console.WriteLine("정렬중...");
            Console.WriteLine();

            Item[] arr = new Item[maxIndex];
            index = 0;
            for(int i = 0; i < inventory.Length; i++)
            {
                if (inventory[i] != null)
                {
                    arr[index] = inventory[i];
                    index++;
                }
            }
            inventory = arr;
        }

        //인벤토리의 아이템 출력
        public void PrintAllItems()
        {
            //배열에 있는 아이템들을 출력 
            //빈공간은 다음과 같이 표현해주세요 : [    ]
            for (int i = 0; i < inventory.Length; i++)
            {
                if (inventory[i] == null)
                {
                    Console.WriteLine("[    ]");
                }
                else
                {
                    Console.WriteLine(inventory[i].name);
                }
            }

            //장검 
            //단검 
            //[   ]
            //[   ]
            //[   ]

            //장검을 꺼내면 
            //[   ]
            //단검 
            //[   ]
            //[   ]
            //[   ]

            //정렬되었다면 
            //단검 
            //[   ]
            //[   ]
            //[   ]
            //[   ]
        }
    }
}

 

실행 결과

 

어려웠던 점 ! bool 사용 익숙해지기 !

 

728x90