using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hw
{
public class App
{
Inventory inven;
//생성자
public App()
{
inven = new Inventory(5);
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("단검"));
inven.AddItem(new Item("활"));
inven.ShowAllItems();
//1. 장검
//2. 단검
//3. 활
Item item = inven.FindItem("장검");
Console.WriteLine(item.Name);
int cnt = inven.Count();
Console.WriteLine(cnt); //3
inven.RemoveItemByName("장검");
cnt = inven.Count();
Console.WriteLine(cnt); //2
inven.ShowAllItems();
//1. 단검
//2. 활
inven.Extend(5);
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.ShowAllItems();
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;
namespace hw
{
internal class Inventory
{
List<Item> itemlist;
int limit;
public Inventory(int n)
{
//생성자정의
itemlist = new List<Item>();
limit = n;
Console.WriteLine($"인벤토리 {n}칸 생성 성공");
}
//인벤토리에 아이템을 추가하는 메서드
public void AddItem(Item item)
{
if (itemlist.Count < limit)
{
itemlist.Add(item);
}
else
{
Console.WriteLine("공간이 부족합니다.");
}
}
//인벤토리에 모든 아이템을 출력하는 메서드
public void ShowAllItems()
{
Item item;
for (int i = 0; i < itemlist.Count; i++)
{
item = itemlist[i];
Console.WriteLine($"{i+1}. {item.Name}");
}
}
//인벤토리내에 특정 문자열과 일치하는 아이템을 찾는 메서드
public Item FindItem(string name)
{
foreach(Item i in itemlist)
{
if (i.Name == name)
{
return i;
}
}
/*
for (int i = 0; i < itemlist.Count; i++)
{
item = itemlist[i];
if (item.Name == name)
{
return item;
}
}*/
Console.WriteLine("해당 아이템이 없습니다.");
return null;
}
//인벤토리에 있는 장비의 갯수를 세는 메서드
public int Count()
{
int cnt = 0;
for (int i = 0; i < itemlist.Count; i++)
{
if (itemlist[i] != null)
{
cnt++;
}
}
return cnt;
}
public void RemoveItemByName(string name)
{
Item item;
item = FindItem(name);
if (item != null)
{
itemlist.Remove(item);
Console.WriteLine($"{name}을(를) 제거했습니다.");
}
}
public void Extend(int n)
{
limit += n;
Console.WriteLine($"인벤토리가 {n}칸 확장 되었습니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hw
{
internal class Item
{
public string Name;
public Item(string name)
{
//생성자 정의
this.Name = name;
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hw
{
internal class Program
{
static void Main(string[] args)
{
App app = new App();
}
}
}