본문 바로가기

C#/수업내용

2020-04-16 수업내용 Source

-----------------------------------------------------------------------------------------------------------------------------------

Fruit Class

-----------------------------------------------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    class Fruit
    {
        public string name;
        public Fruit(string name)
        {
            this.name = name;
            Console.WriteLine("장바구니에 [{0}]를 담았습니다."this.name);
        }
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

Cart 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    class Cart
    {
        public Fruit[] arrfruits;
        int inx = 0;
 
        public Cart(int capacity)
        {
            arrfruits = new Fruit[capacity];
            Console.WriteLine("[{0}]개의 과일을 담을 수 있는 장바구니가 생성되었습니다!\n",capacity);   
        }
 
        public void AddFruit(string name)
        { 
            this.arrfruits[inx] = new Fruit(name);
            this.inx++;
        }
 
        public void PrintItem()
        {
            int i = 0;
 
            foreach (Fruit fruit in this.arrfruits)
            {
                if (this.arrfruits[i] == null)
                {
                    Console.WriteLine("과일이 없습니다.");
                }
                else if(this.arrfruits[i] != null)
                {
                    Console.WriteLine("{0}, {1}, {2}", i, this.arrfruits[i], this.arrfruits[i].name);
                    
                }
                i++;
            }
        
        }
 
            
       
    }
}
 
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

Item 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    class Item
    {
        //public string name;
        //public Item(string name)
        //{
        //    this.name = name;
        //}
 
 
 
        public string name;
        public Item(string name)
        {
            this.name = name;
            Console.WriteLine("{0} 가 생성되었습니다.",this.name);        
        }
 
    }
}
 
 

-----------------------------------------------------------------------------------------------------------------------------------

Inventory 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    class Inventory
    {
        //public Item[] items;
 
        ////string item1Name; //아이템이름1 저장변수
        ////string item1Name; //아이템이름2 저장변수
        ////string item1Name; //아이템이름3 저장변수
        ////string item1Name; //아이템이름4 저장변수
 
        //public int itemInx= 0; //아이템이 추가된 횟수
        //public Inventory(int capacity)
        //{
        //    items = new Item[capacity];
        //}
 
        //public void Add(string itemName)
        //{
        //    if (this.itemInx > this.items.Length-1)
        //    {
        //        Console.WriteLine("가방이 가득차서 아이템을 추가할 수 없습니다.");
        //        return;
        //    }
        //    Console.WriteLine(itemName + "이 추가 되었습니다.");
        //    this.itemInx++;
        //}
 
 
        //public void AddItems(Item[] arritems)
        //{ 
 
        //}
 
 
 
 
        //public void PrintItemNames()
        //{
        //    //배열의 첫 인덱스 부터 배열의 마지막 인덱스
        //    for (int i = 0; i < items.Length; i++)
        //    {
        //        //i : 배열의 인덱스
        //        //string itemName = this.arrBag[i];
        //        //Console.WriteLine(itemName);
        //        Console.WriteLine(this.items[i].name);
        //    }
        //}
 
 
        public Item[] arrItems;
        int arrIndex = 0// 배열 인덱스
 
 
        //생성자
        public Inventory()
        {
            //Item 형식의 값을 3개를 할당 할 수 있는 배열 객체 생성
            this.arrItems = new Item[3];
            Console.WriteLine("인벤토리가 생성되었습니다.");
        }
 
        //단일 아이템을 전달받아 배열의 요소에 할당
 
        public void AddItem(Item item)
        {
            //int n = 0 배열인덱스를 메서드 지역변수로 두었을때 호출될때마다 초기화됨.
            //배열 객체의 n번 인덱스에 매개변수 item값을 할당
            this.arrItems[arrIndex++= item;
        }
 
 
        //배열객체를 전달받아 인벤이 가지고 있는 배열에 요소를 옮겨 담음
 
        public void AddItems(Item[] arrItems)
        {
            for (int i = 0; i < arrItems.Length; i++)
            {
                Console.WriteLine("내꺼 : {0}, 전달받은거: {1}",this.arrItems[i],arrItems[i]);
                this.arrItems[i] = arrItems[i];
            }
 
            Console.WriteLine("----------------------------------------------");
 
            for (int i = 0; i < arrItems.Length; i++)
            {
                Console.WriteLine("내꺼 : {0}, 전달받은거: {1}"this.arrItems[i], arrItems[i]);
            }
            //인벤객체의 배열객체의 첫번째 요소의 이름 변경
 
            Console.WriteLine("----------------------------------------------");
 
        }
        public Item[] GetItems()
        {
            //새로운 배열객체 생성
            Item[] newArrItems = new Item[this.arrItems.Length];
            //배열의 각 요소를 새롭게 생성해서 (새로운 아이템객체를 생성하여)
            //새로운 배열객체에 할당
            for (int i = 0; i < this.arrItems.Length; i++)
            {
                Item item = new Item(this.arrItems[i].name);
                newArrItems[i] = item;
            }
            //새로운 배열객체를 반환
            return newArrItems;
        }
 
 
 
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    class App
    {
        public App()
        {
            //Console.WriteLine("2020-04-16");
 
            //type[] arrayName;
            //int[] array1 = new int[5];
 
            //배열의 선언, 초기화
            //string[] itemNames = new string[3];
 
            //itemNames[0] = "바나나";
            //itemNames[1] = "귤";
            //itemNames[2] = "수박";
            //Console.WriteLine(itemNames.Length); = 3
            //3
            //itemNames[0] = 초기값 = null
 
            //아래처럼 코딩하는 것보다 배열을 사용하는게 효율적
            //string fruitName1 = "바나나";
            //string fruitName2 = "귤";
            //string fruitName3 = "수박";
 
            //foreach (string name in itemNames)
            //{
            //    Console.WriteLine(name);
            //}
 
 
 
 
            //배열(Item타입) 정의 하고 초기화 한다.
 
            //Item[] arrItems = new Item[5];
 
            //아이템 객체(인스턴스)를 3개 생성해서 배열객체에 
            //0 ~ 2번 인덱스에 해당하는 변수에 할당
 
            //for (int i = 0; i < 3; i++)
            //{
            //    arrItems[i] = new Item();
            //}
 
            //배열 객체에 5개의 변수의 값을 출력
            //for, foreach
            //해당 값이 null일경우 문자열 "null"을 출력
            //변수의 인덱스도 출력
            //0, Study_010.Item
            //1, Study_010.Item
            //2, Study_010.Item
            //3, null
            //4, null
 
            //for문
 
            //for (int i = 0; i < arrItems.Length; i++)
            //{
            //    if (arrItems[i] == null)
            //    {
            //        Console.WriteLine("null");
            //    }
            //    else
            //    {
            //        Console.WriteLine(arrItems[i]);
            //    }
            //}
 
 
 
            //foreach 문
 
            //int inx = 0;
            //foreach (Item item in arrItems)
            //{
            //    if (arrItems[inx] == null)
            //    {
            //        Console.WriteLine("null");
            //    }
            //    else
            //    {
            //        Console.WriteLine(arrItems[inx]);
            //    }
            //    inx++;
            //}
 
 
            //arrItems[0]
            //arrItems[1]
            //arrItems[2]
            //arrItems[3]
            //arrItems[4]
 
            //for (int i = 0; i < arrItems.Length; i++)
            //{
            //    Console.WriteLine(arrItems[i]);
            //}
 
 
            //foreach을 사용하면 배열값을 보기에는 편한데 인덱스를 알수없음
 
            //int i = 0; // foreach문에서 인덱스 알기
 
            //foreach (Item item in arrItems)
            //{
            //Console.WriteLine(item);
            //Console.WriteLine(arrItems[i]);
            //위아래 값이 같음
 
            //    i++; //foreach문에서 인덱스 알기
            //}
 
            //string[] arrfruits = new string[3];
 
            //for (int i = 0; i < arrfruits.Length; i++)
            //{
            //    Console.Write("좋아하는 과일을 입력해주세요. ");
            //    arrfruits[i] = Console.ReadLine(); 
            //}
 
            //Console.WriteLine();
 
            //foreach (string fruits in arrfruits)
            //{
            //    Console.WriteLine(fruits);
            //}
 
            //Console.WriteLine("\n를 좋아하시는군요.");
 
 
            //Fruit[] arrfruits = new Fruit[3];
 
            //for (int i = 0; i < arrfruits.Length; i++)
            //{
            //    Console.Write("좋아하는 과일을 입력해주세요.");
            //    arrfruits[i] = new Fruit(Console.ReadLine());
            //}
 
            //Console.WriteLine("");
 
            //int inx = arrfruits.Length-1;
            //foreach (Fruit fruit in arrfruits)
            //{
            //    Console.WriteLine("{0}, {1}, {2}", inx, arrfruits[inx], arrfruits[inx].name);
            //    inx--;
            //}
 
            //Console.WriteLine("\n를 좋아하시는군요.");
 
 
 
            //Cart arrCarts = new Cart(5);
 
            //arrCarts.AddFruit("사과");
            //arrCarts.AddFruit("바나나");
            //arrCarts.AddFruit("복숭아");
 
            //Console.WriteLine("");
 
            //arrCarts.PrintItem();
 
            //Console.WriteLine("");
 
 
 
            //배열 선언 및 초기화 연습
 
            //int[] arrInt = new int[10];
 
            //bool[] arrBool = new bool[3];
 
            //arrBool[arrBool.Length - 1] = true;
 
 
 
 
            //길이가 2인 문자열 배열을 선언하고 초기화
            //배열의 마지막 인덱스에 해당하는 변수에 값을 "홍길동" 할당
            //for문을 사용해서 배열의 요소(값)를 출력
 
            //string[] arrString = new string[2];
            //arrString[arrString.Length - 1] = "홍길동";
 
            //for (int i = 0; i < arrString.Length; i++)
            //{
            //    Console.WriteLine(arrString[i]);
            //}
 
 
 
 
 
            //길이가 4인 Item 배열을 선언하고 초기화
            //배열의 마지막 인덱스에 해당하는 변수에 값을 Item객체로 할당
            //for문을 사용해서 배열의 요소를 출력
 
            //Item[] arrItems = new Item[4];
            //arrItems[arrItems.Length - 1] = new Item();
 
            //for (int inx = 0; inx < arrItems.Length; inx++)
            //{
            //    if (arrItems[inx] == null)
            //    {
            //        Console.WriteLine("null이오!!");
            //    }
            //    else
            //    {
            //        Console.WriteLine(arrItems[inx]);
            //    }
            //}
 
 
            //아이템 객체를 생성하고
            //생성시 매개변수로 문자열 아이템이름을 전달해서 맴버 변수(name)에 할당
 
            //Item item = new Item("장검");
            //아이템 객체의 맴버변수(name)의 값을 출력
            //Console.WriteLine(item.name);
 
            //길이가 3개인 Item 배열을 생성하세요
            //Item[] arrItems = new Item[3];
            //마지막 인덱스에 해당하는 변수에 Item객체를 할당
            //Item객체의 이름은 생성시 문자열값을 전달해서 맴버 변수에 할당
            //0, 1, 2
            //Item item = new Item("장검");
            //Console.WriteLine(item.name);
 
            //arrItems[2] = item;
            //Console.WriteLine(item.name);
 
            //arrItems[arrItems.Length - 1] = new Item("단검");
            //for문을 사용해서 Item배열의 각 요소(값)를 출력합니다.
            //for (int inx = 0; inx < arrItems.Length; inx++)
            //{
            //    Console.WriteLine(arrItems[inx]);
            //}
 
            //Console.WriteLine(arrItems[2].name);
 
 
 
            //Inventory inventory = new Inventory();
            //Console.WriteLine(inventory.itemInx);  //0
            //inventory.Add("장검");
            //Console.WriteLine(inventory.itemInx);  //1
            //inventory.Add("단검");
            //Console.WriteLine(inventory.itemInx);  //2
            //inventory.Add("활");
            //Console.WriteLine(inventory.itemInx);  //3
            //inventory.Add("도끼");
            //Console.WriteLine(inventory.itemInx);  //4
            //inventory.Add("창");
            //Console.WriteLine(inventory.itemInx);  //4
 
 
            //string[] arrItemNames = new string[3];
            //arrItemNames[0] = "장검";
            //arrItemNames[1] = "단검";
            //arrItemNames[2] = "활";
            //for (int i = 0; i < arrItemNames.Length; i++)
            //{
            //    Console.WriteLine(arrItemNames[i]);
            //}
 
            //Console.WriteLine("--------------------------------------");
 
            //Item[] arrItems = new Item[3];
 
            //for (int i = 0; i < arrItemNames.Length; i++)
            //{
            //    string itemName = arrItemNames[i];
            //    arrItems[i] = new Item(itemName);     
            //}
 
            //for (int i = 0; i < arrItems.Length; i++)
            //{
            //    Console.WriteLine(arrItems[i].name);
            //}
 
            //Console.WriteLine("--------------------------------------");
 
 
 
            //Inventory inventory = new Inventory(3);
 
            //inventory.Add("장검");
            //inventory.Add("단검");
            //inventory.Add("활");
 
            //Console.WriteLine("--------------------------------------");
 
            //inventory.PrintItemNames();
 
            //Console.WriteLine("--------------------------------------");
 
 
 
 
 
            //장검 -> 장검
            //단검 -> 단검 
            //활 -> 활 
 
 
            //for (int i = 0; i < inventory.items.Length; i++)
            //{
            //    Console.WriteLine(arrItems[i] == arrItems[i]);
            //}
 
 
 
            //Console.WriteLine("--------------------------------------");
 
            //inventory.PrintItemNames();
 
 
            //Console.WriteLine("--------------------------------------");
 
            //string itemNames = "활,도끼,몽둥이";
            //string[] strItemName = itemNames.Split(',');
 
            //for (int i = 0; i < strItemName.Length; i++)
            //{
            //    inventory.items[i].name = strItemName[i];
            //}
 
            //inventory.PrintItemNames();
 
 
 
 
 
            //인벤 객체 생성
            Inventory inven = new Inventory();
 
            //배열객체 생성
            Item[] arrItems = new Item[3];
 
            //아이템 객체 생성  
            Item item1 = new Item("장검");
            Item item2 = new Item("단검");
            Item item3 = new Item("활");
 
            //배열객체에 아이템 객체 할당
            arrItems[0= item1;
            arrItems[1= item2;
            arrItems[2= item3;
 
            //arrItems[0] = new Item("장검");
 
 
            //인벤객체의 배열 0번 인덱스에 아이템 객체 할당
            //inven.arrItems[0] = item;
 
            //인벤 객체의 배열에 단일 아이템 추가
            //inven.AddItem(item1);
 
 
            //인벤 객체에 아이템의 배열객체를 전달
            inven.AddItems(arrItems);
 
            Item[] invenItems = inven.GetItems();
 
 
            //반환 받은 배열의 첫번째 요소의 name의 값을 변경
            invenItems[0].name = "대검";
 
 
            //inven.arrItems[0].name = "대검";
 
            Console.WriteLine(invenItems[0].name); //대검
            Console.WriteLine(item1.name);     //"장검"
            Console.WriteLine(arrItems[0].name); //"장검"
            
        }
    }
}