본문 바로가기
C#/수업과제

[ C# 8일차] List<T>를 이용한 Inventory 과제

by 왹져박사 2023. 1. 11.
728x90

App Class

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

namespace Day8
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            Inventory inven = new Inventory(5);
            inven.AddItem(new Weapon("장검"));
            inven.AddItem(new Weapon("장검"));
            inven.AddItem(new Weapon("단검"));

            inven.PrintAllItems();
            //장검x2
            //단검x1

            Weapon sword = inven.GetItem("장검");
            Console.WriteLine();
            inven.PrintAllItems();

            Console.WriteLine(sword.Name);  //장검
            Console.WriteLine();

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

            inven.PrintAllItems();
            //장검x1
            //단검x1

            Weapon dagger = inven.GetItem("단검");
            Console.WriteLine(dagger.Name);  //단검

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

            inven.PrintAllItems();
            //장검x1
        }
    }
}

 

Inventory Class

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

namespace Day8
{
    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;
            weapons = new List<Weapon>();
        }

        public void AddItem(Weapon weapon)
        {
            Weapon foundWeapon = null;
            foreach (Weapon item in this.weapons)
            {
                if (item.Name == weapon.Name)
                {
                    foundWeapon = item;
                    break;
                }
            }

            if (foundWeapon != null)
            {
                foundWeapon.Count++;
                this.count++;
            }
            else
            {
                weapons.Add(weapon);
                this.count = this.weapons.Count;
            }
            Console.WriteLine("{0}아이템이 추가되었습니다. ", weapon.Name);
            Console.WriteLine();
        }
        public Weapon GetItem(string name)
        {
            Weapon foundWeapon = null;
            for(int i = 0; i < weapons.Count; i++)
            {
                if (weapons[i].Name == name)
                {
                    foundWeapon = weapons[i];
                    if (foundWeapon.Count > 1)      //아이템을 Remove하면 같은name의 모든 아이템이 사라져, 2개 이상이라면 count만을 감소
                    {
                        foundWeapon.Count--;
                        Console.WriteLine();
                    }
                    else
                    {
                        weapons.Remove(foundWeapon);
                        Console.WriteLine();

                    }
                    this.count = weapons.Count;
                    break;
                }

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

    }
}

 

Weapon Class

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

namespace Day8
{
    class Weapon
    {
        public string Name
        {
            get;
            private set;
        }
        public int Count
        {
            get;
            set;
        }

        //생성자
        public Weapon(string name)
        {
            this.Count = 1;
            this.Name = name;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Day8
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

 

실행 결과

GetItem에서 단순히 Remove를 사용하면 같은 name을 가진 weapon이 다 없어지는 부분이 고민되었다. 

이를 weapon의 count가 2이상이라면 Remove가 아닌 count만을 1줄이는 방법을 사용하였다. 

728x90