10.10 class를 활용해 인벤토리와 기능 만들기

C#/실습 2019. 10. 10. 13:31

예제:

코드:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class App
    {
        public App()
        {
            Item item1 = new Item(1"장검");
            Item item2 = new Item(2"단검");
            Item item3 = new Item(3"활");
            Inventory inventory = new Inventory();
            inventory.AddItem(item1);
            inventory.AddItem(item2);
            inventory.AddItem(item3);
            inventory.DisplayInventoryItemNames();
            inventory.FindItemByName("장검");
            inventory.RemoveItemByName("장검");
            inventory.DisplayInventoryItemNames();
            inventory.FindIndexOfItemByName("활");
 
 
        }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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
class Inventory
    {
        public Item[] items = new Item[10];
        int itemIndex = 0;
        public Inventory()
        {
 
        }
        public void AddItem(Item item)        
        {
            items[itemIndex] = item;
            itemIndex++;
        }
        public Item FindItemByName(string name)
        {
            foreach(Item item in items)
            {
                if (item != null && item.name == name)
                {
                    Console.WriteLine(item.name+"을 찾았습니다.");
                    return item;
                }
            }
            Console.WriteLine("아이템을 찾을 수 없습니다.");
            return null;
        }
        public void RemoveItemByName(string name)
        {
            for (int i = 0; i<items.Length; i++)
            {
                if (items[i] != null && items[i].name == name)
                {
                    items[i] = null;
                    Console.WriteLine($"인벤토리에서 {name}을 삭제했습니다.");
                    return;
                }
            }
            Console.WriteLine("아이템을 찾을 수 없습니다.");
        }
        public void DisplayInventoryItemNames()
        {
            string name=null;
            Console.WriteLine("보유목록: ");
            for (int i = 0; i<items.Length; i++)
            {
                if (items[i] != null)
                {
                    name = items[i].name;                    
                    Console.WriteLine(items[i].name);
                }
            }
            if (name==null)
            {
                Console.WriteLine("아이템이 없습니다.");
            }
        }
        public int FindIndexOfItemByName(string name)
        {                        
            foreach (Item item in items)
            {
                if(item!=null && item.name==name)
                {                    
                    Console.WriteLine($"{name}의 아이디는 {item.id}입니다.");
                    return item.id;
                }
            }                        
            Console.WriteLine("해당 아이템을 찾을 수 없습니다.");
            return 0;            
        }
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
class Item
    {
        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:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

: