'class 활용'에 해당되는 글 3건

  1. 2019.10.07 10.07 Class 활용해 BurgerSet 만들기
  2. 2019.10.01 10.01 커맨드센터에서 SCV1,2를 만들고 공격하기
  3. 2019.10.01 10.01 여러 class를 이용해 햄버거 세트 만들기

10.07 Class 활용해 BurgerSet 만들기

C#/실습 2019. 10. 7. 16:59

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class App
    {
        public App()
        {
            Burger burger = new Burger("슈슈버거");            
            Side side = new Side("감자칩");            
            Drink drink = new Drink();
            drink.SetDrinkName("콜라");
            Set set = new Set();
            set.SetSet(burger, side, drink);
            var GetSetBurger = set.GetBurger();
            var GetSetSide = set.GetSide();
            var GetSetDrink = set.GetDrink();
            Console.WriteLine($"세트메뉴의 구성품은 {GetSetBurger.GetName()},{GetSetSide.GetSideName()},{GetSetDrink.GetDrinkName()}입니다.");
 
        }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
class Burger
    {
        string name;
        public Burger(string name)
        {
            this.name = name;
        }
        public string GetName()
        {            
            return this.name;
        }
    }
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Drink
    {
        string name;
        public Drink()
        {
 
        }
        public void SetDrinkName(string name)
        {
            this.name = name;
        }
        public string GetDrinkName()
        {
            return this.name;
        }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
class Side
    {
        string name;
        public Side(string name)
        {
            this.name = name;
        }
        public string GetSideName()
        {            
            return this.name;
        }
    }
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Set
    {        
        public Burger burger;
        public Side side;
        public Drink drink;
        public Set()
        {
            
        }
        public void SetSet(Burger burger, Side side, Drink drink)
        {
            this.burger = burger;
            this.side = side;
            this.drink = drink;
        }
        public Burger GetBurger()
        {
            return this.burger;
        }
        public Side GetSide()
        {
            return this.side;
        }
        public Drink GetDrink()
        {
            return this.drink;
        }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

:

10.01 커맨드센터에서 SCV1,2를 만들고 공격하기

C#/실습 2019. 10. 1. 16:49

예제:

코드:

Program.cs:
namespace _10._01_step2
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();//App타입 객체를 생성하고 App()이라는 메소드를 호출한다
        }
    }
}

 

App.cs:

namespace _10._01_step2
{
    class App
    {
        public App()
        {
            CommandCenter commandCenter1 = new CommandCenter();
            Console.WriteLine(commandCenter1 + "가 생성되었습니다.");
            Unit unit1 = commandCenter1.MakeUnit("SCV1");
            Unit unit2 = commandCenter1.MakeUnit("SCV2");
            unit1.Attack(unit2);
        }
    }
}

CommandCenter.cs:

namespace _10._01_step2
{
    class CommandCenter
    {
        int hp;

        public CommandCenter()
        {

        }
        public Unit MakeUnit(string name)
        {
            Unit unit = new Unit();
            unit.name = name;
            Console.WriteLine(unit.name + "이 생성되었습니다.");
            return unit;
        }
    }
}

Unit.cs:

using System;

namespace _10._01_step2
{
    class Unit
    {
        //데이터
        public string name;
        int hp =100;
        int maxHp = 100;
        int damage=5;
        public Unit()
        {

        }
        //메서드
        //이동한다
        public void Move()
        {

        }
        public void Attack(Unit unit)
        {
            Console.WriteLine($"{this.name}이{unit.name}에게 {damage}만큼의 피해를 입혔습니다.");
            unit.GetHit(this.damage);
        }
        public void GetHit(int damage)
        {
            this.hp = this.hp - damage;
            Console.WriteLine($"{this.name}이 {damage}만큼의 피해를 입었습니다.");
            Console.WriteLine(this.hp + "/" + this.maxHp);
        }
    }
}

'C# > 실습' 카테고리의 다른 글

10.02 버거세트  (0) 2019.10.02
10.02 커피상점  (0) 2019.10.02
10.01 여러 class를 이용해 햄버거 세트 만들기  (0) 2019.10.01
워크래프트 캐릭생성하기  (0) 2019.09.27
switch 문  (0) 2019.09.26
:

10.01 여러 class를 이용해 햄버거 세트 만들기

C#/실습 2019. 10. 1. 16:45

세트로 만들지 여부 확인
Set 클라스의 인스턴스를 이용해 메뉴를 한 클라스에 묶는다.

Program.cs:

namespace _10._01_step4
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

App.cs:

using System;


namespace _10._01_step4
{
    class App
    {
        public App()
        {
            BurgerShop burgerShop1 = new BurgerShop();
burgerShop1.name = "McDonalds";
            Console.WriteLine("버거를 입력해주세요: ");
            Burger burger1 = burgerShop1.MakeBurger(Console.ReadLine());
            Console.WriteLine("사이드메뉴를 입력해주세요");
            PotatoChip potatoChip1 = burgerShop1.MakePotatoChip(Console.ReadLine());
            Console.WriteLine("음료를 입력해주세요");
            Drink drink1 = burgerShop1.MakeDrink(Console.ReadLine());
            Console.WriteLine(burger1.name + "이(가) 생성되었습니다.");
            Console.WriteLine(potatoChip1.name + "이 생성되었습니다.");
            Console.WriteLine(drink1.name + "이 생성되었습니다.");
            Console.WriteLine("세트로 만드시겠습니까?(1.예, 2.아니오)");
            string input = Console.ReadLine();
            if (input == "1")
            {
                Set set1 = burgerShop1.MakeSet(burger1, potatoChip1, drink1);
                Console.WriteLine("세트(" + set1.burger.name + "," + set1.drink.name + "," + set1.potatochip.name + ")이 생성되었습니다.");
            }
            else if (input=="2")
            {

            }

        }
    }
}

BurgerShop.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10._01_step4
{
    class BurgerShop
    {
        public string name;
        public BurgerShop()
        {

        }
        public Burger MakeBurger(string name)
        {
            Burger burger = new Burger();
            burger.name = name;
            return burger;
        }
        public PotatoChip MakePotatoChip(string name)
        {
            PotatoChip potatoChip = new PotatoChip();
            potatoChip.name = name;
            return potatoChip;
        }
        public Drink MakeDrink(string name)
        {
            Drink drink = new Drink();
            drink.name = name;
            return drink;
        }
        public Set MakeSet(Burger burger, PotatoChip potatochip, Drink drink)
        {
            Set newSet = new Set();
            newSet.burger = burger;
            newSet.potatochip = potatochip;
            newSet.drink = drink;
            return newSet;
        }
    }
}

Burger.cs:

namespace _10._01_step4
{
    class Burger
    {
        public string name;
        public Burger()
        {
            
        }
    }
}

Drink.cs:

namespace _10._01_step4
{
    class Drink
    {
        public string name;
        public Drink()
        {

        }
    }
}

PotatoChip.cs:

namespace _10._01_step4
{
    class PotatoChip
    {
        public string name;
        public PotatoChip()
        {

        }
    }
}

Set.cs:

namespace _10._01_step4
{
    class Set
    {
        public Burger burger;
        public PotatoChip potatochip;
        public Drink drink;
        public Set()
        {

        }
    }
}

'C# > 실습' 카테고리의 다른 글

10.02 커피상점  (0) 2019.10.02
10.01 커맨드센터에서 SCV1,2를 만들고 공격하기  (0) 2019.10.01
워크래프트 캐릭생성하기  (0) 2019.09.27
switch 문  (0) 2019.09.26
09.24 최대공약수 구하기  (0) 2019.09.24
: