수업 010 (04.17 2020) Array - Food

2020. 4. 17. 12:08
728x90

1. App

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._011_4
{
    class App
    {
        public App()
        {
            Ingredient[] arrIngredients = new Ingredient[2];
 
            arrIngredients[0= new Ingredient("밀가루 포대"1);
            arrIngredients[1= new Ingredient("부드러운 양념"1);
 
            Food food = new Food("양념빵", arrIngredients);
            food.PrintFood();
        }
    }
}
 
 
 

 

2. Ingredient

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._011_4
{
    class Ingredient
    {
        public string name;
        public int amount;
 
        // 생성자
        public Ingredient(string name, int amount)
        {
            this.name = name;
            this.amount = amount;
            Console.WriteLine("재료 '{0}'이/(가) {1}개 생성되었습니다."this.name, this.amount);
        }
    }
}
 
 
 

 

3. Food

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study._011_4
{
    class Food
    {
        public string name;
        public Ingredient[] ingredients;
        public Food(string name, Ingredient[] ingredients)
        {
            this.name = name;
            this.ingredients = ingredients;
        }
 
        public void PrintFood()
        {
            Console.Write("{0}: "this.name);
            for (int i = 0; i < ingredients.Length; i++)
            {
                Console.Write("{0} x {1} ", ingredients[i].name, ingredients[i].amount);
            }
            Console.WriteLine();
        }
    }
}
 
 
 

 

4. Result

728x90

BELATED ARTICLES

more