IEnumerator, IEnumerable 상속받은 Inventory 구현 (인덱서)

Unity3D/C# 2019. 4. 1. 23:11
반응형

아이템 추가 하기

출력하기

꺼내기 

foreach가능 하게 만들기 

인덱서 구현 하기 

null값 제거하고 순서 정렬하기 

배열길이 자동증가 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
namespace ConsoleApp9
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new App();
            app.Start();
        }
    }
}
 
 
 

 

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
using System;
using System.Collections.Generic;
 
namespace ConsoleApp9
{
    class App
    {
        public void Start()
        {
            var inventory = new Inventory();
            inventory[0= new Item("도끼");
            inventory.AddItem(new Item("장검"));
            inventory.AddItem(new Item("단검"));
            inventory[6= new Item("가죽옷");
 
            inventory.DisplayItems();
 
            var searchName = "단검";
            var foundItem = inventory.GetItem(searchName);
            if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", foundItem.itemName);
            }
            else
            {
                Console.WriteLine("{0}은 찾을수 없습니다.", searchName);
            }
            inventory.Arrange();
 
            inventory.DisplayItems();
 
            inventory.AddItem(new Item("단검"));
 
            Console.WriteLine();
            Console.WriteLine();
            inventory.DisplayItems();
 
            inventory[9= new Item("판금갑옷");
            Console.WriteLine();
            Console.WriteLine();
            inventory.Arrange();
            inventory.DisplayItems();
 
 
            inventory.AddItem(new Item("초급용 지팡이"));
            Console.WriteLine();
            Console.WriteLine();
            inventory.DisplayItems();
 
            Console.WriteLine();
            Console.WriteLine();
            foreach (Item item in inventory)
            {
                if (item != null)
                {
                    Console.WriteLine(item.itemName);
                }
               
            }
 
            Console.ReadKey();
        }
    }
}
 
 
 

 

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
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace ConsoleApp9
{
    public class Inventory : IEnumerator, IEnumerable
    {
 
        private Item[] items = new Item[5];
        private int nextIndex = 0;
        private int position = -1;
 
        public Item this[int index]
        {
            get
            {
                nextIndex = index;
                    
                return this.items[nextIndex];
            }
            set
            {
                nextIndex = index;
                if (this.nextIndex > this.items.Length)
                {
                    Array.Resize<Item>(ref this.items, this.items.Length * 2);
                }
                this.items[nextIndex++= value;
            }
        }
 
        public int Length
        {
            get
            {
                return this.items.Length;
            }
        }
 
        public Item GetItem(string searchName)
        {
            Item foundItem = null;
            for (int i = 0; i < this.items.Length; i++)
            {
                var item = this.items[i];
                if (item != null)
                {
                    if (item.itemName == searchName)
                    {
                        foundItem = item;
                        this.items[i] = null;
                        break;
                    }
                }
            }
            return foundItem;
        }
 
        public void AddItem(Item item)
        {
            if (this.nextIndex > this.items.Length)
            {
                Array.Resize<Item>(ref this.items, this.items.Length * 2);
            }
 
            this.items[this.nextIndex++= item;
        }
 
 
        #region 인터페이스 구현 시작 
        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)this;
        }
 
        public bool MoveNext()
        {
            this.position++;
            return (this.position < this.items.Length);
        }
 
        public void Reset()
        {
            this.position = 0;
        }
 
        public object Current
        {
            get
            {
                return this.items[this.position];
            }
        }
        #endregion 인터페이스 구현 끝 
 
        public void DisplayItems()
        {
            foreach (Item item in this.items)
            {
                if (item == null)
                {
                    Console.WriteLine("---> {0}", item);
                }
                else
                {
                    Console.WriteLine("---> {0}, {1}", item, item.itemName);
                }
            }
        }
 
        public void Arrange()
        {
            Item[] arrTemp = new Item[this.items.Length];
            int tempIdx = 0;
 
            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i] != null)
                {
                    arrTemp[tempIdx++= this.items[i];
                }
            }
            this.nextIndex = tempIdx;
            this.items = arrTemp;
        }
    }
}
 
 
 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
 
namespace ConsoleApp9
{
    public class Item
    {
        public string itemName;
 
        public Item(string itemName)
        {
            this.itemName = itemName;
        }
    }
}
 
 
 

 

반응형

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

인벤토리 만들기 (제너릭)  (0) 2019.04.02
referencesource microsoft  (0) 2019.04.02
C#. 쓰레드(Thread)  (0) 2019.04.01
컬렉션(C#)  (0) 2019.03.28
2019.03.26 과제 (와우 케릭터 만들기 C#콘솔)  (0) 2019.03.27
: