using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Syntax51
{
class Inventory
{
public Item[] arrItems;
public int index=0;
public Inventory()
{
this.arrItems = new Item[5];
}
public void AddItem(Item item)
{
for (int i = 0; i < arrItems.Length; i++)
{
if (arrItems[i] == null)
{
this.arrItems[i] = item;
Console.WriteLine(item.name+"이 생성되었습니다.");
return;
}
}
Console.WriteLine("공간이부족합니다.");
}
public void RemoveItemByName(string name)
{
for (int i = 0; i < arrItems.Length; i++)
{
if (arrItems[i].name == name)
{
arrItems[i] = null;
break;
}
else
{
Console.WriteLine("해당상품이 없습니다.");
}
}
}
public void DisplayInventoryItemNames()
{
for (int i = 0; i < arrItems.Length; i++)
{
if (arrItems[i] != null)
{
Console.Write(arrItems[i].name+" ");
}
}
Console.WriteLine();
}
public int FindIndexofItemByName(string name)
{
for (int i = 0; i < arrItems.Length; i++)
{
if (arrItems[i].name != null&& arrItems[i].name == name)
{
Console.WriteLine(arrItems[i].id);
return arrItems[i].id;
}
}
Console.WriteLine("해당 아이템을 찾을수 없습니다.");
return 0;
}
public Item FindItemByName(string name)
{
for (int i = 0; i < arrItems.Length; i++)
{
if (arrItems[i] != null && name == arrItems[i].name)
{
Console.WriteLine($"{arrItems[i].name}을 찾았습니다.");
return arrItems[i];
}
Console.WriteLine("아이템을 찾을 수 없습니다.");
return null;
}
return null;
}
}
}
|