
> Item.cs
더보기
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _20200414_array { class Item { public string name; public Item(string name) { this.name = name; Console.WriteLine($"아이템 {this.name}이 생성되었습니다."); } } }
> Inventory.cs
더보기
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _20200414_array { class Inventory { public Item[] items; int itemIndex = 0; public Inventory(int capacity) { items = new Item[capacity]; Console.WriteLine("인벤토리가 생성되었습니다."); } public void AddItem(Item item) { if (this.itemIndex < this.items.Length) { this.items[this.itemIndex] = item; this.itemIndex++; } } public Item GetItem(String itemName) { Item item = null; // 꺼낸 아이템 저장 Item[] tempArr = new Item[this.items.Length - 1]; // 꺼낸 아이템 정리용 임시 배열 bool check = false; int delIndex = 0; for (int i = 0; i < this.items.Length; i++) { if (this.items[i] != null) { // 검색대상 같은지 확인 if (itemName == this.items[i].name) { item = this.items[i]; delIndex = i; check = true; } else { // 기존 배열에서 찾은 인덱스 확인하여 그 인덱스에, 다음 내용물 넣기 if (i > delIndex && check) { tempArr[i - 1] = this.items[i]; } else { // 새로운 배열에 넣기 if (tempArr.Length > i) { tempArr[i] = this.items[i]; } } //if end } // else end } // if end } //for end // 내용물 찾아 꺼낸 경우 기존 배열 업데이트 if (check) { this.items = tempArr; Console.WriteLine(item.name); } return item; } public Item FindItem(String itemName) { Item item = null; for (int i = 0; i < this.items.Length; i++) { if (this.items[i] != null) { if (itemName == this.items[i].name) { Console.WriteLine(this.items[i].name); item = this.items[i]; break; } } } return item; } public int GetCount() { int itemCount = 0; foreach (Item item in this.items) { if (item != null) { itemCount++; } } return itemCount; } public void PrintItemNames() { Console.WriteLine("> 전체 목록"); /* // forEach 순환문 foreach (Item item in this.items) { if (item == null) { Console.WriteLine("[ ]"); } else { Console.WriteLine(item.name); } } */ for (int i = 0; i < this.items.Length; i++) { if (this.items[i] == null) { Console.WriteLine("[ ]"); } else { Console.WriteLine($"({i}){this.items[i].name}"); } } } } }
> App.cs
더보기
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _20200414_array { class App { public App() { Inventory inventory = new Inventory(5); inventory.AddItem(new Item("장검")); inventory.AddItem(new Item("궁")); inventory.AddItem(new Item("투구")); inventory.AddItem(new Item("장화")); inventory.AddItem(new Item("벨트")); Console.WriteLine(); // ㅡㅡㅡㅡㅡㅡ 가방내용 전체 출력 ㅡㅡㅡㅡㅡㅡ inventory.PrintItemNames(); Console.WriteLine(); // ㅡㅡㅡㅡㅡㅡ 특정 아이템 검색 ㅡㅡㅡㅡㅡㅡ Console.Write("> 검색: "); Item findedItem = inventory.FindItem("궁"); if (findedItem == null) { Console.WriteLine("찾는 아이템 없음"); } else { Console.WriteLine($"[{findedItem.name}]을 찾았습니다."); } Console.WriteLine(); //ㅡㅡㅡㅡㅡㅡ 찾아 꺼내기 ㅡㅡㅡㅡㅡㅡ Console.Write("> 꺼내기: "); Item getedItem = inventory.GetItem("장화"); if (getedItem == null) { Console.WriteLine("> 꺼낼 아이템 없음"); } else { Console.WriteLine($"[{getedItem.name}]을 꺼냈습니다."); } Console.WriteLine(); inventory.PrintItemNames(); } } }
'c# 강의 > 과제' 카테고리의 다른 글
20200420 - List 과제 (0) | 2020.04.20 |
---|---|
20200417 - 주말과제(요리,레시피) (0) | 2020.04.19 |
20200414 - 테란~아카데미까지 (0) | 2020.04.14 |
20200413 - 슈팅 게임 (0) | 2020.04.13 |
20200413 - 팩토리 시즈모드 (0) | 2020.04.13 |