-
03/16 컬렉션을 이용 예제C#/수업과제 2021. 3. 16. 18:25
1. Dictionary를 사용하여 던파 세라샵의 아이템 요소를 추가
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { class Program { static void Main(string[] args) { Console.WriteLine("Main"); new App(); } } }
App.cs
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public class App { public App() { //Dictionary<int, Product> 변수 선언 Dictionary<int, Product> dicProducts; //컬렉션 인스턴스화 dicProducts = new Dictionary<int, Product>(); //Product 생성 ID, 이름, 가격 Product product1 = new Product("증폭 보호권", 12900, 1, eCurrencyType.Sera); Product product2 = new Product("장비 보호권", 9800, 1, eCurrencyType.Sera); Product product3 = new Product("칼레이도 박스", 19900, 50, eCurrencyType.Sera); Product product4 = new Product("칼레이도 박스", 12700, 30, eCurrencyType.Sera); Product product5 = new Product("칼레이도 박스", 4500, 10, eCurrencyType.Sera); Product product6 = new Product("칼레이도 박스", 500, 1, eCurrencyType.Sera); Product product7 = new Product("칼레이도 박스", 500, 1, eCurrencyType.GoldCoin); Product product8 = new Product("이계 기운의 소멸서", 1900, 1, eCurrencyType.Sera); //딕셔너리에 요소 추가 (키, 값: Product) dicProducts.Add(1, product1); dicProducts.Add(2, product2); dicProducts.Add(3, product3); dicProducts.Add(4, product4); dicProducts.Add(5, product5); dicProducts.Add(6, product6); dicProducts.Add(7, product7); dicProducts.Add(8, product8); //요소의 키로 검색 Product searchProduct = dicProducts[4]; Console.WriteLine("{0} {1} {2} {3} \n", searchProduct.GetItemName(), searchProduct.GetItemPrice(), searchProduct.GetCurrencyType(), searchProduct.GetCount()); //foreach문으로 요소 출력 //KeyValuePaire<int, Product> foreach (KeyValuePair<int, Product> keyValuePair in dicProducts) { Console.WriteLine("{0}. {1} {2} {3} {4}", keyValuePair.Key, keyValuePair.Value.GetItemName(), keyValuePair.Value.GetItemPrice(), keyValuePair.Value.GetCurrencyType(), keyValuePair.Value.GetCount() > 1 ? String.Format("{0}개 셋트", keyValuePair.Value.GetCount()) : String.Format("{0}개", keyValuePair.Value.GetCount())); } } } }
Product.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public enum eCurrencyType { Sera, GoldCoin } public class Product { private string itemName; private int itemPrice; private int count; private eCurrencyType currencyType; public Product(string itemName, int itemPrice, int count, eCurrencyType currencyType) { this.itemName = itemName; this.itemPrice = itemPrice; this.count = count; this.currencyType = currencyType; } public string GetItemName() { return itemName; } public string GetItemPrice() { return String.Format("{0:#,0}", itemPrice); } public int GetCount() { return count; } public eCurrencyType GetCurrencyType() { return currencyType; } } }
2. ArrayList를 사용하여 타입이 다른 객체 삽입 및 출력
던파 파티 맴버 출력
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { class Program { static void Main(string[] args) { Console.WriteLine("Main"); new App(); } } }
App.cs
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public class App { public App() { //변수 선언 ArrayList list; //인스턴스 생성 list = new ArrayList(); //요소 생성 Gunner gunner = new Gunner("눈설탕맛쿠키"); GhostKnight ghostKnight = new GhostKnight("truer"); Knight knight = new Knight("수갑철컹!?"); Priest priest = new Priest("홀리데잇"); //ArrayList에 요소 추가 list.Add(gunner); list.Add(ghostKnight); list.Add(knight); list.Add(priest); //요소 출력 Console.WriteLine("******파티맴버*******"); foreach(object obj in list) { if (typeof(Gunner) == obj.GetType()) { Gunner character = (Gunner)obj; Console.WriteLine("{0}", character.GetName()); continue; } if (typeof(GhostKnight) == obj.GetType()) { GhostKnight character = (GhostKnight)obj; Console.WriteLine("{0}", character.GetName()); continue; } if (typeof(Knight) == obj.GetType()) { Knight character = (Knight)obj; Console.WriteLine("{0}", character.GetName()); continue; } if (typeof(Priest) == obj.GetType()) { Priest character = (Priest)obj; Console.WriteLine("{0}", character.GetName()); continue; } } Console.WriteLine("*********************"); } } }
Gunner.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public class Gunner { private string name; public Gunner(string name) { this.name = name; } public string GetName() { return this.name; } } }
GhostKnight.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public class GhostKnight { private string name; public GhostKnight(string name) { this.name = name; } public string GetName() { return this.name; } } }
Knight.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public class Knight { private string name; public Knight(string name) { this.name = name; } public string GetName() { return this.name; } } }
Priest.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public class Priest { private string name; public Priest(string name) { this.name = name; } public string GetName() { return this.name; } } }
3. List<T>를 사용하여 사이퍼즈 킬 정보 표시
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { class Program { static void Main(string[] args) { Console.WriteLine("Main"); new App(); } } }
App.cs
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public class App { public App() { //변수 선언 List<User> list; //인스턴스 생성 list = new List<User>(); //요소 생성 User user1 = new User("truer", ePosition.Dealer, 66, 17, 4, 6); User user2 = new User("제닢울비메이져", ePosition.Supporter, 54, 3, 0, 26); User user3 = new User("니인생초우자이", ePosition.Dealer, 50, 8, 5, 9); User user4 = new User("앎", ePosition.Tanker, 48, 3, 0, 23); User user5 = new User("WEEHIN", ePosition.Tanker, 35, 1, 0, 13); //ArrayList에 요소 추가 list.Add(user1); list.Add(user2); list.Add(user3); list.Add(user4); list.Add(user5); //요소 출력 Console.WriteLine("********************************************"); Console.WriteLine("포지션 레벨 아이디 킬 파괴 도움"); foreach (User user in list) { Console.WriteLine("{0} {1} {2} {3} {4} {5}", user.GetPosition(), user.GetLevel(), user.GetName(), user.GetKill(), user.GetDestruction(), user.GetSupport()); } Console.WriteLine("********************************************"); } } }
User.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Study04 { public enum ePosition { Dealer, Supporter, Tanker } public class User { private string name; private ePosition position; private int level; private int kill; private int destruction; private int support; public User(string name, ePosition position, int level, int kill, int destruction, int support) { this.name = name; this.position = position; this.level = level; this.kill = kill; this.destruction = destruction; this.support = support; } public string GetName() { return this.name; } public ePosition GetPosition() { return this.position; } public int GetLevel() { return this.level; } public int GetKill() { return this.kill; } public int GetDestruction() { return this.destruction; } public int GetSupport() { return this.support; } } }
'C# > 수업과제' 카테고리의 다른 글
03/19 2차원 배열 2048 구현 (0) 2021.03.22 03/17 추상클래스, 인터페이스, 속성, 스택, 큐를 사용한 예제 (0) 2021.03.18 03/12 1주차 주말과제 (0) 2021.03.15 03/11 클래스 생성 -> 인스턴스 생성 연습 (0) 2021.03.11 03/10 매개변수가 있고 반환 값이 있는 메서드 정의 및 호출 (0) 2021.03.11