using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study08
{
class App
{
//생성자
public App()
{
Inventory inventory = new Inventory(5);
inventory.AddItem(new Item("장검"));
inventory.AddItem(new Item("장검")); //이미 장검이 있습니다.
inventory.AddItem(new Item("단검"));
int count = inventory.GetItemCount();
Console.WriteLine("count: {0}", count); //2
string searchName = "장검";
Item item = inventory.GetItemByName(searchName);
if (item == null)
Console.WriteLine("{0}을 찾을수 없습니다.", searchName);
else
Console.WriteLine("{0}을 찾았습니다.", searchName);
//만약에 있다면 1 없다면 2
count = inventory.GetItemCount();
Console.WriteLine("count: {0}", count); //1
inventory.PrintAllItems();
inventory.Arrange(); //정렬
inventory.PrintAllItems();
inventory.AddItem(new Item("창"));
inventory.PrintAllItems();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study08
{
class Item
{
public string name;
//생성자
public Item(string name)
{
this.name = name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study08
{
class Inventory
{
private int capacity; //최대 용량
private Item[] items; //아이템들을 관리 배열
private int idx = 0;
//생성자
public Inventory(int capacity)
{
this.capacity = capacity;
//배열 초기화
this.items = new Item[this.capacity];
}
public void AddItem(Item item)
{
Console.WriteLine();
bool contains = false; //포함하는 게 거짓이면
for (int i = 0; i < this.items.Length; i++)
{
if (this.items[i] != null && this.items[i].name == item.name)//아이템 값이 null이 아니고 값을 가지고 있다면
{
contains = true; //포함한다
break;
}
}
if (contains)
{
Console.WriteLine("{0}은 이미 있습니다.", item.name);
}
else
{
Console.WriteLine("{0}이 추가 되었습니다.", item.name);
this.items[this.idx] = item;
idx++;
}
Console.WriteLine();
}
public int GetItemCount()
{
//배열이 가지고있는 아이템의 갯수
int cnt = 0;
for (int i = 0; i < this.items.Length; i++)
{
if (this.items[i] != null) cnt++;
}
return cnt;
}
public Item GetItemByName(string searchName)
{
//배열을 순회하며 아이템이 있을경우
//아이템의 이름과 searchName이 같으면
//해당 아이템을 반환
//배열의 요소를 비워줘야함
//빈공간이 있다면 정렬 해야함
Item foundItem = null;
for (int i = 0; i < this.items.Length; i++)
{
if (this.items[i] != null && this.items[i].name == searchName) //아이템 값이 null이 아니고 값이 있으면
{
foundItem = this.items[i]; //값을 할당
this.items[i] = null;
Console.WriteLine("i: {0}, idx: {1}", i, this.idx);
break;
}
}
return foundItem;
}
public void PrintAllItems()
{
//배열에 있는 아이템들을 출력
//빈공간은 다음과 같이 표현해주세요 : [ ]
for (int i = 0; i < this.items.Length; i++)
{
string str = "";
if (this.items[i] == null)
str = "[ ]";
else
str = this.items[i].name;
Console.WriteLine(str);
}
}
public void Arrange()
{
Console.WriteLine();
Console.WriteLine("정렬중....");
//앞에 빈공간이 있다면 채웁니다
Item[] arr = new Item[this.capacity];
this.idx = 0;
for (int i = 0; i < this.items.Length; i++)
{
if (this.items[i] != null)
{
arr[this.idx++] = this.items[i];
}
}
this.items = arr;
Console.WriteLine();
}
}
}