<인벤토리 1차>중첩방식 목록보기

C#/과제 2019. 3. 31. 18:35



Program.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
 
cs

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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class App
    {
        public App()
        {
 
        }
        public void Run()
        {
            var inventory = new Inventory(10);//인벤토리 생성, 및 최대량 지정
            Console.WriteLine("인벤토리 오픈");
 
            //메인메뉴
            while (true)
            {
                Console.WriteLine("===================================================================");
                Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/0.종료({0}/{1})", inventory.items.Count, inventory.inventoryVolumeMax);
                Console.Write("입력(숫자):");
                var input = Console.ReadLine();
 
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")//아이템 생성
                {
                    if (inventory.items.Count >= inventory.inventoryVolumeMax)
                    {
                        Console.WriteLine("인벤토리가 가득 찼습니다.");
                    }
                    else
                    {
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 천옷, 판금갑옷, 반지, 목걸이)");
                        Console.Write("생성할 아이템 이름을 입력:");
                        input = Console.ReadLine();
 
                        if (input == "롱소드")
                        {
                            inventory.AddItem(new Item(new ItemData(00"롱소드"50)));
                        }
                        else if (input == "숏소드")
                        {
                            inventory.AddItem(new Item(new ItemData(10"숏소드"30)));
                        }
                        else if (input == "천옷")
                        {
                            inventory.AddItem(new Item(new ItemData(21"천옷"05)));
                        }
                        else if (input == "판금갑옷")
                        {
                            inventory.AddItem(new Item(new ItemData(31"판금갑옷"010)));
                        }
                        else if (input == "반지")
                        {
                            inventory.AddItem(new Item(new ItemData(42"반지"01)));
                        }
                        else if (input == "목걸이")
                        {
                            inventory.AddItem(new Item(new ItemData(52"목걸이"00)));
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")//아이템 제거
                {
                    if (inventory.items.Count <= 0)
                    {
                        Console.WriteLine("버릴 아이템이 없습니다.");
                    }
                    else
                    {
                        Item founditem = null;
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 천옷, 판금갑옷, 반지, 목걸이)");
                        Console.Write("버릴 아이템 이름을 입력:");
                        input = Console.ReadLine();
 
                        foreach (var item in inventory.items)
                        {
                            if (item.itemData.name == input)
                            {
                                founditem = item;
                                break;
                            }
                        }
                        if (founditem == null)
                        {
                            Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                        }
                        else
                        {
                            Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                            inventory.RemoveItem(founditem);
                        }
                    }
                }
                else if (input == "3")//목록보기
                {
                    int countLongSword = 0, countShortSword = 0, countCloth = 0, countPlateArmor = 0, countRing = 0, countNecklace = 0;//아이템 카운터
                    if (inventory.items.Count <= 0)
                    {
                        Console.WriteLine("인벤토리가 비었습니다.");
                    }
                    else
                    {
                        Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", inventory.items.Count, inventory.inventoryVolumeMax);
                        foreach (Item item in inventory.items)
                        {
                            if (item.itemData.id == 0)
                            {
                                countLongSword++;
                            }
                            else if (item.itemData.id == 1)
                            {
                                countShortSword++;
                            }
                            else if (item.itemData.id == 2)
                            {
                                countCloth++;
                            }
                            else if (item.itemData.id == 3)
                            {
                                countPlateArmor++;
                            }
                            else if (item.itemData.id == 4)
                            {
                                countRing++;
                            }
                            else if (item.itemData.id == 5)
                            {
                                countNecklace++;
                            }
                        }
                        if (countLongSword != 0)
                        {
                            Console.WriteLine("롱소드x{0}", countLongSword);
                        }
                        if (countShortSword != 0)
                        {
                            Console.WriteLine("숏소드x{0}", countShortSword);
                        }
                        if (countCloth != 0)
                        {
                            Console.WriteLine("천옷x{0}", countCloth);
                        }
                        if (countPlateArmor != 0)
                        {
                            Console.WriteLine("판금갑옷x{0}", countPlateArmor);
                        }
                        if (countRing != 0)
                        {
                            Console.WriteLine("반지x{0}", countRing);
                        }
                        if (countNecklace != 0)
                        {
                            Console.WriteLine("목걸이x{0}", countNecklace);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("잘못된 입력입니다.");
                }
            }
        }
    }
}
 
cs


Inventory.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class Inventory
    {
        public int inventoryVolumeMax;
        public List<Item> items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new List<Item>();
            this.inventoryVolumeMax = inventoryVolumeMax;
 
            Console.WriteLine("인벤토리 생성(0/{0})", inventoryVolumeMax);
        }
 
        //추가
        public void AddItem(Item item)//아이템 추가
        {
            //아이템 추가
            this.items.Add(item);
        }
        //제거
        public void RemoveItem(Item founditem)
        {
            this.items.Remove(founditem);
        }
        //목록
    }
}
 
cs

Item.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Armor = 1,
            Accessory = 2
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
            Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
        }
    }
}
 
cs

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
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    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# > 과제' 카테고리의 다른 글

<인벤토리> 저장, 불러오기 기능  (0) 2019.04.08
<IEnumerable과 IEnumerator> 내일 다시 확인하여 예제 준비할 것  (0) 2019.04.01
<인벤토리> 0.5차  (0) 2019.03.27
<객체와 개체의 차이>  (0) 2019.03.27
<코드읽기>  (0) 2019.03.26
: