> 바닥 수량보다 많이 입력시, 바닥 최대수량 획득 후 바닥 아이템 목록에서 제거

> 수량 반영

> App 생성자
더보기
List<Item> dropItemList; Inventory inventory; public App() { ListApp3(); }
> ListApp3()
더보기
public void ListApp3() { this.dropItemList = new List<Item>(); this.dropItemList.Add(new Item("장검", 10)); this.dropItemList.Add(new Item("단검", 2)); this.dropItemList.Add(new Item("활", 5)); string dropItemName; int dropItemAmount; this.inventory = new Inventory(); while (true) { PrintDropList(dropItemList); Console.WriteLine(); Console.Write("아이템을 획득하려면 명령어를 입력해주세요: "); var input = Console.ReadLine(); string[] cInput = input.Split(' '); dropItemName = cInput[0]; dropItemAmount = int.Parse(cInput[1]); GetItemList(dropItemName, dropItemAmount); this.inventory.PrintAllItems(); } }
> PrintDropList()
더보기
public void PrintDropList(List<Item> list) { Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■"); Console.WriteLine("길바닥에 떨어져있는 아이템"); foreach (var item in list) { if (item != null) { Console.WriteLine($"{item.name}, {item.amount}"); } } Console.WriteLine("■■■■■■■■■■■■■■■■■■■■■■■■■■■■"); Console.WriteLine(); }
> GetItemList()
더보기
public void GetItemList(string dropItemName, int dropItemAmount) { var findDropitem = this.dropItemList.Find(v => v.name == dropItemName); if (findDropitem == null) { Console.WriteLine(">입력한 아이템은 획득 할 수 없습니다."); Console.WriteLine(); return; } // 바닥에 있는 수량보다 많이 획득 할 경우 if (findDropitem.amount <= dropItemAmount) { Console.WriteLine($"최대 {findDropitem.amount}개 를 획득했습니다."); this.inventory.Add(new Item(dropItemName, findDropitem.amount)); this.dropItemList.Remove(findDropitem); } else { this.inventory.Add(new Item(dropItemName, dropItemAmount)); findDropitem.amount -= dropItemAmount; } }
'c# 강의 > 과제' 카테고리의 다른 글
20200427 - 싱글톤 (0) | 2020.04.28 |
---|---|
20200421 - File 읽기 (JSON) (0) | 2020.04.22 |
20200417 - 주말과제(요리,레시피) (0) | 2020.04.19 |
20200415 - 아이템 생성, 검색, 꺼내기, 출력 과제 (0) | 2020.04.16 |
20200414 - 테란~아카데미까지 (0) | 2020.04.14 |