제너릭 클래스, 상속을 이용하여 Inventory 만들기

C#/수업내용 2019. 4. 1. 15:37


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    public class HerbItem : Item
    {
        public HerbItem(string name)
        {
            this.name = name;
        }
    }
}
 

cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    public class MagicalItem : Item
    {
        public MagicalItem(string name)
        {
            this.name = name;
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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()
        {
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class Inventory<T>
    {
        //Item형식의 값을 저장할 공간 
        private List<Item> items = new List<Item>();
        public int inventoryCount = 10;
 
        //기본 생성자 
        public Inventory()
        {
            Console.WriteLine("Inventory클래스의 생성자");
            Console.WriteLine("items.Length: {0}", items.Count);
        }
 
        //넣고 (매개변수로 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
            //더이상 넣을수 없습니다.
            if (this.GetItemCount() == inventoryCount)
            {
                Console.WriteLine("인벤토리가 꽉찼습니다.");
            }
            else
            {
                items.Add(item);
                Console.WriteLine("{0}의 제작이 완료되었습니다.", item.name);
            }
 
 
        }
 
        //뺀다 
        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;
            var readKey = ConsoleKey.D0;
            foreach (Item checkInventory in items)
            {
                if (checkInventory.name == searchName)
                {
                    Console.WriteLine("{0}가 인벤토리에 있습니다. 버리시겠습니까?(1.버린다 2.넣어둔다)", searchName);
                    readKey = Console.ReadKey().Key;
                    break;
                }
                index++;
            }
            if (readKey == ConsoleKey.D1)
            {
                items.RemoveAt(index);
                Console.WriteLine("{0}을 버렸습니다.", searchName);
            }
        }
 
        //목록출력
        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
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
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<HerbItem> herbInventory = new Inventory<HerbItem>(); //허브인벤토리
            Inventory<MagicalItem> magicInventory = new Inventory<MagicalItem>(); //마부인벤토리
 
            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.WriteLine("제작하실 아이템 종류와 품목을 선택하세요");
                        Console.WriteLine("약초 : 평온초 / 은엽수 / 뱀뿌리 / 마법초 / 무덤이끼");
                        Console.WriteLine("마부 : 가속의 서약 / 원소의 격류 / 치명적인 항해");
                        inputItemName = Console.ReadLine();
                        switch(inputItemName)
                        {
                            case "평온초":
                                herbInventory.AddItem(new HerbItem(inputItemName));
                                break;
                            case "은엽수":
                                herbInventory.AddItem(new HerbItem(inputItemName));
                                break;
                            case "뱀뿌리":
                                herbInventory.AddItem(new HerbItem(inputItemName));
                                break;
                            case "마법초":
                                herbInventory.AddItem(new HerbItem(inputItemName));
                                break;
                            case "무덤이끼":
                                herbInventory.AddItem(new HerbItem(inputItemName));
                                break;
                            case "가속의 서약":
                                magicInventory.AddItem(new MagicalItem(inputItemName));
                                break;
                            case "원소의 격류":
                                magicInventory.AddItem(new MagicalItem(inputItemName));
                                break;
                            case "치명적인 항해":
                                magicInventory.AddItem(new MagicalItem(inputItemName));
                                break;
                            default:
                                Console.WriteLine("제작할 수 없는 아이템입니다.");
                                break;
                        }
                        break;
                    case ConsoleKey.D2:
                        Console.Write("약초 가방 : ");
                        herbInventory.GetItemList();
                        Console.Write("\n마부 가방 : ");
                        magicInventory.GetItemList();
                        break;
                    case ConsoleKey.D3:
                        Console.Write("검색하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        herbInventory.GetItemByName(inputItemName);
                        magicInventory.GetItemByName(inputItemName);
                        break;
                    case ConsoleKey.D4:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
 
cs


: