티스토리 뷰

C#/수업 과제

인벤토리 만들기

Game Client Lee Hwanguk 2023. 1. 25. 17:31
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework
{
    internal class Inventory
    {
        public int capacity;
        public Item[]items;
        public int index=0;
        
        
        public Inventory(int capacity) 
        {
            this.capacity = capacity;
            this.items= new Item[capacity];
            Console.WriteLine("capacity:{0} 인벤토리 생성",items.Length);
        }

        public void AddItem(Item item) //동일한 name은 추가할수 없음
        {
            bool isItem = false;    
            foreach(Item item2 in items)
            { 
                if(item2 != null)
                {
                    if(item2.Name == item.Name)
                    {
                        Console.WriteLine("이미 {0} 가지고 있습니다.",item2.Name);
                        isItem = true;
                        break;
                    }
                    
                }
            }

            if (isItem == false) //==if(!isItem)
            {
                this.items[index] = item; //추가하고
                this.index++; //index +1
                Console.WriteLine("{0}을(를) 추가", item.Name);
            }        
            //예외처리-만약 capacity 초과로 들어온다면 ? 들어오지않게 막는기능을 넣어야곘다
        }

        public void RemoveItem(string name) 
        {
            for(int i=0; i<items.Length; i++)
            {
                if (items[i].Name == name)
                {
                    items[i] = null;
                    Console.WriteLine("{0}을 삭제",name);
                    break; //break가 없다면 없는 index까지 탐색하니 오류가 난다 
                }
            }
            
        }

        public void CountItem()
        {
            int cnt = 0;
            for(int i=0; i<items.Length;i++)
            {
                if (items[i] != null)
                {
                    cnt++;
                }
                else continue;
            }
            Console.WriteLine("inventory:{0}",cnt);
        }

        public void AddCapacity(int add) //최대수량(capacity)을 가지고있으며 늘릴수도있다
        {
            //int capacity0=Convert.ToInt32(items.Length);
            //Console.WriteLine(capacity0 + add);
            int capacity0 = 0;
            for(int i=0; i< items.Length; i++)
            {
                if (items[i] != null)
                {
                    capacity0++;
                }
            }
            Console.WriteLine("add_capacity:{0} ,capacity:{1}",add,capacity0+add);
        }

        public void SearchItem(string name)
        {
            bool isItem=false;
            foreach(Item item1 in items)
            {
                if (item1 != null) 
                {
                    if (item1.Name == name)
                    {
                        isItem = true;
                        Console.WriteLine("{0}(이)가 있습니다", name);
                        break;
                    }
                    else continue;                  
                }              
            }
            if (!isItem) Console.WriteLine("아이템이 없습니다.");
            Console.WriteLine();
        }

        public void ItemType(string type)
        {
            for(int i=0; i<items.Length ; i++) 
            {
                if (items[i] != null)
                {
                    if (items[i].GetType().ToString().Contains(type)) //items[i]의 GetType을 받고 string으로 변환후 Contains로 탐색
                    {
                        
                        Console.WriteLine("Type:{0}, Name:{1}",type,items[i].Name);
                    }
                }
            }
            Console.WriteLine();
        }

        public void PrintAllItem() 
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] != null)
                {
                    Console.WriteLine(items[i].Name);
                }

                else if (items[i] == null)
                {
                    Console.WriteLine("[    ]");
                }

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

namespace Homework
{
    internal class App
    {
        public App() 
        {
            Inventory inventory=new Inventory(5);
            inventory.AddItem(new Weapon("장검",12));
            inventory.AddItem(new Weapon("도끼", 14));
            inventory.AddItem(new Weapon("창", 13));
            inventory.AddItem(new Weapon("장검", 12)); //이미 가지고 있다면 추가X
            inventory.AddItem(new Armor("투구", 3));
            inventory.AddItem(new Armor("갑옷", 5));
            inventory.CountItem();
            //inventory.AddItem(new Weapon("너클", 3)); //capacity 를 초과하면 넣을수 없다
            Console.WriteLine();
            inventory.PrintAllItem();
            inventory.CountItem();
            inventory.RemoveItem("장검");
            inventory.CountItem();

            inventory.AddCapacity(3);
            inventory.SearchItem("창");
            inventory.ItemType("Weapon");
            inventory.ItemType("Armor");
        }
    }
}

공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함