using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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
Weapon sword = inven.GetItem("장검");
Console.WriteLine("찾은 아이템명: {0}", 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();
////장검 x 1
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study09
{
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)
{
//리스트에 weapon추가
this.weapons.Add(weapon);
Console.WriteLine("{0}이 추가 되었습니다.", weapon.Name);
this.count = this.weapons.Count;
}
public void PrintAllItems()
{
//리스트를 순회하며 모든 요소의 이름을 출력
foreach (Weapon weapon in this.weapons)
{
Console.WriteLine(weapon.Name);
}
}
public Weapon GetItem(string name)
{
//찾은 아이템을 반환
Weapon foundWeapon = null;
foreach (Weapon weapon in this.weapons)
{
if (weapon.Name == name)
{
foundWeapon = weapon;
//리스트에서 제거
this.weapons.Remove(weapon);
this.count = this.weapons.Count;
break;
}
}
return foundWeapon;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study09
{
class Weapon
{
public string Name
{
get; private set;
}
public int Count
{
get; set;
}
//생성자
public Weapon(string name)
{
this.Count = 1;
this.Name = name;
}
}
}