1. App class
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study_011
{
class App
{
public App()
{
Console.WriteLine("2020-04-18\n");
//캐릭터(홍길동) 생성
Character hong = new Character("홍길동");
Console.WriteLine("\n=================================");
//재료 문자열 선언, 초기화
string[] arrIngredients = new string[3];
//재료 문자열 값 할당
arrIngredients[0] = "거대한 알";
arrIngredients[1] = "독특한 양념";
//레시피 생성
Recipe recipe = new Recipe("괴물 오믈렛",arrIngredients);
//홍길동이 레시피를 획득한다.
hong.GetRecipe(recipe);
Console.WriteLine("\n=================================");
//음식재료(객체)를 획득한다.
hong.GetFoodIngredient(new FoodIngredient(arrIngredients[0], 1));
hong.GetFoodIngredient(new FoodIngredient(arrIngredients[1], 1));
Console.WriteLine("\n=================================");
arrIngredients = new string[3];
arrIngredients[0] = "토끼엉겅퀴풀";
arrIngredients[1] = "맑은 샘물";
//레시피 생성
recipe = new Recipe("엉겅퀴 차", arrIngredients);
//홍길동이 레시피를 획득한다.
hong.GetRecipe(recipe);
Console.WriteLine("\n=================================");
hong.GetFoodIngredient(new FoodIngredient(arrIngredients[0], 1));
hong.GetFoodIngredient(new FoodIngredient(arrIngredients[1], 1));
Console.WriteLine("\n=================================");
arrIngredients = new string[3];
arrIngredients[0] = "랩터 고기";
arrIngredients[1] = "매운 양념";
//레시피 생성
recipe = new Recipe("랩터 숯불구이", arrIngredients);
//홍길동이 레시피를 획득한다.
hong.GetRecipe(recipe);
Console.WriteLine("\n=================================");
hong.GetFoodIngredient(new FoodIngredient(arrIngredients[0], 1));
hong.GetFoodIngredient(new FoodIngredient(arrIngredients[1], 1));
Console.WriteLine("\n=================================");
//정렬 테스트용 레시피 생성1
recipe = new Recipe("랩터 숯불구이2", arrIngredients);
//홍길동이 레시피를 획득한다.
hong.GetRecipe(recipe);
//정렬용 레시피 생성
recipe = new Recipe("랩터 숯불구이3", arrIngredients);
//홍길동이 레시피를 획득한다.
hong.GetRecipe(recipe);
Console.WriteLine("\n=================================");
//소지중인 미사용 레시피를 출력한다.
hong.PrintUnuseRecipe();
Console.WriteLine("\n=================================");
//홍길동이 선택한 레시피를 사용한다.
hong.UseRecipe("괴물 오믈렛");
//hong.UseRecipe("엉겅퀴 차");
hong.UseRecipe("랩터 숯불구이");
Console.WriteLine("\n=================================");
//소지중인 미사용 레시피를 출력한다.
hong.PrintUnuseRecipe();
//조리가능 레시피를 출력한다.
hong.PrintAvailableCookFoodName();
//만들 수 있는 음식 정보를 출력한다(조리법 이름과 재료를 확인한다)
hong.PrintAvailableCookFoodInfo();
//내가 갖고 있는 음식 재료들을 출력한다.
hong.PrintMyIngredient();
//선택한 요리를 한다
Food omlet=hong.Cook("괴물 오믈렛");
Food tea=hong.Cook("엉겅퀴 차");
Food Raptor=hong.Cook("랩터 숯불구이");
//내가 갖고 있는 음식 재료들을 출력한다.
hong.PrintMyIngredient();
}
}
}
|
2. Character class
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
351
352
353
354
355
356
357
358
359
360
361
362
363
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study_011
{
class Character
{
public string name; //캐릭터 이름
public Recipe[] arrRecipeName; //소지 중인 미사용 조리법(음식) 목록
int arrRecipeNameIdx; //인덱스(획득 메소드 호출시 사용)
public Recipe[] arrUseRecipe; //사용한(배운) 레시피
int arrUseRecipeIdx; //인덱스(정렬 메소드 사용)
public FoodIngredient[] arrFoodIngredients; //레시피에 나오는 소지 중인 음식 재료들
int arrIngredientsIdx;
//생성자
public Character(string name)
{
this.name = name;
this.arrRecipeName = new Recipe[10];
this.arrFoodIngredients = new FoodIngredient[10];
this.arrUseRecipe = new Recipe[10];
Console.WriteLine("\n{0}이 생성되었습니다.", this.name);
}
//레시피를 획득한다. 소지중인 미사용 조리법에 추가한다.
public void GetRecipe(Recipe recipe)
{
Console.WriteLine("\n조리법 {0}를 획득합니다.", recipe.name);
this.arrRecipeName[arrRecipeNameIdx++] = recipe;
}
//소지중인 미사용 조리법을 출력한다.
public void PrintUnuseRecipe()
{
Console.WriteLine("\n***소지중인 미사용 레시피***");
for (int i = 0; i < this.arrRecipeName.Length; i++)
{
Recipe unUseRecipe = this.arrRecipeName[i];
if (unUseRecipe != null)
{
Console.WriteLine("{0}. {1}", i, unUseRecipe.name);
}
else
{
Console.WriteLine("{0}. [ ]", i);
}
}
}
//미사용 조리법을 사용한다 -> 사용 조리법에 추가되고 미사용 조리법에서 사라진다
public void UseRecipe(string name)
{
Recipe foundRecipe = null;
Console.WriteLine("\n조리법 {0}을(를) 사용합니다.",name);
for(int i=0; i<arrRecipeName.Length; i++)
{
if(arrRecipeName[i]!=null)
{
if(arrRecipeName[i].name==name)
{
foundRecipe = this.arrRecipeName[i];
this.arrRecipeName[i] = null;
this.arrUseRecipe[arrUseRecipeIdx++] = foundRecipe;
this.Sort();
break;
}
}
}
}
//소지 중인 레시피 배열 정렬하기
public void Sort()
{
for(int i=0; i<this.arrRecipeName.Length; i++)
{
if(this.arrRecipeName[i]!=null)
{
for(; ;)
{
if(i==0 || this.arrRecipeName[i-1]!=null)
{
break;
}
if(this.arrRecipeName[i-1]==null)
{
this.arrRecipeName[i - 1] = this.arrRecipeName[i];
this.arrRecipeName[i] = null;
}
}
}
}
}
//만들 수 있는 음식목록을 출력한다(사용한 조리법 출력)
public void PrintAvailableCookFoodName()
{
Console.WriteLine("\n***조리가능 레시피***");
for (int i = 0; i < this.arrUseRecipe.Length; i++)
{
Recipe UseRecipe = this.arrUseRecipe[i];
if (UseRecipe != null)
{
Console.WriteLine("{0}. {1}", i, UseRecipe.name);
}
else
{
Console.WriteLine("{0}. [ ]", i);
}
}
Console.WriteLine();
}
//음식 재료를 획득한다
public void GetFoodIngredient(FoodIngredient foodIngredient)
{
Console.WriteLine("\n{0}을 획득하였습니다.", foodIngredient.name);
this.arrFoodIngredients[arrIngredientsIdx] = foodIngredient; //레시피에 나올 음식 재료 배열에 추가
this.arrFoodIngredients[arrIngredientsIdx].myAmount = foodIngredient.amount;
this.arrIngredientsIdx++;
}
//만들 수 있는 음식 정보를 출력한다.
public void PrintAvailableCookFoodInfo()
{
Console.WriteLine("\n***조리가능 레시피 정보***");
int index = 0;
foreach (Recipe recipe in this.arrUseRecipe)
{
if (recipe != null && recipe.name == "괴물 오믈렛")
{
Console.WriteLine("\n{0}, {1}의 재료", index, recipe.name);
index++;
foreach (FoodIngredient ingredient in this.arrFoodIngredients)
{
if (ingredient != null)
{
if (ingredient.name == "거대한 알" || ingredient.name == "독특한 양념")
{
Console.WriteLine("{0}x{1}", ingredient.name, ingredient.amount);
}
}
}
}
if (recipe != null && recipe.name == "엉겅퀴 차")
{
Console.WriteLine("\n{0}, {1}의 재료", index, recipe.name);
index++;
foreach (FoodIngredient ingredient in this.arrFoodIngredients)
{
if (ingredient != null)
{
if (ingredient.name == "토끼엉겅퀴풀" || ingredient.name == "맑은 샘물")
{
Console.WriteLine("{0}x{1}", ingredient.name, ingredient.amount);
}
}
}
}
if (recipe != null && recipe.name == "랩터 숯불구이")
{
Console.WriteLine("\n{0}, {1}의 재료", index, recipe.name);
index++;
foreach (FoodIngredient ingredient in this.arrFoodIngredients)
{
if (ingredient != null)
{
if (ingredient.name == "랩터 고기" || ingredient.name == "매운 양념")
{
Console.WriteLine("{0}x{1}", ingredient.name, ingredient.amount);
}
}
}
}
}
}
//요리를 만든다--> 조리가능한 음식목록에서 + 재료가 있으면 만들 수 있음 -> 재료가 줄어듬
public Food Cook(string name)
{
Console.WriteLine("\n***요리하기***");
Recipe food = null;
foreach (Recipe recipe in this.arrUseRecipe)
{
if (recipe != null)
{
if (recipe.name == name)
{
//괴물 오믈렛 = 거대한 알 + 독특한 양념
if (recipe.name == "괴물 오믈렛")
{
for (int i = 0; i < this.arrFoodIngredients.Length; i++)
{
FoodIngredient ingredient = this.arrFoodIngredients[i];
if (ingredient != null)
{
if (ingredient.myAmount > 0 && ingredient.name == "거대한 알" || ingredient.name == "독특한 양념")
{
food = recipe;
//내가 가진 재료 양 줄이기
this.CountAmount(ingredient);
Console.WriteLine("남은 재료 : {0}x{1}", ingredient.name, ingredient.myAmount);
}
}
}
Console.WriteLine("{0} 완성", food.name);
}
//엉겅퀴 차 = 토끼엉겅퀴풀 + 맑은 샘물
if (recipe.name== "엉겅퀴 차")
{
for (int i = 0; i < this.arrFoodIngredients.Length; i++)
{
FoodIngredient ingredient = this.arrFoodIngredients[i];
if (ingredient != null)
{
if (ingredient.myAmount > 0 && ingredient.name == "토끼엉겅퀴풀" || ingredient.name == "맑은 샘물")
{
food = recipe;
//내가 가진 재료 양 줄이기
this.CountAmount(ingredient);
Console.WriteLine("남은 재료 : {0}x{1}", ingredient.name, ingredient.myAmount);
}
}
}
Console.WriteLine("{0} 완성", food.name);
return new Food(food.name);
}
//랩터 숯불구이= 랩터 고기 + 매운 양념
if (recipe.name == "랩터 숯불구이")
{
for (int i = 0; i < this.arrFoodIngredients.Length; i++)
{
FoodIngredient ingredient = this.arrFoodIngredients[i];
if (ingredient != null)
{
if (ingredient.myAmount > 0 && ingredient.name == "랩터 고기" || ingredient.name == "매운 양념")
{
food = recipe;
//내가 가진 재료 양 줄이기
this.CountAmount(ingredient);
Console.WriteLine("남은 재료 : {0}x{1}", ingredient.name, ingredient.myAmount);
}
}
}
Console.WriteLine("{0} 완성", food.name);
return new Food(food.name);
}
}
}
else if (recipe == null)
{
Console.WriteLine("{0}을(를) 요리할 수 없습니다.",name);
return null;
}
}
return null;
}
//내가 가진 재료양 감소시키기
public void CountAmount(FoodIngredient ingredient)
{
foreach(FoodIngredient myIngredient in this.arrFoodIngredients)
{
if(myIngredient==ingredient)
{
myIngredient.myAmount--;
}
}
}
//내가 소지중인 재료 목록 출력
public void PrintMyIngredient()
{
int index = 0;
Console.WriteLine("\n***소지중인 재료***");
foreach(FoodIngredient ingredient in this.arrFoodIngredients)
{
if(ingredient!=null)
{
Console.WriteLine("{0}. {1} x{2}",index,ingredient.name,ingredient.myAmount);
index++;
}
}
}
}
}
|
|
|
3. Recipe class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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[] arrFoodIngredientsNames; //필요한 재료들 이름
public Recipe(string name, string[] arrFoodIngredientsNames)
{
this.name = name;
this.arrFoodIngredientsNames = arrFoodIngredientsNames;
Console.WriteLine("\n조리법 {0}이(가) 생성되었습니다.",this.name);
}
}
}
|
4. FoodIngredient class
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 FoodIngredient
{
public string name; //조리법에 나타날 이름
public int amount; //조리법에 나타날 양
public int myAmount; //내가 소지할 실제 재료의 양
public FoodIngredient(string name, int amount)
{
this.name = name;
this.amount = amount;
Console.WriteLine("\n{0} {1}개가 생성되었습니다.",this.name, this.amount);
}
}
}
|
5. Food class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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 Food(string name)
{
this.name = name;
}
}
}
|
6. 결과값
1)
캐릭터와 조리법(recipe)과 재료(ingredient) 객체 생성
조리법 획득 메서드, 재료 획득 메서드 호출
이어서
2)
소지중인 레시피 목록을 출력하는 메서드 호출
레시피 사용 메서드 호출
-소지중인 레시피 목록 ->조리가능 레시피 목록
-소지중인 레시피 목록에서 해당 배열의 값(레시피 객체)을 지움
-정렬 메서드를 호출해서 정렬함
조리가능 레시피 목록 출력하는 메서드 호출
이어서
3) 조리가능 레시피 정보를 출력하는 메서드 호출
소지중인 재료의 이름과 가지고 있는 재료의 양을 출력하는 메서드 호출
요리 메서드를 호출한 뒤 재료의 양을 감소시키는 메서드 호출
'C# > 과제' 카테고리의 다른 글
2020.04.21. 과제 - json 파일 불러오기 (0) | 2020.04.21 |
---|---|
2020.04.20. 과제 - 길바닥 아이템 획득하기(컬렉션(list)) (0) | 2020.04.21 |
2020.04.14. 과제 - 아이템 배열 값 빼기, 배열 정리하기(Inventory class, Item class) (0) | 2020.04.14 |
2020.04.10. 과제 - Character class, Item class (0) | 2020.04.10 |
2020.04.08. 종족, 직업 선택하기 (switch, 열거형 변환) (0) | 2020.04.08 |