C#/수업내용

2020.04.20. 수업내용 - 아이템 수량만 늘리기(객체 생성 x)

dev_sr 2020. 4. 21. 09:30

 

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_012
{
    class App
    {
        public App()
        {
        
         
 
            Inventory inventory = new Inventory();
 
            
            inventory.Add(new Item("장검"3));
            inventory.Add(new Item("장검"2));
            inventory.Add(new Item("장검"5));
 
        }
    }
}
 

 

 

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
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 Study_012
{
    class Inventory
    {
        public List<Item> itemList;
 
        public Inventory()
        {
            this.itemList = new List<Item>();
        }
 
 
        public void Add(Item item)
        {
            //itemlist에 item.name이 있는지 확인
            //있다면
 
            Item foundItem = null;
 
            foreach(Item element in this.itemList)
            {
                if(element.name==item.name)
                {
                    foundItem = element;
                    break;
                }
            }
 
            if(foundItem!=null)
            {
                foundItem.amount += item.amount;
 
                //Console.WriteLine(foundItem.amount);
 
                foreach (Item items in this.itemList)
                {
                    Console.WriteLine("{0}x{1}"items.name, items.amount);
                }
 
 
 
            }
 
            else
            {
                //같은 이름이 없다면
                this.itemList.Add(item);
            }
           
          
        }
 
        public Item GetItemByName(string name)
        {
            Item foundItem = null;
 
            foreach (Item item in itemList)
            {
                if(item.name==name)
                {
                    foundItem = item;
                    break;
                }
            }
 
 
            //찾은 아이템이 null이 아니라면
            //수량을 1 감소시킨다
            //새로운 아이템을 생성한다.
            //아이템의 이름은 찾은 아이템이다.
            //수량은 1이다.
 
            Item rtnItem = null;
 
            if(foundItem!=null)
            {
                foundItem.amount -= 1;
                new Item(foundItem.name, 1);
            }
          
 
            return rtnItem;
        }
 
 
        public void PrintAllName()
        {
            foreach (Item items in itemList)
            {
                Console.WriteLine("{0}x{1}",items.name,items.amount);
            }
        }
    }
}
 
 

 

 

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
{
    class Item
    {
        public string name;
        public int amount;
 
        public Item(string name, int amount)
        {
            this.name = name;
            this.amount = amount;
        }
    }
}
 

 

 

4. 결과값