C#/수업 과제

List<T>를 이용한 인벤토리 구현

HSH12345 2023. 1. 10. 18:02
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _230110
{
    class Weapon : Item
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public int Count
        {
            get; set;
        }
        public Weapon(string name)
        {
            this.name = name;
            this.Count = 1;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Study09
{
    class App
    {
        public App()
        {
            Inventory inven = new Inventory(5);
            inven.AddItem(new Weapon("장검"));
            inven.AddItem(new Weapon("장검"));
            inven.AddItem(new Weapon("단검"));

            inven.PrintAllItems();
            //장검 x 2
            //단검 x 1

            Item sword = inven.GetItem("장검");
            Console.WriteLine(sword.Name); //장검

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

            inven.PrintAllItems();
            //장검 x 1
            //단검 x 1
            Item dagger = inven.GetItem("단검");
            Console.WriteLine(dagger.Name); //장검

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

            inven.PrintAllItems();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _230110
{
    class Inventory
    {
        private int capacity;
        private List<Weapon> weapons;
        private int count;
        public int Count
        {
            get
            {
                return count;
            }
            private set
            {
                count = value;
            }
        }

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

        public void AddItem(Weapon weapon) //장검의 수량을 2로 늘림
        {
            Weapon foundItem = null;
            foreach (Weapon element in this.weapons)
            {
                if (element.Name == weapon.Name)
                {
                    foundItem = element;
                    break;
                }
            }
            if (foundItem != null)
            {
                foundItem.Count++;          //Weapon 클래스에 Count를 정의해줘야 함       
            }
            else
            {
                this.weapons.Add(weapon);
            }

            this.count++;
            Console.WriteLine("{0}이 추가 되었습니다.", weapon.Name);

        }

        public void PrintAllItems()
        {
            foreach (Weapon weapon in this.weapons)
            {
                Console.WriteLine("{0} x {1}", weapon.Name, weapon.Count);
            }
        }

        public Item GetItem(string weapon) //장검이 한번에 두개가 사라지는 것을 해결해야 함
        {
            Item tempItem = null;

            for (int i = 0; i < weapons.Count; i++)
            {
                if (weapons[i].Name == weapon)
                {
                    tempItem = weapons[i];
                    weapons.Remove(weapons[i]);
                    count = weapons.Count;
                    break;
                }
            }
            return tempItem;
        }

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

namespace Study09
{
    class Item
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public Item()
        {

        }
    }
}