(1)Item
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class Item
{
public ItemData data;
//생성자
public Item(ItemData data)
{
this.data = data;
}
public string GetName()
{
//반환값
return this.data.name;
}
}
}
(2)ItemData
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class ItemData
{
//아이템의 정보
public int id;
public string name;
public int damage;
//생성자
public ItemData(int id, string name, int damage)
{
this.id = id;
this.name = name;
this.damage = damage;
}
}
}
(3)Inventory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class Inventory
{
public List<Item> items;
int capacity;
public Inventory(int capacity)
{
this.capacity = capacity;
this.items = new List<Item>();
Console.WriteLine("{0}칸짜리 가방이 생성되었습니다.", this.capacity);
}
public void AddItem(Item item)
{
if (this.items.Count == this.capacity)
{
Console.WriteLine("용량이 가득찼습니다. {0}을 넣을 수 없습니다.", item.data.name);
}
else
{
this.items.Add(item);
Console.WriteLine("{0}이 추가되었습니다. [{1}/{2}]", item.data.name, this.items.Count, this.capacity);
}
}
public Item GetItem(int id)
{
for(int i = 0; i < this.items.Count; i++)
{
if (this.items[i].data.id == id)
{
Item temp = this.items[i];
this.items.Remove(temp);
return temp;
}
}
return null;
}
public void PrintAllItems()
{
foreach(Item item in this.items)
{
Console.WriteLine(item.data.name);
}
}
}
}
(4)App
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class App
{
public App()
{
//초기화
Dictionary<int, ItemData> dicItemDatas = new Dictionary<int, ItemData>();
dicItemDatas.Add(100, new ItemData(100, "장검", 8));
dicItemDatas.Add(101, new ItemData(101, "단검", 5));
dicItemDatas.Add(102, new ItemData(102, "활", 6));
dicItemDatas.Add(103, new ItemData(103, "도끼", 10));
Inventory inven = new Inventory(3);
inven.AddItem(new Item(dicItemDatas[100]));
inven.AddItem(new Item(dicItemDatas[101]));
inven.AddItem(new Item(dicItemDatas[102]));
Item item = inven.GetItem(100);
Console.WriteLine(item.data.name);
Console.WriteLine(inven.items.Count);
inven.PrintAllItems();
inven.AddItem(new Item(dicItemDatas[103]));
inven.AddItem(new Item(dicItemDatas[100]));
}
}
}
결과
'K-digital traning > C#프로그래밍' 카테고리의 다른 글
대리자 연습 - 데이터매니저 로드 (0) | 2023.07.27 |
---|---|
대리자 연습 - Hero 이동 (0) | 2023.07.27 |
2차원 배열 연습 (0) | 2023.07.25 |
1차원 배열 인벤토리 복습 (+ 인벤토리 용량확장 (0) | 2023.07.25 |
배열 연습 (0) | 2023.07.25 |