상세 컨텐츠

본문 제목

Study_012

C#/수업내용

by McRobbin 2020. 4. 20. 18:22

본문

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    public class App
    {
 
        public App()
        {
            Console.WriteLine("2020-04-20\n================");
 
 
 
            /*
            //배열 예제.
            int[] priceArray = new int[3];
            priceArray[0] = 3000;
            priceArray[1] = 2400;
            priceArray[priceArray.Length - 1] = 3200;
 
            string[] productNameArray = new string[3];
            productNameArray[0] = "칙촉";
            productNameArray[1] = "빈츠";
            productNameArray[productNameArray.Length - 1] = "초코파이";
 
            Product[] productArray = new Product[3];
            for (int i = 0; i < priceArray.Length; i++)
                productArray[i] = new Product(new ProductData(productNameArray[i], priceArray[i]));
 
            Console.WriteLine("\nfor문 이용 물건 출력.");
            for (int i = 0; i < productArray.Length; i++)
                productArray[i].PrintInfo();
 
            Console.WriteLine("\nforeach문 이용 물건 출력.");
            foreach (Product product in productArray)
                product.PrintInfo();
            */
 
 
 
 
 
            /*
            //캐릭터 배열 예제.
            CharacterData hongData = new CharacterData("홍길동", 100, 10);
            Character hong = new Character(hongData);
            hong.PrintInfo();
 
            Character lim = new Character(new CharacterData("임꺽정", 120, 8));
            lim.PrintInfo();
 
            WeaponData sword = new WeaponData("장검", 20);
            Weapon hongWeapon = new Weapon(sword);
 
            WeaponData axe = new WeaponData("도끼", 13);
            Weapon limWeapon = new Weapon(axe);
 
            hong.PutWeapon(hongWeapon);
            lim.PutWeapon(limWeapon);
 
            hong.PrintWeaponsInfo();
            lim.PrintWeaponsInfo();
 
            WeaponData ballData = new WeaponData("철퇴", 15);
            Weapon ball = new Weapon(ballData);
 
            lim.PutWeapon(ball);
            lim.PrintWeaponsInfo();
 
            WeaponData bowData = new WeaponData("활", 10);
            Weapon bow = new Weapon(bowData);
            lim.PutWeapon(bow);
            lim.PrintWeaponsInfo();
 
            Weapon[] limWeapons = lim.GetAllWeapons();
            for(int i = 0; i < limWeapons.Length; i++)
            {
                limWeapons[i].PrintInfo();
            }
 
            lim.GetWeapon("장검");
            lim.GetWeapon("철퇴");
 
            lim.PrintEquipedWeaponInfo();
            lim.EquipWeapon(lim.GetWeapon("철퇴"));
            lim.PrintWeaponsInfo();
            lim.PrintEquipedWeaponInfo();
 
            lim.EquipWeapon(lim.GetWeapon("활"));
            lim.PrintWeaponsInfo();
            lim.PrintEquipedWeaponInfo();
 
            WeaponData daggerData = new WeaponData("단검", 5);
            Weapon dagger = new Weapon(daggerData);
 
            WeaponData shieldData = new WeaponData("방패", 2);
            Weapon shield = new Weapon(shieldData);
 
            Weapon[] hongWeaponArray = { dagger, shield};
            hong.PutWeapons(hongWeaponArray);
            hong.PrintWeaponsInfo();
 
            hong.EquipWeapon("방패");
            hong.PrintEquipedWeaponInfo();
            hong.PrintWeaponsInfo();
 
 
            hongWeaponArray = new Weapon[4];
            hongWeaponArray[0] = new Weapon(new WeaponData("목검", 4));
            hongWeaponArray[1] = new Weapon(new WeaponData("뾰족한 단검", 9));
            hongWeaponArray[2] = new Weapon(new WeaponData("지팡이", 6));
            hongWeaponArray[3] = new Weapon(new WeaponData("완드", 2));
 
            hong.PutWeapons(hongWeaponArray);
            hong.PrintWeaponsInfo();
            */
 
 
 
 
 
 
 
            //리스트 예제.
            Inventory inventory = new Inventory();
            /*
            List<string> list = new List<string>();
            list.Add("hong");
            list.Add("nam");
            list.Add("lim");
 
            list.Find(x => x == "hong");
            list.Remove("lim");
            */
 
            Inventory roadInventory = new Inventory();
            Inventory playerInventory = new Inventory();
 
            roadInventory.AddItem(new Item("장검"10));
            roadInventory.AddItem(new Item("단검"3));
            roadInventory.AddItem(new Item("활"5));
            roadInventory.AddItem(new Item("철퇴"12));
            roadInventory.AddItem(new Item("도끼"20));
 
            while (true)
            {
                roadInventory.PrintItemListForRoad();
                inventory.PrintItemList();
                Console.Write("아이템 획득하려면 명령어를 입력해주세요. ");
                string[] input = Console.ReadLine().Split(' ');
 
                //입력 잘못됐을 때.
                if (input.Length > 2)
                {
                    Console.WriteLine("명령어가 잘못 됐습니다.");
                    continue;
                }
 
                //입력 길이 맞을 때.
                else
                {
                    string itemName = input[0];
                    int itemCount = (input.Length < 2)? 1 : int.Parse(input[1]);
 
                    Item getItem = roadInventory.GetItemByName(itemName, itemCount);
 
                    //없는 아이템일 때.
                    if(getItem == null)
                    {
                        Console.WriteLine("없는 아이템 입니다.");
                        continue;
                    }
 
                    else
                    {
                        inventory.AddItem(getItem);
                        Console.WriteLine("{0} 아이템을 {1} 개 획득했습니다."getItem.Name, getItem.Amount);
                    }
                }
            }
 
        }
    }
}
 
 
 

 

아이템 획득 인벤토리 App 생성자 내용.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    public class Item
    {
        public int Amount { get; set; }
        public string Name { get; }
 
        public Item(string name, int amount)
        {
            this.Name = name;
            this.Amount = amount;
        }
 
        public void PrintItem()
        {
            Console.WriteLine("이름 : {0}, 갯수 : {1}"this.Name, this.Amount);
        }
    }
}
 
 
 

 

Item클래스 내용.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    public class Inventory
    {
        public List<Item> ItemList { get; }
        
        public Inventory()
        {
            this.ItemList = new List<Item>();
        }
 
        //아이템 추가.
        public void AddItem(Item item)
        {
            int index = this.ItemList.FindIndex(x => x.Name == item.Name);
 
            if (index != -1)
                this.ItemList[index].Amount += item.Amount;
            else
                this.ItemList.Add(item);
 
        }
 
        //아이템 이름 받아서 한개 가져오기.
        public Item GetItemByName(string name)
        {
            return this.GetItemByName(name, 1);
        }
 
        //아이템 이름 수량만큼 가져오기.
        public Item GetItemByName(string name, int amount)
        {
            Item found = this.ItemList.Find(x => x.Name == name);
 
            //아이템 없을 때.
            if (found == null)
                return null;
 
            //아이템 갯수가 모자를때.
            else if(found.Amount <= amount)
            {
                this.ItemList.Remove(found);
                return new Item(name, found.Amount);
            }
 
            //아이템 갯수 충분할때.
            else
            {
                found.Amount -= amount;
                return new Item(name, amount);
            }
        }
 
        //아이템 리스트 전부 출력.
        public void PrintItemList()
        {
            Console.WriteLine("\n소지중인 아이템=====");
 
            int index = 0;
            foreach(Item item in this.ItemList)
            {
                if (item == null)
                    Console.WriteLine("{0} : [    ]", index++);
                else
                    Console.WriteLine("{0} : [{1} X {2}]", index++item.Name, item.Amount);
            }
 
            Console.WriteLine();
        }
 
        public void PrintItemListForRoad()
        {
            Console.WriteLine("\n길에 떨어진 아이템=====");
 
            int index = 0;
            foreach (Item item in this.ItemList)
            {
                if (item == null)
                    Console.WriteLine("{0} : [    ]", index++);
                else
                    Console.WriteLine("{0} : [{1} X {2}]", index++item.Name, item.Amount);
            }
 
            Console.WriteLine();
        }
    }
}
 
 
 

 

Inventory 클래스 내용 입니다. Find, Remove 함수 사용했습니다.

특이한 점은 GetItemByName(string name)의 경우 1개의 아이템을 제거하는데,

밑에 정의된 GetItemByName(string name, int amount)를 그대로 불러와 사용합니다.

==> GetItemByName(name, 1)을 그대로 사용하면 되겠습니다.

 

실행 결과 입니다.

관련글 더보기