15일차 배열과 컬렉션을 사용해 인벤토리 만들기
Console Programming/C# Console 2019. 10. 11. 13:17
|
Program.cs
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Syntax03
{
class Program
{
static void Main(string[] args)
{
new App();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
|
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Syntax03
{
class App
{
public App()
{
Item item1 = new Item("장검");
Item item2 = new Item("장검");
Item item3 = new Item("도끼");
Item item4 = new Item("활");
Item item5 = new Item("지팡이");
//인벤토리타입의 인스턴스 생성
Inventory inventory = new Inventory();
//배열에 아이템 추가
inventory.AddItem(item1);
inventory.AddItem(item2);
inventory.AddItem(item3);
inventory.AddItem(item4);
inventory.AddItem(item5);
//배열 출력
inventory.PrintAllItems();
//컬렉션에 아이템 추가
inventory.AddItemList(item1);
inventory.AddItemList(item2);
inventory.AddItemList(item3);
inventory.AddItemList(item4);
inventory.AddItemList(item5);
//컬렉션 출력
inventory.PrintAllItemsList();
//배열에서 도끼를 찾음
var foundItem1 = inventory.FindItemByName("도끼");
//배열에서 장거를 찾음
var foundItem2 = inventory.FindItemByNameList("장거");
//배열에서 장검을 제거
inventory.RemoveItem("장검");
inventory.PrintAllItems();
//배열에서 장검을 제거
inventory.RemoveItem("장검");
inventory.PrintAllItems();
//컬렉션에서 장검을 제거
inventory.RemoveItemList("장검");
inventory.PrintAllItemsList();
//컬렉션에서 장검을 제거
inventory.RemoveItemList("장검");
inventory.PrintAllItemsList();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
|
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
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Syntax03
{
class Inventory
{
//data
//name
//Item[]
//List<Item>
public Item[] arrItems = new Item[5];
public List<Item> itemList = new List<Item>();
public Inventory()
{
}
public void AddItem(Item item)
{
{
if (arrItems[i] == null)
{
arrItems[i] = item;
return;
}
{
arrItems[i].quantity++;
return;
}
}
}
public void AddItemList(Item item)
{
{
{
itemList[i].quantity++;
return;
}
}
}
public void RemoveItem(string name)
{
{
if(arrItems[i] != null && arrItems[i].name == name)
{
if(arrItems[i].quantity>1)
{
arrItems[i].quantity--;
return;
}
arrItems[i] = null;
}
}
}
public void RemoveItemList(string name)
{
{
if (itemList[i].name == name)
{
if(itemList[i].quantity>1)
{
itemList[i].quantity--;
return;
}
itemList.RemoveAt(i);
}
}
}
public Item FindItemByName(string name)
{
{
if (arrItems[i] != null && arrItems[i].name == name)
{
return arrItems[i];
}
}
return null;
}
public Item FindItemByNameList(string name)
{
if(founditem == null)
{
Console.WriteLine("아이템을 찾지 못했습니다");
return founditem;
}
return founditem;
}
public void PrintAllItems()
{
foreach(Item item in arrItems)
{
if(item != null && item.quantity>0)
{
}
}
Console.WriteLine();
}
public void PrintAllItemsList()
{
foreach (Item item in itemList)
{
if (item != null && item.quantity>0)
{
}
}
Console.WriteLine();
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
|
Item.cs
|
|
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;
namespace Syntax03
{
class Item
{
public string name;
public int quantity;
public Item(string name)
{
this.name = name;
quantity = 1;
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'Console Programming > C# Console' 카테고리의 다른 글
2019-10-14 UIPanel 과 Data 클래스 만들기 (수정본) (0) | 2019.10.14 |
---|---|
2019-10-14 UIPanel 과 Data 클래스 만들기 (0) | 2019.10.14 |
백준 8958 OX퀴즈 (0) | 2019.10.11 |
백준 2920 음계 (0) | 2019.10.10 |
14일차 배열, 인벤토리에 아이템배열을 넣고, 인벤토리에 메서드 구현하기 (0) | 2019.10.10 |