14일차 배열, 인벤토리에 아이템배열을 넣고, 인벤토리에 메서드 구현하기

Console Programming/C# Console 2019. 10. 10. 13:26

Inventory

=======================

Item[]

=======================

아이템을추가한다.

아이템배열의 이름을 출력한다.

아이템이름으로 검색하고 아이템을 반환한다.

아이템이름으로 검색해서 아이템을 삭제한다.

아이템이름으로 검색해서 ID를 반환한다.

 

Item

=======================

ID

이름

=======================

 

 

 

 
Program.cs
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax01
{
    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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax01
{
    public class App
    {
        public App()
        {
            var inventory = new Inventory();
 
            var item1 = new Item(100"장검");
            var item2 = new Item(200"단검");
            var item3 = new Item(300"도끼");
            var item4 = new Item(400"활");
            var item5 = new Item(500"지팡이");
 
            inventory.AddItem(item1);
            inventory.AddItem(item2);
            inventory.AddItem(item3);
            inventory.AddItem(item4);
            inventory.AddItem(item5);
 
            inventory.DisplayInventoryItemNames();
 
            inventory.SearchItemByName("활");
 
            inventory.RemoveItemByName("단검");
 
            inventory.DisplayInventoryItemNames();
 
            Console.WriteLine(inventory.FindIndexOfItemByName("도끼"));
 
 
 
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax01
{
    public class Inventory
    {
        //data
        //Item[] arrItems
        public Item[] arrItems= new Item[10];
        int index = 0;
        public Inventory()
        {
 
        }
 
        //function
//DisplayInventoryItemNames
        //AddItem
        //SearchItemByName
        //RemoveItemByName
        //FindIndexOfItemByName
        public void DisplayInventoryItemNames()
        {
            Console.WriteLine("아이템 목록을 출력합니다.");
 
            foreach (Item item in arrItems)
            {
                if (item != null)
                {
                    Console.WriteLine(item.name);
                }
            }
        }
 
        public void AddItem(Item item)
        {
            this.arrItems[index] = item;
            index++;
        }
        public Item SearchItemByName(string name)
        {
            Console.WriteLine($"{name}을 찾습니다.");
            foreach(Item item in arrItems)
            {
                if(item != null && item.name == name)
                {
                    Console.WriteLine($"{item.name}을 찾았습니다.");
                    return item;
                }
            }
            Console.WriteLine("아이템을 찾지 못했습니다.");
            return null;
        }
 
        public void RemoveItemByName(string name)
        {
            int i = 0;
            foreach (Item item in arrItems)
            {
                if (item != null && item.name == name)
                {                    
                    arrItems[i] = null;
                    Console.WriteLine($"{item.name}을 인벤토리에서 제거합니다.");
                }
                i++;
            }
        }
        public int FindIndexOfItemByName(string name)
        {
            foreach (Item item in arrItems)
            {
                if (item != null && item.name == name)
                {
                    Console.Write($"{item.name}의 ID를 반환합니다.");
 
                    return item.ID;                    
                }
            }
            Console.WriteLine("아이템을 찾지 못했습니다.");
            return 0;
 
        }
 
 
 
    }
}
 
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
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax01
{
    public class Item
    {
        //data
        //ID
        //name
        public int ID;
        public string name;
        public Item(int ID, string name)
        {
            this.ID = ID;
            this.name = name;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

: