<인벤토리>List<T> 제네릭형

C#/수업내용 2019. 4. 1. 18:15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class App
    {
        public App()
        {
 
        }
        public void Run()
        {
            var EquipmentInventory = new Inventory<EquipmentItem>(5);
            var PotionItem = new Inventory<PotionItem>(5);
 
            //시작
            while(true)
            {
                Console.WriteLine("1.장비가방/2.포션가방/0.종료");
                var input = Console.ReadLine();
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")
                {
                    while (true)
                    {
                        Console.WriteLine("========================장비가방========================");
                        Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", EquipmentInventory.items.Count, EquipmentInventory.inventoryVolumeMax);
                        Console.Write("입력(숫자):");
                        input = Console.ReadLine();
 
                        if (input == "0")//종료
                        {
                            Console.WriteLine("인벤토리를 닫습니다.");
                            break;
                        }
                        else if (input == "1")//아이템 생성
                        {
                            if (EquipmentInventory.items.Count >= EquipmentInventory.inventoryVolumeMax)
                            {
                                Console.WriteLine("인벤토리가 가득 찼습니다.");
                            }
                            else
                            {
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("생성할 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                if (input == "롱소드")
                                {
                                    EquipmentInventory.AddItem(new EquipmentItem(new ItemData(00"롱소드"50)));
                                }
                                else if (input == "숏소드")
                                {
                                    EquipmentInventory.AddItem(new EquipmentItem(new ItemData(10"숏소드"30)));
                                }
                                else if (input == "빨간포션")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "파란포션")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("잘못된 입력입니다.");
                                }
                            }
                        }
                        else if (input == "2")//아이템 제거
                        {
                            if (EquipmentInventory.items.Count <= 0)
                            {
                                Console.WriteLine("버릴 아이템이 없습니다.");
                            }
                            else
                            {
                                EquipmentItem founditem = null;
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("버릴 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                foreach (var item in EquipmentInventory.items)
                                {
                                    if (item.itemData.name == input)
                                    {
                                        founditem = item;
                                        break;
                                    }
                                }
                                if (founditem == null)
                                {
                                    Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                                    EquipmentInventory.RemoveItem(founditem);
                                }
                            }
                        }
                        else if (input == "3")//목록보기
                        {
                            if (EquipmentInventory.items.Count <= 0)
                            {
                                Console.WriteLine("인벤토리가 비었습니다.");
                            }
                            else
                            {
                                Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", EquipmentInventory.items.Count, EquipmentInventory.inventoryVolumeMax);
                                foreach (Item item in EquipmentInventory.items)
                                {
                                    Console.WriteLine(item.itemData.name);
                                }
                            }
                        }
                        else if (input == "4")
                        {
                            Console.WriteLine("인벤토리 확장완료!");
                            EquipmentInventory.inventoryVolumeMax += 5;
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")
                {
                    while (true)
                    {
                        Console.WriteLine("========================포션가방========================");
                        Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", PotionItem.items.Count, PotionItem.inventoryVolumeMax);
                        Console.Write("입력(숫자):");
                        input = Console.ReadLine();
 
                        if (input == "0")//종료
                        {
                            Console.WriteLine("인벤토리를 닫습니다.");
                            break;
                        }
                        else if (input == "1")//아이템 생성
                        {
                            if (PotionItem.items.Count >= PotionItem.inventoryVolumeMax)
                            {
                                Console.WriteLine("인벤토리가 가득 찼습니다.");
                            }
                            else
                            {
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("생성할 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                if (input == "롱소드")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "숏소드")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "빨간포션")
                                {
                                    PotionItem.AddItem(new PotionItem(new ItemData(21"빨간포션"00)));
                                }
                                else if (input == "파란포션")
                                {
                                    PotionItem.AddItem(new PotionItem(new ItemData(31"파란포션"00)));
                                }
                                else
                                {
                                    Console.WriteLine("잘못된 입력입니다.");
                                }
                            }
                        }
                        else if (input == "2")//아이템 제거
                        {
                            if (PotionItem.items.Count <= 0)
                            {
                                Console.WriteLine("버릴 아이템이 없습니다.");
                            }
                            else
                            {
                                PotionItem founditem = null;
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("버릴 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                foreach (var item in PotionItem.items)
                                {
                                    if (item.itemData.name == input)
                                    {
                                        founditem = item;
                                        break;
                                    }
                                }
                                if (founditem == null)
                                {
                                    Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                                    PotionItem.RemoveItem(founditem);
                                }
                            }
                        }
                        else if (input == "3")//목록보기
                        {
                            if (PotionItem.items.Count <= 0)
                            {
                                Console.WriteLine("인벤토리가 비었습니다.");
                            }
                            else
                            {
                                Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", PotionItem.items.Count, PotionItem.inventoryVolumeMax);
                                foreach (Item item in PotionItem.items)
                                {
                                    Console.WriteLine(item.itemData.name);
                                }
                            }
                        }
                        else if (input == "4")
                        {
                            Console.WriteLine("인벤토리 확장완료!");
                            PotionItem.inventoryVolumeMax += 5;
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("잘못된 입력 입니다.");
                }
            }
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Inventory<T>
    {
        public int inventoryVolumeMax;
        public List<T> items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new List<T>();
            this.inventoryVolumeMax = inventoryVolumeMax;
        }
 
        //추가
        public void AddItem(T item)//아이템 추가
        {
            //아이템 추가
            this.items.Add(item);
        }
        //제거
        public void RemoveItem(T founditem)
        {
            this.items.Remove(founditem);
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Potion = 1
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
        }
    }
}
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_06
{
    class EquipmentItem:Item
    {
        public EquipmentItem(ItemData itemData) : base(itemData)
        {
            if (this.itemData.type != 0)
            {
                Console.WriteLine("해당 아이템은 장비가방에 넣을 수 없습니다.");
            }
            else
            {
                this.itemData = itemData;
                Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
            }
        }
    }
}
 
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_06
{
    class PotionItem:Item
    {
        public PotionItem(ItemData itemData):base(itemData)
        {
            if (this.itemData.type != 1)
            {
                Console.WriteLine("해당 아이템은 포션가방에 넣을 수 없습니다.");
            }
            else
            {
                this.itemData = itemData;
                Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
            }
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class ItemData
    {
        //아이템 데이터가 가지고 있어야하는 것, 아이디, 타입, 이름, 공격력, 방어력
        public int id;
        public int type;
        public string name;
        public int damage;
        public int armor;
 
        //아이템 데이터의 생성자
        public ItemData(int id, int type, string name, int damage, int armor)
        {
            this.id = id;
            this.type = type;
            this.name = name;
            this.damage = damage;
            this.armor = armor;
        }
    }
}
cs


'C# > 수업내용' 카테고리의 다른 글

<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기  (0) 2019.04.03
<인벤토리>Array형으로 롤백  (0) 2019.04.01
이따가 분류  (0) 2019.03.29
1차원 배열  (0) 2019.03.28
<foreach in, for, while>  (0) 2019.03.27
: