App.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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_011_2
{
public class App
{
public App()
{
Character hong = new Character("홍길동");
string[] arrFoodIngredients = new string[5];
arrFoodIngredients[0] = "밀가루 포대";
arrFoodIngredients[1] = "부드러운 양념";
int[] arrNeedCount = new int[5];
arrNeedCount[0] = 1;
arrNeedCount[1] = 1;
FoodIngredient[] breadNeeds = { new FoodIngredient("밀가루 포대"),
new FoodIngredient("부드러운 양념") };
FoodIngredient[] cupCakeNeeds = {new FoodIngredient("밀가루 포대", 2),
new FoodIngredient("북지 알")};
FoodIngredient[] blackJellyNeeds = { new FoodIngredient("북풍 해파리", 3) };
Recipe breadRecipe = new Recipe("양념빵", breadNeeds);
Recipe cupcakeRecipe = new Recipe("맛있는 컵케이크", cupCakeNeeds);
Recipe blackJellyRecipe = new Recipe("검은 젤리", blackJellyNeeds);
hong.GatherRecipe(breadRecipe, 1);
hong.GatherRecipe(cupcakeRecipe, 2);
hong.GatherRecipe(blackJellyRecipe, 4);
hong.PrintHavingRecipes();
hong.LiftHavingRecipeArray();
hong.PrintHavingRecipes();
hong.UseRecipe("검은 젤리");
hong.UseRecipe("양념빵");
hong.PrintHavingRecipes();
hong.PrintUsedRecipes();
hong.GatherIngredient("북풍 해파리", 10);
hong.PrintHavingIngredients();
hong.GatherIngredient("밀가루 포대", 5);
hong.PrintHavingIngredients();
hong.GatherIngredient("북지 알", 2);
hong.PrintHavingIngredients();
hong.GatherIngredient("부드러운 양념", 1);
hong.FindIngredient("밀가루 포대");
hong.Cook("검은 젤리");
hong.Cook("양념빵");
}
}
}
|
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_011_2
{
public class Character
{
//변수.
public string Name { get { return this.name; } }
string name;
public FoodIngredient[] HavingIngredients { get { return this.havingIngredients; } }
FoodIngredient[] havingIngredients;
public Recipe[] UsedRecipes { get { return this.usedRecipes; } }
Recipe[] usedRecipes;
public Recipe[] HavingRecipes { get { return this.havingRecipes; } }
Recipe[] havingRecipes;
//캐릭터 생성자
public Character(string name)
{
this.name = name;
this.havingIngredients = new FoodIngredient[10];
this.usedRecipes = new Recipe[5];
this.havingRecipes = new Recipe[5];
}
//레시피 획득.
public void GatherRecipe(Recipe recipe)
{
{
if(havingRecipes[i] == null)
{
Console.WriteLine("{0} 자리에 {1} 을 획득했습니다.", i, recipe.GetName());
havingRecipes[i] = recipe;
return;
}
}
Console.WriteLine("조리법을 획득할 수 없습니다.");
}
//레시피를 해당 인덱스에 획득. 빈칸 당기기를 확인하기 위해 만든 함수.
public void GatherRecipe(Recipe recipe, int index)
{
this.HavingRecipes[index] = recipe;
}
//레시피 사용.
public void UseRecipe(string recipeName)
{
int nullIndex = 0;
{
if (usedRecipes[nullIndex] == null)
break;
}
{
Console.WriteLine("더이상 레시피를 사용할 수 없습니다. ");
return;
}
else
{
Recipe temp = null;
{
if(havingRecipes[i] != null)
{
if(havingRecipes[i].Name == recipeName)
{
temp = havingRecipes[i];
havingRecipes[i] = null;
break;
}
}
}
if(temp != null)
{
this.usedRecipes[nullIndex] = temp;
Console.WriteLine("{0} 레시피를 사용했습니다.", temp.GetName());
}
else
{
Console.WriteLine("해당 레시피는 없는 레시피 입니다.");
}
}
}
//사용한 레시피 출력 함수.
public void PrintUsedRecipes()
{
Console.WriteLine("사용한 레시피 목록 ======");
{
if(usedRecipes[i] == null)
{
Console.WriteLine("{0} : [ ]", i);
}
else
{
Console.WriteLine("{0} : [{1}]", i, usedRecipes[i].GetName());
}
}
Console.WriteLine();
}
public void PrintHavingRecipes()
{
Console.WriteLine("소지중인 레시피 목록 ======");
{
if (havingRecipes[i] == null)
{
Console.WriteLine("{0} : [ ]", i);
}
else
{
Console.WriteLine("{0} : [{1}]", i, havingRecipes[i].GetName());
}
}
Console.WriteLine();
}
//가지고있는 재료들 출력 함수.
//만든 아이템까지 이곳에서 출력 가능.
public void PrintHavingIngredients()
{
Console.WriteLine("소지중인 레시피 목록 ======");
{
if (havingIngredients[i] == null)
{
Console.WriteLine("{0} : [ ]", i);
}
else
{
Console.WriteLine("{0} : [{1} / {2}]", i, havingIngredients[i].Name, havingIngredients[i].Count);
}
}
Console.WriteLine();
}
//재료획득, 음식 획득까지 이곳에서 함.
public void GatherIngredient(string name, int count)
{
//같은 재료가 있는지 확인.
int targetIndex = 0;
{
if(this.havingIngredients[targetIndex] != null)
{
if(this.havingIngredients[targetIndex].Name == name)
{
this.havingIngredients[targetIndex].AddCount(count);
Console.WriteLine("{0} 아이템에 {1} 개를 추가했습니다. ", name, count);
return;
}
}
}
//빈칸 확인
int nullIndex = 0;
{
if (havingIngredients[nullIndex] == null)
break;
}
{
Console.WriteLine("더이상 재료를 얻을 수 없습니다. ");
return;
}
this.havingIngredients[nullIndex] = new FoodIngredient(name, count);
Console.WriteLine("{0}번째 칸에 {1} 재료를 {2}개 얻었습니다. ", nullIndex, name, count);
}
//가지고 있는 재료를 위로 올리는 함수. 자동으로 되게 하고 싶다면 UseRecipe()메서드에 넣으면 됨.
public void LiftIngredientsArray()
{
{
if (havingIngredients[i] == null)
{
{
if(havingIngredients[j] != null)
{
havingIngredients[i] = havingIngredients[j];
havingIngredients[j] = null;
break;
}
}
}
}
}
//가지고 있는 레시피 위로 올리는 함수.
public void LiftHavingRecipeArray()
{
{
if (havingRecipes[i] == null)
{
{
if (havingRecipes[j] != null)
{
havingRecipes[i] = havingRecipes[j];
havingRecipes[j] = null;
break;
}
}
}
}
}
//사용된 레시피 위로 올리는 함수.
public void LiftUsedRecipeArray()
{
{
if (UsedRecipes[i] == null)
{
{
if (UsedRecipes[j] != null)
{
UsedRecipes[i] = UsedRecipes[j];
UsedRecipes[j] = null;
break;
}
}
}
}
}
//재료 찾는 함수.
public FoodIngredient FindIngredient(string foodName)
{
{
if(this.havingIngredients[i] != null)
{
if(this.havingIngredients[i].Name == foodName)
{
return havingIngredients[i];
}
}
}
Console.WriteLine("해당 재료가 없습니다.");
return null;
}
//레시피 찾는 함수.
public Recipe FindRecipe(string name)
{
{
if (usedRecipes[i] != null && usedRecipes[i].Name == name)
return usedRecipes[i];
}
return null;
}
//요리하는 함수.
//레시피가 없을 때, 재료가 없을 때 예외처리
public void Cook(string name)
{
Recipe target = this.FindRecipe(name);
if (target == null)
{
Console.WriteLine("해당 레시피는 가지고 있지 않습니다.\n");
return;
}
int index = 0;
//모든 요구 량에 대해서.
foreach (FoodIngredient need in target.foodIngredients)
{
//자신의 모든 재료들을 돌며.
{
//둘다 널이 아닐때.
if(need != null && this.havingIngredients[i] != null)
{
//둘의 이름이 같을 때.
{
arr[index] = this.havingIngredients[i];
break;
}
}
}
//필요한 모든 재료와 현재 재료 출력.
index++;
}
//모든 재료를 돌며 갯수가 맞는지 확인.
{
if(arr[i].Count < target.foodIngredients[i].Count)
{
Console.WriteLine("{0}의 재료가 부족합니다.\n", arr[i].Name);
return;
}
}
//모든 재료를 돌며 갯수를 빼준다. 갯수 확인은 끝났다.
{
arr[i].Count -= target.foodIngredients[i].Count;
}
this.PrintHavingIngredients();
}
}
}
|
FoodIngredient.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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_011_2
{
public class FoodIngredient
{
public static string[] AllIngredients = {
"밀가루 포대", "북지 알", "부드러운 양념"};
public string Name { get { return this.name; } }
string name;
public int Count { get { return this.count; } set { this.count = value; } }
int count;
public FoodIngredient(string name)
{
this.name = name;
this.count = 1;
}
public FoodIngredient(string name, int count)
{
this.name = name;
this.count = count;
}
public void PrintInfo()
{
Console.WriteLine("재료 : {0} / 갯수 : {1}", this.name, this.count);
}
public void AddCount(int count)
{
this.count += count;
}
}
}
|
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
26
27
28
29
30
31
32
33
34
35
36
37
38
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_011_2
{
public class Recipe
{
public string Name { get { return this.name; } }
string name;
public FoodIngredient[] foodIngredients { get { return this.needFoodIngredients; } }
FoodIngredient[] needFoodIngredients;
public Recipe(string name, FoodIngredient[] ingredients)
{
this.name = name;
this.needFoodIngredients = ingredients;
}
public string GetName()
{
return "조리법 : " + this.name;
}
public void PrintRecipe()
{
Console.WriteLine(this.GetName());
foreach(FoodIngredient ingredient in this.needFoodIngredients)
{
if (ingredient != null)
}
}
}
}
|
실행 결과 입니다.
Cook된 레시피는 Character의 FoodIngredients 배열에 넣었습니다. 갯수를 포함합니다.
2048 콘솔 게임. (0) | 2020.05.01 |
---|---|
과제 - 롤 챔프, 스킨 구매 시뮬레이션..? (0) | 2020.04.30 |
홍길동과 임꺽정의 전투 클래스 사용. (0) | 2020.04.10 |
과제 04-03 (0) | 2020.04.03 |
과제 04-02 (0) | 2020.04.02 |