티스토리 뷰

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

namespace Study10
{
    internal class App
    {
        
        //List<T>를 사용하여 인벤토리를 만들어보자 
        public App()
        {
            Inventory inven = new Inventory(5);
            inven.AddItem(new Weapon("장검"));
            inven.AddItem(new Weapon("장검"));
            Weapon dagger =  new Weapon("단검"); //weapon 클래스의 Name으로 받은 "단검"을 Weapon 타입의 dagger에 담아서
            inven.AddItem(dagger); //메서드의 매개변수로 넣고
            //bool isRight = (inven.weapons[2] == dagger);
            //Console.WriteLine(isRight); //bool 식으로 if문 을 걸고 한번더 확인
            

            inven.printAllItems(); //추가된 아이템들을 출력 console.writeLine을 통해
            //장검 * 2
            //단검 * 1

            Weapon sword  = inven.GetItem("장검"); //GetItem 메서드를 통해 값을 반환받자 //리스트를 for문으로 돌려야한다
            Console.WriteLine(sword.Name); //장검 

            Console.WriteLine(inven.Count); //2

            inven.printAllItems();
            ////장검 x 1
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Study10
{
    internal class Inventory
    {
        int capacity;
        //리스트를 사용하자
       public  List<Weapon> weapons; //리스트의 용량 5 를 생각해야한다
        private int count;
        public int Count
        {
            get
            {

                count = weapons.Count;
                return count;

            }
            set 
            {

            }
        }
        
        public Inventory(int capacity)
        {
            this.capacity = capacity;
            this.weapons = new List<Weapon>();

        }

        public Weapon AddItem(Weapon name)
        {
            weapons.Add(name); //리스트로 만든 weapons에 ADD기능으로 name을 할당   //장검,장검,단검 
            Console.WriteLine(name.Name); //출력해서 확인
            return name; //받은 내용을 다시 반환, 
        }

        public void printAllItems()
        {
            this.count = 0;
            foreach(Weapon weapon in weapons)
            {
                if(weapon!=null)
                {
                    Console.Write("{0}을 획득 ", weapon.Name); //획득한 무기의 수량까지 알려주고싶다 //획득했다면 +1

                    Console.WriteLine("현재 수량 {0}개"); 
                }
                
            }

        }

        public Weapon GetItem(string itemName) //"장검" 을 받았다
        {
            Weapon foundWeapon = null; //null값을 foundweapon 변수에 저장 //왜 Weapon 타입이지? //
            foreach (Weapon weapon in weapons) //리스트 weapons의 값들을 Weapon weapon에 넣어서 본다
            {
                if (weapon.Name == itemName) //만약 받아온 "장검"이 Additem 메서드에서 담긴 weapon.Name값과 같다면
                {
                    foundWeapon = weapon; //foundWeapon에 값을 담고
                    this.weapons.Remove(weapon); //리스트 weapons에 있는 weapon값을 지운다 
                    break; //그리고 foreach문 종료
                }
            }
            return foundWeapon; //foreach의 반복이 끝나면 foundWeapon의 값은 사라진다 따라서 밖에서 정의하고 밖에서 반환받아야한다.


            }

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

namespace Study10
{
    internal class Weapon
    {

               //반환받을 값의 타입 string
        public string Name //property 형식 
        {
            get; //값을 반환하기위해
                 set;//클래스 안에서 값을 바꿀때
        }

        public Weapon(string name)
        {
            this.Name = name;
        }
    }
}

#foreach 외부에 빈 변수를넣고 값을 저장하지않으면 foreach문이 돌면서 값이 복사만된다

 

#foreach문의 사용법을 알았다 이걸로 값을 넣고 저장하고 빼는과정을알았다

 

#획득한 아이템의 수량을 출력하고싶은데 아직 해결을 못했다

#printAllItems 에서 foreach문을 사용하여 count 값을 올려보니 "장검 2" 까진 아직 어떻게하는지 모르겠다 

#배열인덱스 연습이 좀더 필요할것 같다...

 

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