C#/과제

2020.04.20. 과제 - 길바닥 아이템 획득하기(컬렉션(list))

dev_sr 2020. 4. 21. 00:02

 

1. App class

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012_1
{
    class App
    {
        public App()
        {
            List<Item> dropItemList = new List<Item>();
            Inventory inventory=new Inventory();
 
            var item1 = new Item("장검"10);
            var item2 = new Item("단검"2);
            var item3 = new Item("활"5);
 
            dropItemList.Add(item1);
            dropItemList.Add(item2);
            dropItemList.Add(item3);
 
            
            int leftAmount=0;
 
 
 
            this.PrintList(dropItemList,inventory);
 
            
            Console.Write("아이템을 획득하려면 명령어를 입력해주세요 ");
            string input = Console.ReadLine();
            string[] strinput=input.Split(' ');
            string name=strinput[0];
            int amount=int.Parse(strinput[1]);
 
 
            Item foundItem=null;
            foundItem=dropItemList.Find(x=>x.name==name);
   
            leftAmount=foundItem.amount-amount;
 
            foreach(Item item in dropItemList)
            {
                if(item.name==name)
                {  
                    item.amount=leftAmount;
                }
            }
 
 
            inventory.getItem(foundItem,amount);
            
            this.PrintList(dropItemList,inventory);
 
        }
 
 
 
        public void PrintList(List<Item> list,Inventory inventory)
        {
            Console.WriteLine("길바닥에 떨어져있는 아이템");
            Console.WriteLine("-----------------------------------");
 
            int i = 0;
 
            foreach (var item in list)
            {
 
 
                if (i >= list.Count - 1)
                {
                    Console.Write("{0}x{1} "item.name, item.amount);
                    Console.WriteLine();
                }
 
                else
                {
                    Console.Write("{0}x{1}, "item.name, item.amount);
                }
 
                i++;
            }
 
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("소지중인 아이템");
            Console.WriteLine("-----------------------------------");
            
            if(inventory.GetItemCount()==0)
            {
                Console.WriteLine("아이템이 없습니다.");
            }
            else
            {
               for(int j=0; j<inventory.GetItemCount(); j++)
                {
                    Console.WriteLine("{0}x{1}",inventory.ItemList[j].name,inventory.ItemList[j].amount);
                }
            }
            
            
        }
 
 
    }
}
 
 

 

 

2. Inventory class

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012_1
{
    class Inventory
    {
        public List<Item> ItemList;
 
        public Inventory()
        {
            this.ItemList = new List<Item>();
        }
 
        public int GetItemCount()
        {
            return ItemList.Count;
        }
 
        public void getItem(Item item, int amount)
        {
            Item newitem = new Item(item.name,amount);
            this.ItemList.Add(newitem);
 
            Console.WriteLine("{0} {1}개를 획득 했습니다.",newitem.name,newitem.amount);
            Console.WriteLine();
        }
 
    }
}
 
 

 

 

3. Item class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012_1
{
    class Item
    {
        public string name;
        public int amount;
 
        public Item(string name, int amount)
        {
            this.name = name;
            this.amount = amount;
        }
    }
}
 
 

 

 

4. 결과값