인벤토리

2020. 4. 14. 18:26C#/과제

1) 인벤토리 정리 - CleanUp() 메소드

2) 인벤토리에서 아이템 추출 - GetItem() 메소드

 

<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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    class Inventory
    {
        public Item[] items;        // 인벤토리가 관리하는 아이템들
        int capacity;
        int itemIdx;
        public Inventory(int capacity)
        {
            this.capacity = capacity;
            items = new Item[capacity];
            itemIdx = 0;
            Console.WriteLine("인벤토리가 생성되었습니다.");
        }
        public void AddItem(Item item)
        {
            if (itemIdx < this.capacity)
            {
                this.items[this.itemIdx++= item;
                Console.WriteLine($"인벤토리에 {item.name}을 넣었습니다. ");
            }
            else
                Console.WriteLine("인벤토리가 가득 찼습니다. ");
        }
        public void PrintItemNames()
        {            
            foreach (Item item in this.items)
            {
                if (item == null)
                {
                    Console.WriteLine("Null");
                }
                else
                {
                    Console.WriteLine($"{item.name}");
                }
            }
        }
        public Item FindItem(string itemName)
        {
            foreach (Item item in this.items)
            {
                if (item != null)
                {
                    if (item.name == itemName)
                    {
                        Console.WriteLine($"{itemName}을 찾았습니다. ");
                        return item;            // 아이템을 찾은 경우
                    }
                }
            }
            Console.WriteLine($"{itemName}을 찾지 못했습니다. ");
            return null;                        // 아이템을 찾지 못한 경우
        }
        public Item GetItem(string itemName)
        {
            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i] != null)
                {
                    if (this.items[i].name == itemName)
                    {
                        Console.WriteLine($"{itemName}을 찾았습니다. ");  // 아이템을 찾은 경우
                        Item getItem = new Item(this.items[i].name);
                        this.items[i] = null;
                        return getItem;
                    }
                }
            }
            Console.WriteLine($"{itemName}을 찾지 못했습니다. ");
            return null;                        // 아이템을 찾지 못한 경우            
        }
        public void CleanUp()
        {
            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i] == null)
                {
                    if (i + 1 < this.items.Length)
                    {
                        this.items[i] = this.items[i + 1];
                        this.items[i + 1= null;
                    }
                }
            }
        }
    }
}
 

 

<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;
using System.Threading.Tasks;
 
namespace Study_009
{
    class Item
    {
        public string name;
 
        public Item(string name)
        {
            this.name = name;
            Console.WriteLine($"{this.name}이(가) 생성되었습니다.");
        }
    }
}
 

 

<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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    class App
    {
        public App()
        {
            Inventory inventory = new Inventory(5);
                        
            inventory.AddItem(new Item("장검"));
            inventory.AddItem(new Item("단검"));
            inventory.AddItem(new Item("도끼"));
            inventory.AddItem(new Item("활"));
            inventory.AddItem(new Item("창"));
            inventory.AddItem(new Item("장검"));
 
            inventory.PrintItemNames();
            Console.WriteLine("");
            Item bow = inventory.GetItem("단검");
            inventory.PrintItemNames();
            inventory.CleanUp();
 
            Console.WriteLine("");
            inventory.PrintItemNames();
        }
    }
}
 

'C# > 과제' 카테고리의 다른 글

객체지향이란  (0) 2020.04.22
레시피 관리 및 요리하기  (0) 2020.04.20
캐릭터간 대전  (0) 2020.04.10
2020-04-03  (0) 2020.04.03
2020-04-02 과제  (0) 2020.04.02