using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _230109
{
class App
{
public App()
{
Inventory inventory = new Inventory(5);
inventory.AddItem(new Item("장검"));
inventory.AddItem(new Item("장검"));
inventory.AddItem(new Item("단검"));
Console.WriteLine();
int count = inventory.GetItemCount();
Console.WriteLine("인벤토리의 아이템 갯수 : {0}", count);
Console.WriteLine();
string searchName = "장검";
Item item = inventory.GetItemByName(searchName);
if (item == null)
Console.WriteLine("{0}을 찾을수 없습니다.", searchName);
else
Console.WriteLine("{0}을 장착하였습니다.", searchName);
string searchName1 = "활";
Item item1 = inventory.GetItemByName(searchName1);
if (item1 == null)
Console.WriteLine("{0}을 찾을수 없습니다.", searchName1);
else
Console.WriteLine("{0}을 장착하였습니다.", searchName1);
Console.WriteLine();
count = inventory.GetItemCount();
Console.WriteLine("인벤토리의 아이템 갯수 : {0}", count);
Console.WriteLine();
inventory.PrintAllItems();
Console.WriteLine();
inventory.Arrange();
Console.WriteLine();
inventory.PrintAllItems();
Console.WriteLine();
inventory.returnToInventory();
Console.WriteLine();
inventory.AddItem(new Item("활"));
inventory.PrintAllItems();
Console.WriteLine();
inventory.AddItem(new Item("도끼"));
inventory.AddItem(new Item("망치"));
Console.WriteLine();
inventory.PrintAllItems();
string searchName2 = "활";
Item item2 = inventory.GetItemByName(searchName2);
if (item2 == null)
Console.WriteLine("{0}을 찾을수 없습니다.", searchName2);
else
Console.WriteLine("{0}을 장착하였습니다.", searchName2);
Console.WriteLine();
inventory.PrintAllItems();
Console.WriteLine();
inventory.Arrange();
Console.WriteLine();
inventory.PrintAllItems();
Console.WriteLine();
inventory.returnToInventory();
Console.WriteLine();
inventory.PrintAllItems();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _230109
{
class Inventory
{
Item[] items;
Item[] tempItems;
int capacity;
int index;
public Inventory(int capacity)
{
this.capacity = capacity;
items = new Item[this.capacity];
tempItems = new Item[this.capacity];
}
public void AddItem(Item item)
{
bool contain = false; //이 부분 구현이 안돼서 bool타입 사용하는 것 확인
for (int i = 0; i < items.Length; i++)
{
if(items[i] != null && items[i].name == item.name) //인스턴스 값을 통해 비교연산 할 때는 Null Check 필수
{
contain = true;
}
}
if (index < items.Length)
{
if (contain == false && items[index] == null)
{
items[index] = item;
Console.WriteLine("{0}을 인벤토리에 추가하였습니다.", item.name);
index++;
}
else if(items[index] != null)
{
for(int i = 0; i < items.Length; i++)
{
if(items[i] == null)
{
index = i;
}
else
{
index = items.Length;
}
}
}
else
{
Console.WriteLine("{0}를 이미 소유하고 있습니다.", item.name);
}
}
else
{
Console.WriteLine("인벤토리가 가득 찼습니다.");
}
}
public int GetItemCount()
{
int count = 0;
for(int i = 0; i < items.Length; i++)
{
if(items[i] != null)
{
count++;
}
}
return count;
}
public Item GetItemByName(string name)
{
Item item = null;
for (int i = 0; i < items.Length; i++)
{
if (items[i] != null && items[i].name == name)
{
item = items[i];
tempItems[i] = item;
items[i] = null;
index = i; //Null 값에 아이템 추가할 수 있도록 인덱스 값 재설정
}
}
return item;
}
public void PrintAllItems()
{
Console.WriteLine("인벤토리 목록");
for (int i = 0; i < items.Length; i++)
{
if (items[i] != null)
{
Console.WriteLine(items[i].name);
}
else
{
Console.WriteLine("[ ]");
}
}
}
public void Arrange()
{
Console.WriteLine("아이템을 정렬합니다.");
for (int i = 0; i < items.Length - 1; i++)
{
if (items[i] == null)
{
items[i] = items[i + 1];
items[i + 1] = null;
}
}
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null)
{
index = i; //Null 값에 아이템 추가할 수 있도록 인덱스 값 재설정
break;
}
else if(items[items.Length -1 ] != null)
{
index = items.Length - 1; //인벤토리가 가득 찼을 경우 인덱스 값
}
}
}
public void returnToInventory()
{
for(int i = 0; i < tempItems.Length; i++)
{
if (tempItems[i] != null)
{
for(int j = 0; j < items.Length; j++)
{
if(items[j] == null && index < items.Length)
{
items[index] = tempItems[i];
Console.WriteLine("{0}을 인벤토리에 되돌립니다.", tempItems[i].name);
index++;
tempItems[i] = null;
break;
}
else if(items[items.Length - 1] != null)
{
Console.WriteLine("인벤토리 공간이 부족합니다.");
}
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _230109
{
class Item
{
public string name;
public Item(string name)
{
this.name = name;
}
}
}
'C# > 수업 과제' 카테고리의 다른 글
직렬화/역직렬화 연습 (0) | 2023.01.12 |
---|---|
List<T>를 이용한 인벤토리 구현 (0) | 2023.01.10 |
배열을 활용한 인벤토리 구현 (0) | 2023.01.06 |
클래서,메서드 과제5 (0) | 2023.01.05 |
클래스/메서드 과제4 (0) | 2023.01.05 |