수업 012 (04.21 2020) List<> HerbBag
2020. 4. 22. 00:13
728x90
1. App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._013_3
{
class App
{
//생성자
public App()
{
//약초가방 생성
HerbsBag bag = new HerbsBag();
Item item1 = bag.GetItem("물망초");
Item item2 = bag.GetItem(1000);
bool haveItem1 = bag.HaveItemByName("물망초");
Console.WriteLine(haveItem1);
bool haveItem2 = bag.HaveItemByName("1000");
Console.WriteLine(haveItem2);
Console.WriteLine();
List<Item> dropItemList = new List<Item>();
// id guid
Console.WriteLine("바닥에 있는 아이템 목록");
Console.WriteLine("=======================");
bag.AddItem(dropItemList);
Console.WriteLine("=======================");
List<Item> itemList1 = bag.GetAllItems();
// 확인하기
foreach (Item item in itemList1)
{
}
Console.WriteLine();
List<Item> itemList2 = bag.GetItemsExceptByName("물망초 \n");
// 확인하기
foreach (Item item in itemList2)
{
}
Console.WriteLine();
int count = bag.GetItemCountById(1000);
}
}
}
|
2. HerBag.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._013_3
{
class HerbsBag
{
public int sellPrice; //판매 가격
public List<Item> itemList; //아이템들
public int maxItemCount = 12; //최대 갯수
//생성자
public HerbsBag()
{
this.sellPrice = 2500;
this.itemList = new List<Item>();
}
//아이템 넣기
public void AddItem(Item item)
{
//아이템 타입이 약초인가?
{
//리스트에 추가
}
else
{
//약초만 넣을수 있습니다 출력
Console.WriteLine("약초만 넣을수 있습니다");
}
}
public void AddItem(List<Item> dropItemList)
{
{
{
Console.WriteLine("{0}이 추가 되었습니다.", dropItemList[i].name);
}
}
}
//아이템 빼기
public Item GetItem(string name)
{
Item foundItem = null;
foreach (Item item in this.itemList)
{
{
foundItem = item;
break;
}
}
if (foundItem != null)
{
}
return foundItem;
}
public Item GetItem(int id)
{
Item foundItem = null;
foreach (Item item in this.itemList)
{
{
foundItem = item;
break;
}
}
// Item foundItem = this.itemList.Find(x => x.id == id); 변경가능
if (foundItem != null)
{
}
return foundItem;
}
public List<Item> GetAllItems()
{
return this.itemList;
}
public List<Item> GetItemsExceptByName(string name)
{
List<Item> newList = new List<Item>();
foreach (Item item in itemList)
{
{
}
}
return newList;
}
public int GetItemCountById(int id)
{
int itemCount = 0;
foreach (Item item in itemList)
{
{
itemCount++;
}
}
return itemList.Count;
}
public void PrintAllItems(string name)
{
{
Console.WriteLine("가방에 아무것도 없습니다.");
}
else
{
foreach (var item in this.itemList)
{
}
}
}
public bool HaveItemByName(string name)
{
bool isExist = false;
foreach (Item item in itemList)
{
{
isExist = true;
break;
}
}
return isExist;
}
public bool HaveItemByName(int id)
{
bool isExist = false;
foreach (Item item in this.itemList)
{
{
isExist = true;
break;
}
}
return isExist;
}
}
}
|
3. Item.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._013_3
{
class Item
{
public enum eItemType
{
Herbs, Weapon
}
public string name; //이름
public eItemType itemType; //아이템 타입
public int id;
public string guid;
//생성자
public Item(int id, string guid, string name, eItemType itemType)
{
this.id = id;
this.name = name;
this.itemType = itemType;
}
}
}
|
4. Result
728x90
'C# > Study' 카테고리의 다른 글
수업 013 (04.22 2020) dictionary, json // Achievement (0) | 2020.04.22 |
---|---|
수업 013 (04.22 2020) 복습 - dictionary<Tkey, TValue> (0) | 2020.04.22 |
수업 012 (04.21 2020) List<> add, remove, enum (0) | 2020.04.21 |
수업 012 (04.21 2020) List<> 복습 (0) | 2020.04.21 |
수업 011 (04.20 2020) List<> (0) | 2020.04.21 |