Array를 이용하여 Inventory 만들기

C#/수업내용 2019. 4. 1. 14:46

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class App
    {
        public App()
        {
            this.Start();
        }
        public void Start()
        {
            Inventory inventory = new Inventory();
            var inputKey = ConsoleKey.D0;
            while (inputKey != ConsoleKey.D4)
            {
                Console.WriteLine("\n1.아이템제작 2.인벤토리 3.아이템검색 4.종료하기");
                inputKey = Console.ReadKey().Key;
                string inputItemName;
                switch(inputKey)
                {
                    case ConsoleKey.D1:
                        Console.Write("제작하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.AddItem(new Item(inputItemName));
                        break;
                    case ConsoleKey.D2:
                        inventory.GetItemList();
                        break;
                    case ConsoleKey.D3:
                        Console.Write("검색하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.GetItemByName(inputItemName);
                        break;
                    case ConsoleKey.D4:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
 
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 ArrayInventory
{
    public class Item
    {
        public string name;
 
        public Item(string name) //매개변수로  이름을 받아 멤버변수에 저장함
        {
            this.name = name;
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class Inventory
    {
        //Item형식의 값을 저장할 공간 
        private Item[] items = new Item[10];
 
        //기본 생성자 
        public Inventory()
        {
            Console.WriteLine("Inventory클래스의 생성자");
            Console.WriteLine("items.Length: {0}", items.Length);
        }
 
        //넣고 (매개변수로 Item인스턴스를 받아 배열에 넣는다)
        public void AddItem(Item item)
        {
            //배열의 방에 현재 위치에 있는지 확인후 없으면 다음방에 넣는다.
            //Item, Item, null, null,null, null,null, null,null, null
            //Item, Item, Item, Item,Item, Item,Item, Item,Item, Item
            //더이상 넣을수 없습니다.
            for(int index = 0; index < items.Length; index++)
            {
                if(this.GetItemCount() == 10)
                {
                    Console.WriteLine("인벤토리가 꽉찼습니다.");
                    break;
                }
                if(items[index] == null)
                {
                    items[index] = item;
                    Console.WriteLine("{0}의 제작이 완료되었습니다.", item.name);
                    break;
                }
            }
            
            
        }
 
        //뺀다 
        public void GetItemByName(string searchName)
        {
            //배열안에 있는 요소들중에 serachName 문자열과 비교해서 같은거 찾음
            //찾은거 반환, 못찾으면? null반환 
            //같은게 있다면? 하나만 가져오자 
            //돌도끼, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //반환값 : 돌도끼
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, 장검,null, null,null, null,null, null
            //돌도끼, 장검, null, null, null, ,null, null,null, null,null, null
            //손도끼, 돌도끼, null, null,null, null,null, null,null, null
            int index = 0;
            Item temp;
            foreach(Item checkInventory in items)
            {
                if(checkInventory.name == searchName)
                {
                    Console.WriteLine("{0}가 인벤토리에 있습니다. 버리시겠습니까?(1.버린다 2.넣어둔다)", searchName);
                    var readKey = Console.ReadKey().Key;
                    if(readKey == ConsoleKey.D1)
                    {
                        items[index] = null;
                        if(items.Length != 0)
                        {
                            for (int moveIndex = 0; moveIndex < items.Length - 1; moveIndex++)
                            {
                                if (items[moveIndex] == null)
                                {
                                    temp = items[moveIndex + 1];
                                    items[moveIndex + 1= items[moveIndex];
                                    items[moveIndex] = temp;
                                }
                            }
                            Console.WriteLine("{0}이 삭제되고 인벤토리가 정렬되었습니다.", searchName);
                        }
                        Console.WriteLine("{0}이 삭제되었습니다.", searchName);
                        break;
                    }
                }
                index++;
            }
        }
        
        //목록출력
        public void GetItemList()
        {
            foreach(Item itemList in items)
            {
                if(itemList == null)
                {
                    break;
                }
                Console.Write("{0}  ", itemList.name);
            }
        }
 
        //인벤토리에 있는 아이템 갯수 가져오기 
        public int GetItemCount()
        {
            //배열안에 있는 값이 null이 아닐경우 카운트 해서 반환함 
            int cnt = 0;
            foreach (var item in items)
            {
                if (item != null)
                {
                    cnt++;
                }
            }
            return cnt;
        }
    }
}
cs


'C# > 수업내용' 카테고리의 다른 글

제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
List를 이용하여 Inventory 만들기  (0) 2019.04.01
공부할것  (0) 2019.03.29
2. for, foreach, while  (0) 2019.03.27
1. Method  (0) 2019.03.24
: