22.01.03 인벤토리에서 아이템 꺼내오기, 다시 넣기

2022. 1. 3. 22:54C#/수업 내용

[퀘스트]

1. GC의 Collect()함수

2. delegate class

3. System.Action

4. 람다식

5. 프라퍼티의 존재이유 == private변수 {생성자 set, 메서드 get} 의 간소화 ?

 

이하 수업내용 -------------------------------------------------------------------------------------------------

 

결과

using System;

namespace helloworldConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hero knight = new Hero("김봉식");

            knight.AddItemToInventory(new Item("정밀한 단도", 3));
            knight.AddItemToInventory(new Item("정밀한 단도", 3));
            knight.AddItemToInventory(new Item("그레이트 소드", 3));
            knight.AddItemToInventory(new Item("그레이트 소드", 3));
            knight.AddItemToInventory(new Item("떡갈나무 지팡이", 3));
            knight.PrintInventory();
            knight.EquipWeapon("정밀한 단도");
            knight.PrintInventory();
            knight.UnEquipWeapon();
            knight.PrintInventory();
        }
    }
}

------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace helloworldConsoleApp1
{
    class Hero
    {
        private Inventory_List inventory = new Inventory_List();
        private int hp;
        private int damage;
        private Item weapon;
        private string name;

        public Hero(string name)
        {
            this.name = name;
            weapon = new Item("맨손", 0);
            hp = 50;
            damage = 2;
            this.PrintStatus();
        }

        public void PrintStatus()
        {
            Console.WriteLine(" [스테이터스 창]");
            Console.WriteLine("기사의 이름은 {0}, {1}의 체력과 {2}의 공격력을 가지고 있다.", this.name, this.hp, this.GetDamage());
        }

        public void AddItemToInventory(Item item)
        {
            inventory.AddItem(item);
            Console.WriteLine("   <<System>>");
            Console.WriteLine("인벤토리에 {0} 아이템이 추가되었습니다.", item.GetWeaponName());
        }

        public void EquipWeapon(string name)
        {
            Item item = inventory.FindItemByName(name);
            if(item != null)
            {
                this.weapon = item;
                Console.WriteLine("   <<System>>");
                Console.WriteLine("{0}이 {1}을 장착했습니다. 현재 공격력 : {2}", this.name, this.weapon.GetWeaponName(), this.GetDamage());
            }
        }

        public void UnEquipWeapon()
        {
            if(this.weapon.GetWeaponName() != "맨손")
            {
                inventory.AddItem(this.weapon);
                Console.WriteLine("   <<System>>");
                Console.WriteLine("{0}이 {1}을 장착해제했습니다.", this.name, this.weapon.GetWeaponName());
                this.weapon = new Item("맨손", 0);
            } else
            {
                Console.WriteLine("   <<System>>");
                Console.WriteLine("장착 중인 무기가 없습니다.");
            }
        }

        private int GetDamage()
        {
            return this.weapon.GetWeaponDamage() + this.damage;
        }

        public void PrintInventory()
        {
            Console.WriteLine(" [인벤토리]");
            inventory.PrintAllItems();
        }
    }
}

------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace helloworldConsoleApp1
{
    class Inventory_List
    {
        private List<Item> inventolist = new List<Item>();

        public Inventory_List()
        {
        }

        public void AddItem(Item item)
        {
            bool hasItem = false;
            foreach(Item search in inventolist)
            {
                if(search.GetWeaponName() == item.GetWeaponName())
                {
                    search.IncreaseItemCount();
                    hasItem = true;
                }
            }
            if (!hasItem)
            {
                inventolist.Add(new Item(item.GetWeaponName(), item.GetWeaponDamage()));
            }
        }

        public void PrintAllItems()
        {
            if(this.inventolist.Count > 0)
            {
                foreach (Item item in inventolist)
                {
                    Console.WriteLine("{0} x{1}", item.GetWeaponName(), item.GetWeaponCount());
                }
            } else
            {
                Console.WriteLine("   <<System>>");
                Console.WriteLine("보유한 아이템이 없습니다.");
            }
        }

        public int GetItemsCount()
        {
            return this.inventolist.Count;
        }

        public Item FindItemByName(string name)
        {
            foreach(Item item in inventolist)
            {
                if (item.GetWeaponName() == name)
                {
                    Item foundItem = item;
                    item.DecreaseItemCount();
                    if(item.GetWeaponCount() <= 0)
                    {
                        inventolist.Remove(item);
                    }
                    return foundItem;
                }
            }
            Console.WriteLine("   <<System>>");
            Console.WriteLine("{0} 미보유.", name);
            return null;
        }
    }
}

------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace helloworldConsoleApp1
{
    class Item
    {
        private string itemName;
        private int count;
        private int damage;

        public Item(string name, int damage)
        {
            this.itemName = name;
            this.count = 1;
            this.damage = damage;
        }

        public void IncreaseItemCount()
        {
            this.count++;
        }

        public void DecreaseItemCount()
        {
            this.count--;
        }

        public int GetWeaponCount()
        {
            return this.count;
        }

        public int GetWeaponDamage()
        {
            return this.damage;
        }

        public string GetWeaponName()
        {
            return this.itemName;
        }
    }
}

 

'C# > 수업 내용' 카테고리의 다른 글

22.01.05 대리자 delegate Part.2  (0) 2022.01.05
22.01.04 대리자 delegate  (0) 2022.01.04
22.01.01 캐리어  (2) 2022.01.01
21.12.30 부대 관리 & 드랍쉽  (0) 2021.12.30
21.12.29 아이템 & 인벤토리  (0) 2021.12.30