문제> add에서 Item타입의 리스틀를 불러서 name 변수를 쓰는데 왜 new item이 또 필요하지?
Main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace listi_nven
{
internal class Program
{
static void Main(string[] args)
{
// 리스트로 동적 배열을 생성해 장검, 단검, 활을 넣음
// 장검을 불러서 삭제함
Inven inven = new Inven();
// 리스트에 아이템을 추가함
Console.WriteLine("==========Add 아이템 실행==========");
inven.Add(new Item("장검"));
inven.Add(new Item("단검"));
inven.Add(new Item("활"));
Console.WriteLine();
//아이템 리스트 출력
Console.WriteLine("=======ShowList(foreach) 실행=======");
inven.ShowList();
Console.WriteLine();
//리스트에 있는 아이템 갯수 확인
Console.WriteLine("=============Count 실행=============");
inven.Count();
Console.WriteLine();
//아이템 찾기
Console.WriteLine("========FindItem(for) 실행==========");
inven.FindItem("단검");
Console.WriteLine();
//아이템 삭제
Console.WriteLine("==============Remove 실행============");
inven.Remove("장검");
Console.WriteLine();
//아이템 삭제가 잘됐는지 확인
Console.WriteLine("===아이템 삭제 확인 ShowList 실행===");
inven.ShowList();
Console.WriteLine();
//아이템 삭제가 잘됐는지 리스트에 있는 아이템 갯수 확인
Console.WriteLine("=====아이템 삭제 확인 Count실행=====");
inven.Count();
Console.WriteLine();
//리스트 클리어
Console.WriteLine("=============Clear 실행=============");
inven.Clear();
Console.WriteLine();
}
}
}
Item
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace listi_nven
{
internal class Item
{
//맴버 변수
public string name;
//생성자
public Item(string name)
{
Console.WriteLine("Item 생성자 실행");
this.name = name;
Console.WriteLine($" this.name: {this.name}");
Console.WriteLine();
}
}
}
Inven
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace listi_nven
{
internal class Inven
{
public List<Item> inven;
//생성자
public Inven()
{
Console.WriteLine("App 생성자 실행");
inven = new List<Item>();
}
public void Add(Item item)
{
inven.Add(item);
}
public void ShowList()
{
int cnt = 0;
foreach (var item in inven)
{
cnt++;
Console.WriteLine($"{cnt}.{item.name}");
}
}
public void Count()
{
int cnt = 0;
foreach (var item in inven)
{
cnt++;
}
Console.WriteLine($"인벤토리 아이템: {cnt}개");
}
public void FindItem(string name)
{
int cnt = 0;
for (int i = 0; i < inven.Count; i++)
{
cnt++;
if (name == inven[i].name)
{
Console.WriteLine($"찾는 아이템 명: {cnt}.{inven[i].name}");
}
}
}
public void Remove(string name)
{
int cnt = 0;
Console.WriteLine($"삭제할 아이템 명: {name}");
for (int i = 0;i < inven.Count;i++)
{
cnt++;
if (name == inven[i].name)
{
inven.RemoveAt(i);
}
}
Console.WriteLine($"{name}을 삭제하였습니다.");
}
public void Clear()
{
inven.Clear();
Count();
}
}
}