티스토리 뷰

C#/수업 과제

배열을 이용한 인벤토리 만들기

Game Client Lee Hwanguk 2023. 1. 10. 01:38

#App

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

namespace test02
{
    internal class App
    {
        public App()
        {
            Inventory inventory = new Inventory(5);
            inventory.AddItem(new Item("장검")); //Item클래스의 name을 할당하고 inventort 인스턴스에 메서드를 호출한다
            inventory.AddItem(new Item("장검"));  //이미 장검이 있습니다. 
            inventory.AddItem(new Item("단검"));
            int count = inventory.GetItemCount(); //return받은 값을 count에 할당
            Console.WriteLine("count: {0}", count); //2
            string searchName = "장검";
            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

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

namespace test02
{
    class Inventory
    {
        private int capacity;   //최대 용량 
        public Item[] items;   //아이템들을 관리 배열 
        int itemCount;
        public Inventory(int capacity) //인벤토리의 최대용량을 할당받음
        {
            this.capacity = capacity; 
            this.items = new Item[this.capacity]; //items 배열 생성 /용량은 인벤토리의 최대용량
        }

        public void AddItem(Item item) //아이템 이름을 할당받고 하나씩 넣는다
        {
            for (int i = 0; i < items.Length; i++) //items 배열을 반복하며 
            {
                if (items[i] == null) //[i]번째 값이 null일때
                {
                    Console.WriteLine("{0} 을 인벤토리에 넣었습니다", item.name);
                    items[i] = item; //아이템을 추가하고 items[0]="장검"
                    break; //for문을 멈춘다

                }
                else if (items[i].name == item.name) //"items[i].name"의 아이템이름이 할당받은 아이템 이름과 같다면
                {
                    Console.WriteLine("{0} 은 인벤토리에 이미있습니다", item.name);
                    break;  //for문을 멈춘다
                }//배열 items의 초기값은 모두 null이다 그래서 for문으로 반복하며 
            }
        }

        public int GetItemCount()
        {
            //배열이 가지고있는 아이템의 갯수 
            itemCount =0;
            for (int i=0; i < items.Length; i++)
            {
                if (items[i] != null)
                {
                    itemCount++; //{장검,단검,null,null,null} //아이템은 2개있다
                }
                else { continue; }
            }
            return itemCount; //items에 있는 아이템 갯수를 itemCount에 할당 후 return한다
        }

        public int GetItemByName(string searchName)
        {
            int count = 0;
            //배열을 순회하며 아이템이 있을경우 //foreach문 
            //아이템의 이름과 searchName이 같으면 
            //해당 아이템을 반환 //return
            //배열의 요소를 비워줘야함 
            //빈공간이 있다면 정렬 해야함 
            for(int i=0; i< items.Length; i++)
            {
                if (items[i]!=null)
                {
                    count=count + 1;
                }
                return count;
            }
        }

        public void PrintAllItems()
        {
            //배열에 있는 아이템들을 출력 
            //빈공간은 다음과 같이 표현해주세요 : [    ]
            //장검 
            //단검 
            //[   ]
            //[   ]
            //[   ]

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

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

        }
}   }

 

#Item

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

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

        }
    }
}

# GetItemCount 까지만 성공...

#GetItemByName은 클래스 설정을 잘못한것같다 다시 확인해봐야겠다

'C# > 수업 과제' 카테고리의 다른 글

인벤토리 만들기  (0) 2023.01.25
List<T>를 이용하여 인벤토리를 만들어보자  (0) 2023.01.10
인벤토리 만들기  (0) 2023.01.09
class 연습2 (2중 for문 멈추기 이슈)  (0) 2023.01.05
class연습 SCV  (0) 2023.01.04
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함