09.23 상점 구매하기 및 판매하기

C#/실습 2019. 9. 23. 15:57

예제.

1.구매하기 기능과 판매하기 기능

구매및 판매

1.1 보유 금화 및 상점에 있는 아이템 종류, 가격및 수량 설정

상점페이지

1.2 구입하고자 아이템 종류와 수량 설정기능과 상점의 재고 상태 고려

1.3 보유 금화보다 많은 아이템 구매시:

1.4 상점의 재고량 보다 많은 아이템 구매시:

1.5 재고가 없는 아이템 구매시:

1.6 상점에 없는 아이템 선택시:

 

코드:

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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax05
{
    class Program
    {
        static void Main(string[] args)
        {
 
            string a = "장검";
            string b = "단검";
            string c = "활";
            string d = "도끼";
            int shopa = 5;//아이템 수량
            int shopb = 6;
            int shopc = 7;
            int shopd = 8;
            int a1 = 800;//아이템 가격
            int a2 = 550;
            int a3 = 760;
            int a4 = 810;
            int wallet = 5000;
            int ha1 = 0//보유 개수
            int ha2 = 0;
            int ha3 = 0;
            int ha4 = 0;
            while (true)
            {
            Restart:
                Console.WriteLine("1. 상품 구매하기");
                Console.WriteLine("2. 상품 판매하기");
                string choice = Console.ReadLine();
                if (choice == "1")//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@구매하기 시작
                {
                    Console.WriteLine("*** 상점 ***");
                    Console.WriteLine($"금화: {wallet}");
                    Console.WriteLine($"1. 장검: {a1}원, 보유 {shopa}개");
                    Console.WriteLine($"2. 단검: {a2}원, 보유 {shopb}개.");
                    Console.WriteLine($"3. 활: {a3}원, 보유{shopc}개.");
                    Console.WriteLine($"4. 도끼: {a4}원, 보유 {shopd}개.");
                    Console.Write("상점에서 구입 하고자 하는 아이템을 입력해주세요: ");
                    string input = Console.ReadLine();
                    if (input == "장검")
                    {
                        if (shopa >= 1)
                        {
                            if (wallet >= a1)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba1 = Console.ReadLine();
                                int ba1n = Convert.ToInt32(ba1);
                                if (shopa >= ba1n)
                                {
                                    if (wallet >= a1 * ba1n)
                                    {
                                        Console.WriteLine($"{input}을 {ba1n}개 구입했습니다. (-{a1 * ba1n})");
                                        wallet = wallet - (a1 * ba1n);
                                        ha1 = ha1 + ba1n;
                                        shopa = shopa - ba1n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else if (input == "단검")
                    {
                        if (shopb >= 1)
                        {
                            if (wallet >= a2)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba2 = Console.ReadLine();
                                int ba2n = Convert.ToInt32(ba2);
                                if (shopb >= ba2n)
                                {
                                    if (wallet >= a2 * ba2n)
                                    {
                                        Console.WriteLine($"{input}을 {ba2n}개 구입했습니다. (-{a2 * ba2n})");
                                        wallet = wallet - (a2 * ba2n);
                                        ha2 = ha2 + ba2n;
                                        shopb = shopb - ba2n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else if (input == "활")
                    {
                        if (shopc >= 1)
                        {
                            if (wallet >= a3)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba3 = Console.ReadLine();
                                int ba3n = Convert.ToInt32(ba3);
                                if (shopc >= ba3n)
                                {
                                    if (wallet >= a3 * ba3n)
                                    {
                                        Console.WriteLine($"{input}을 {ba3n}개 구입했습니다. (-{a3 * ba3n})");
                                        wallet = wallet - (a3 * ba3n);
                                        ha3 = ha3 + ba3n;
                                        shopc = shopc - ba3n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else if (input == "도끼")
                    {
                        if (shopd >= 1)
                        {
                            if (wallet >= a4)
                            {
                                Console.WriteLine("------------------");
                                Console.Write("구입하고자 하는 갯수를 입력해주세요: ");
                                string ba4 = Console.ReadLine();
                                int ba4n = Convert.ToInt32(ba4);
                                if (shopd >= ba4n)
                                {
                                    if (wallet >= a4 * ba4n)
                                    {
                                        Console.WriteLine($"{input}을 {ba4n}개 구입했습니다. (-{a4 * ba4n})");
                                        wallet = wallet - (a4 * ba4n);
                                        ha4 = ha4 + ba4n;
                                        shopd = shopd - ba4n;
                                    }
                                    else
                                    {
                                        Console.WriteLine("금화가 부족합니다.");
                                    }
                                    Console.WriteLine("------------------");
                                }
                                else
                                {
                                    Console.WriteLine("재고가 부족합니다.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("금화가 부족합니다.");
                                Console.WriteLine("------------------");
                            }
                        }
                        else
                        {
                            Console.WriteLine("매진 되었습니다.");
                            Console.WriteLine("------------------");
                        }
                    }
                    else
                    {
                        Console.WriteLine("------------------");
                        Console.WriteLine($"해당상품 (\"{input}\")(은)는 없습니다.");
                        Console.WriteLine("------------------");
                    }
                }// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 구매하기끝
                else if (choice == "2")
                {
                    Console.WriteLine($"금화: {wallet}");
                    Console.WriteLine("------------------");
                    Console.WriteLine("보유 목록:");
                    if (ha1 > 0)
                    {
                        Console.WriteLine($"{a} X {ha1}");
                    }
                    if (ha2 > 0)
                    {
                        Console.WriteLine($"{b} X {ha2}");
                    }
                    if (ha3 > 0)
                    {
                        Console.WriteLine($"{c} X {ha3}");
                    }
                    if (ha4 > 0)
                    {
                        Console.WriteLine($"{d} X {ha4}");
                    }
                    Console.Write("판매할 아이템을 입력해주세요: ");
                    string input = Console.ReadLine();
                    if (input == "장검")
                    {
                        if (ha1 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa1 = Console.ReadLine();
                            int nsa1 = Convert.ToInt32(sa1);
                            if (ha1 >= nsa1)
                            {
                                ha1 = ha1 - nsa1;
                                Console.WriteLine($"{input}을 {nsa1}개 판매했습니다. (+{a1 * nsa1})");
                                wallet = wallet + (a1 * nsa1);
                                shopa = shopa + nsa1;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
 
                    }
                    else if (input == "단검")
                    {
                        if (ha2 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa2 = Console.ReadLine();
                            int nsa2 = Convert.ToInt32(sa2);
                            if (ha2 >= nsa2)
                            {
                                ha2 = ha2 - nsa2;
                                Console.WriteLine($"{input}을 {nsa2}개 판매했습니다. (+{a2 * nsa2})");
                                wallet = wallet + (a2 * nsa2);
                                shopb = shopb + nsa2;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
                    }
                    else if (input == "활")
                    {
                        if (ha3 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa3 = Console.ReadLine();
                            int nsa3 = Convert.ToInt32(sa3);
                            if (ha3 >= nsa3)
                            {
                                ha3 = ha3 - nsa3;
                                Console.WriteLine($"{input}을 {nsa3}개 판매했습니다. (+{a3 * nsa3})");
                                wallet = wallet + (a3 * nsa3);
                                shopc = shopc + nsa3;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
 
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
                    }
                    else if (input == "도끼")
                    {
                        if (ha4 >= 1)
                        {
                            Console.WriteLine("------------------");
                            Console.WriteLine("------------------");
                            Console.Write("판매할 수량을 입력해주세요: ");
                            string sa4 = Console.ReadLine();
                            int nsa4 = Convert.ToInt32(sa4);
                            if (ha4 >= nsa4)
                            {
                                ha4 = ha4 - nsa4;
                                Console.WriteLine($"{input}을 {nsa4}개 판매했습니다. (+{a4 * nsa4})");
                                wallet = wallet + (a4 * nsa4);
                                shopd = shopd + nsa4;
                            }
                            else
                            {
                                Console.WriteLine("더 이상 판매 할 수 없습니다.");
                            }
 
                        }
                        else
                        {
                            Console.WriteLine("더 이상 판매 할 수 없습니다.");
                        }
 
                    }
                    else
                    {
                        Console.WriteLine("------------------");
                        Console.WriteLine($"해당상품 (\"{input}\")(은)는 없습니다.");
                        Console.WriteLine("------------------");
                    }
 
                }//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@판매 하기 끝
                else
                {
                    Console.WriteLine("잘못된 메뉴입니다.");
                    goto Restart;
 
                }
                Console.WriteLine($"금화: {wallet}");
                Console.WriteLine("------------------");
                Console.WriteLine("보유 목록:");
                if (ha1 > 0)
                {
                    Console.WriteLine($"{a} X {ha1}");
                }
                if (ha2 > 0)
                {
                    Console.WriteLine($"{b} X {ha2}");
                }
                if (ha3 > 0)
                {
                    Console.WriteLine($"{c} X {ha3}");
                }
                if (ha4 > 0)
                {
                    Console.WriteLine($"{d} X {ha4}");
                }
                Console.WriteLine("------------------");
                Console.WriteLine();
                Console.WriteLine();
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

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

switch 문  (0) 2019.09.26
09.24 최대공약수 구하기  (0) 2019.09.24
09.20 줄넘기  (0) 2019.09.20
09.20 for문을 이용한 던전게임  (0) 2019.09.20
09.20 과일입력, 숫자입력, 문자열에 따옴표입력  (0) 2019.09.20
: