레시피 관리 및 요리하기

2020. 4. 20. 09:36C#/과제

<Recipe.cs>

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_011
{
    class Recipe
    {
        public string name; // 레시피 이름
        public string[] arrFoodIngredientNames; // 레시피 재료 목록
        public Recipe(string name, string[] arrFoodIngredientNames)
        {
            this.name = name;
            this.arrFoodIngredientNames = arrFoodIngredientNames;
            //Console.WriteLine($"조리법 : {this.name}이 생성되었습니다.");
        }  
        public string GetName()
        {           
            return "조리법: " + this.name; ;
        }
    }
}
 

 

 

<Ingredient.cs>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_011
{
    class Ingredient
    {
        public string name;
        public int amount;
        public Ingredient(string name)
        {
            this.name = name;
        }
        public Ingredient(string name, int amount)      // Food
        {
            this.name = name;
            this.amount = amount;
        }
    }
}
 

 

<Food.cs>

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
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_011
{
    class Food
    {
        public string name;
        public Ingredient[] ingredients;
        public Food(string name, Ingredient[] ingredient)
        {
            this.name = name;
            this.ingredients = ingredient;
            Console.WriteLine($"{this.name}이 생성되었습니다.");
 
        }
        public void PrintFoodInformation()
        {
            Console.Write($"{this.name} : ");
 
            string data = "";
            for(int i = 0; i<ingredients.Length;i++)
            {
                data += $"{ingredients[i].name}*{ingredients[i].amount}, ";
            }
            data = data.Substring(0data.Length - 2);
 
            Console.WriteLine(data);
 
        }
    }
}
 

<Character.cs>

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_011
{
    class Character
    {
        string name;
        string[] arrFoodIngredients;        
        Recipe[] arrRecipe;            // 소지중인 레시피
        Recipe[] arrCookRecipe;            // 조리 가능한 레시피
        public int recipeIndex;     // 레시피 보유 갯수
        public int cookRecipeIndex;  // 조리 가능한 레시피 보유 갯수 
        public int ingredientIndex;
 
        public bool recipeItem = true;
        public Character(string name)
        {
            this.name = name;
            this.arrRecipe = new Recipe[10];
            this.arrCookRecipe = new Recipe[10];
            this.arrFoodIngredients = new string[10];
            Console.WriteLine($"{this.name}이 생성되었습니다.");
 
        }
        public void GetRecipe(Recipe recipe)
        {
            this.arrRecipe[recipeIndex] = recipe;
            Console.WriteLine($"{this.name} 이(가) [{this.arrRecipe[recipeIndex].GetName()}] 을(를) 획득했습니다. ");
            recipeIndex++;
        }
        public void UseRecipe(string recipeName)
        {
            Console.WriteLine();
            for (int i = 0; i < arrRecipe.Length; i++)
            {
                if (arrRecipe[i] != null)
                {
                    if (arrRecipe[i].name == recipeName)
                    {
                        Console.WriteLine();
                        Console.WriteLine($"\"{recipeName}\" 레시피를 찾았습니다");
                        Console.WriteLine($"Index : {i}");
                        arrCookRecipe[cookRecipeIndex] = new Recipe(arrRecipe[i].name, arrRecipe[i].arrFoodIngredientNames);
                        cookRecipeIndex++;
                        arrRecipe[i] = null;
 
                        for (int j = 0; j < arrRecipe.Length; j++)
                        {
                            if (arrRecipe[j] == null && j != arrRecipe.Length - 1)
                            {
                                arrRecipe[j] = arrRecipe[j + 1];
                                arrRecipe[j + 1= null;
                            }
                        }
                        Console.WriteLine("***소지중인 레시피***");
                        for (int j = 0; j < arrRecipe.Length; j++)
                        {
                            if (arrRecipe[j] != null)
                                Console.WriteLine($"{j} : {arrRecipe[j].name}");
                            else
                                Console.WriteLine($"{j} : NULL");
                        }
                        Console.WriteLine();
 
                        Console.WriteLine("***습득한 레시피***");
                        for (int j = 0; j < arrRecipe.Length; j++)
                        {
                            if (arrCookRecipe[j] != null)
                                Console.WriteLine($"{j} : {arrCookRecipe[j].name}");
                            else
                                Console.WriteLine($"{j} : NULL");
                        }
                        recipeIndex--;
                        return;
                    }
                }
            }
            Console.WriteLine($"\"{recipeName}\" 이라는 레시피는 보유하고 있지 않습니다. ");
        }
        public void PrintRecipeInfo(string recipeName)
        {
            for (int i = 0; i < arrCookRecipe.Length; i++)
            {
                if (arrCookRecipe[i] != null)
                {
                    if (arrCookRecipe[i].name == recipeName)
                    {
                        Console.WriteLine($"\"{arrCookRecipe[i].name}\" 의 정보를 출력합니다.");
                        Console.WriteLine($"<재료>");
                        for (int j = 0; j < arrCookRecipe[i].arrFoodIngredientNames.Length; j++)
                        {
                            Console.WriteLine(arrCookRecipe[i].arrFoodIngredientNames[j]);
                        }
                    }
                }
            }
        }
        public void Cook(string recipeName)
        {
            int ingredientsMatchCount = 0;
 
            for (int i = 0; i < arrCookRecipe.Length; i++)
            {
                if (arrCookRecipe[i] != null)       // 갖고 있는 레시피 검색 중 null이 아니고
                {
                    if (arrCookRecipe[i].name == recipeName)        // 입력받은 레시피를 가지고 있다면
                    {
                        for (int j = 0; j < arrCookRecipe[i].arrFoodIngredientNames.Length; j++)
                        {
                            for(int k = 0; k<arrFoodIngredients.Length;k++)
                            {
                                if (arrCookRecipe[i].arrFoodIngredientNames[j] == arrFoodIngredients[k])
                                {
                                    ingredientsMatchCount++;
                                    if (ingredientsMatchCount == arrFoodIngredients.Length)   // 요리 성공
                                    {
                                        Console.WriteLine($"\"{arrCookRecipe[i].name}\" 을 만들었습니다. "); 
                                    }
                                }
                            }                            
                        }
                    }
                }
            }         
        }
        public void GetItem(string itemName)
        {
            this.arrFoodIngredients[ingredientIndex] = itemName;
            ingredientIndex++;
        }
    }
}
 

'C# > 과제' 카테고리의 다른 글

캐릭터 전직 종합편  (0) 2020.04.27
객체지향이란  (0) 2020.04.22
인벤토리  (0) 2020.04.14
캐릭터간 대전  (0) 2020.04.10
2020-04-03  (0) 2020.04.03