using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자
public App()
{
Inventory inven = new Inventory(5);
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("단검"));
inven.AddItem(new Item("활"));
inven.PrintAllItems();
string searchItemName = "장검";
inven.GetItemByName(searchItemName);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class Item
{
public string Name { get; set; }
//생성자
public Item(string name)
{
this.Name = name;
Console.WriteLine("{0}이 생성되었습니다.", this.Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Inventory
{
Item[] items;
int count;
public Inventory(int capacity)
{
//생성자
this.items = new Item[capacity];
}
public void AddItem(Item item)
{
this.items[count] = item;
count++;
}
public void PrintAllItems()
{
foreach (Item item in this.items)
{
if (item != null)
Console.WriteLine("{0}", item.Name);
}
}
public Item GetItemByName(string searchItemName)
{
for (int i = 0; i < this.items.Length; i++)
{
if (items != null)
{
if (items[i].Name == searchItemName)
{
Console.WriteLine("찾았다 {0}", items[i].Name);
items[i] = null;
break;
}
}
}
return null;
}
}
}