유니티3D 프로그래밍
C# 2주차 2일 수업 과제 (21.03.16) 본문
신년 패키지
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
//Dictionary<int, Product> 변수 선언
Dictionary<string, Product> dicProduct;
//컬렉션 인스턴스화
dicProduct = new Dictionary<string, Product>();
//Product 생성 Id, 이름, 가격
Product product = new Product("tjdgus9955", "신년패키지", 33000);
//딕셔너리에 요소 추가(키, 값: Product객체)
dicProduct.Add("tjdgus9955", product);
//요소의 키로 검색
Product foundProduct = dicProduct["tjdgus9955"];
//foreach문으로 요소 출력
//KeyValuePrice<int, Product>
foreach(KeyValuePair<string , Product> pair in dicProduct)
{
Product element = pair.Value;
Console.WriteLine("{0} {1} {2} : {3}", foundProduct.GetID(), foundProduct.GetName(), foundProduct.GetStrPrice(), element);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
class Product
{
private string id;
private string name;
private int price;
//생성자
public Product(string id, string name, int price)
{
this.id = id;
this.name = name;
this.price = price;
}
public string GetName()
{
return this.name;
}
public string GetID()
{
return this.id;
}
public int GetPrice()
{
return this.price;
}
//3자리수 마다 , 찍고 출력
public string GetStrPrice()
{
return string.Format("{0:#,0}", this.price);
}
}
}
좋아하는 게임
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
List<string> favoriteGame = new List<string>();
favoriteGame.Add("패스 오브 엑자일");
favoriteGame.Add("사이퍼즈");
favoriteGame.Add("팩토리오");
foreach(string game in favoriteGame)
{
Console.WriteLine("좋아하는 게임 : {0}", game);
}
}
}
}
좋아하는 게임 2
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
List<string> favoriteGame = new List<string>();
favoriteGame.Add("패스 오브 엑자일");
favoriteGame.Add("사이퍼즈");
favoriteGame.Add("팩토리오");
foreach (string gameLike in favoriteGame)
{
Console.WriteLine("좋아하는 게임 : {0}", gameLike);
}
Console.WriteLine();
List<Game> gameInfo = new List<Game>();
Game game = new Game(favoriteGame[0].ToString(), Game.eGameType.MMORPG, 0);
Game game1 = new Game(favoriteGame[1].ToString(), Game.eGameType.AOS, 0);
Game game2 = new Game(favoriteGame[2].ToString(), Game.eGameType.SANDBOX, 33000);
gameInfo.Add(game);
gameInfo.Add(game1);
gameInfo.Add(game2);
foreach (Game aboutGame in gameInfo)
{
Console.WriteLine("게임 가격 : {0}", aboutGame.GetStrPrice());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Game
{
public enum eGameType
{
MMORPG,
AOS,
SANDBOX
}
private string name;
private eGameType gameType;
private int price;
//생성자
public Game(string name, eGameType gameType, int price)
{
this.name = name;
this.gameType = gameType;
this.price = price;
}
public string GetName()
{
return this.name;
}
public eGameType GameType()
{
return this.gameType;
}
public int GetPrice()
{
return this.price;
}
//3자리수 마다 ,를 찍거나 가격이 0원이면 부분 무료화 출력
public string GetStrPrice()
{
if(this.price == 0)
{
return "부분 무료화";
}
else
{
return string.Format("{0:#,0}", this.price);
}
}
}
}
좋아하는 게임 3
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
class App
{
//생성자
public App()
{
List<string> favoriteGame = new List<string>();
favoriteGame.Add("패스 오브 엑자일");
favoriteGame.Add("사이퍼즈");
favoriteGame.Add("팩토리오");
foreach (string gameLike in favoriteGame)
{
Console.WriteLine("좋아하는 게임 : {0}", gameLike);
}
Console.WriteLine();
List<Game> gameInfo = new List<Game>();
Game game = new Game(1, favoriteGame[0].ToString(), Game.eGameType.MMORPG, 0);
Game game1 = new Game(2, favoriteGame[1].ToString(), Game.eGameType.AOS, 0);
Game game2 = new Game(3, favoriteGame[2].ToString(), Game.eGameType.SANDBOX, 33000);
gameInfo.Add(game);
gameInfo.Add(game1);
gameInfo.Add(game2);
foreach (Game aboutGame in gameInfo)
{
Console.WriteLine("게임 가격 : {0}", aboutGame.GetStrPrice());
}
Dictionary<int, Game> dicGame = new Dictionary<int, Game>();
dicGame.Add(1, game);
dicGame.Add(2, game1);
dicGame.Add(3, game2);
Console.WriteLine();
Game foundGame = dicGame[1];
Console.WriteLine("Dictionary로 찾는 게임 : {0}", foundGame.GetName());
Console.WriteLine();
Console.WriteLine("모든 게임 출력.");
foreach(KeyValuePair<int, Game> pair in dicGame)
{
Game element = pair.Value;
Console.WriteLine("{0} : {1}, {2}, {3}", pair.Key, element.GetName(), element.GameType(), element.GetStrPrice());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Game
{
public enum eGameType
{
MMORPG,
AOS,
SANDBOX
}
private int key;
private string name;
private eGameType gameType;
private int price;
//생성자
public Game(int key, string name, eGameType gameType, int price)
{
this.key = key;
this.name = name;
this.gameType = gameType;
this.price = price;
}
public string GetName()
{
return this.name;
}
public eGameType GameType()
{
return this.gameType;
}
public int GetPrice()
{
return this.price;
}
//3자리수 마다 ,를 찍거나 가격이 0원이면 부분 무료화 출력
public string GetStrPrice()
{
if(this.price == 0)
{
return "부분 무료화";
}
else
{
return string.Format("{0:#,0}", this.price);
}
}
public int GetKey()
{
return key;
}
}
}
'C# > 수업과제' 카테고리의 다른 글
C# 2주차 5일 수업 내용 및 과제 : 2차원 배열 및 2048 프로그램(21.03.19) (0) | 2021.03.19 |
---|---|
C# 2주차 3일 수업 과제 : Stack (21.03.17) (0) | 2021.03.17 |
C# 2주차 1일 수업 과제 (21.03.15) (0) | 2021.03.15 |
C# 1주차 5일 연습 과제 (21.03.12) (0) | 2021.03.12 |
C# 1주차 4일 연습 과제 (21.03.11) (0) | 2021.03.11 |