<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기

C#/수업내용 2019. 4. 3. 10:58


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    class App
    {
        public void Start()
        {
            Console.WriteLine("시작");
 
            string searchItemName = "";
            var inventory = new Inventory();
            inventory.AddItem(new Item("피바라기"));
            inventory.AddItem(new Item("무한의 대검"));
            inventory.AddItem(new Item("피바라기"));
            inventory.DisplayItem();
            Console.WriteLine();
            
            searchItemName = "란두인의 예언";
            Item foundItem = inventory.FindItem("란두인의 예언");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
 
            Console.WriteLine();
            inventory.UpdateItem(ref foundItem, "피바라기");
            Console.WriteLine();
 
            searchItemName = "피바라기";
            foundItem = inventory.FindItem("피바라기");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
            Console.WriteLine();
            
            inventory.UpdateItem(ref foundItem, "워모그의 갑옷");
            Console.WriteLine();
 
            foundItem=inventory.FindItem("무한의 대검");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
            Console.WriteLine();
 
            inventory.RemoveItem("루덴의 메아리");
            Console.WriteLine();
            inventory.RemoveItem("피바라기");
            Console.WriteLine();
 
            inventory.DisplayItem();
        }
    }
}
 
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
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    public class Inventory
    {
        public int inventoryValue = 0;
        public int inventoryMaxValue = 5;
 
        Item[] items = new Item[5];
 
        public Inventory()
        {
        }
 
        public void AddItem(Item item)
        {
            for(int i = 0; i<inventoryMaxValue;i++)
            {
                if(items[i]==null)
                {
                    items[i] = item;
                    inventoryValue++;
                    break;
                }
            }
        }
 
        public void RemoveItem(string name)
        {
            Item foundItem = null;
            int i;
            for (i = 0; i < inventoryMaxValue; i++)
            {
                if (items[i] == null)
                {
 
                }
                else if (items[i].name == name)
                {
                    foundItem = items[i];
                    break;
                }
                i++;
            }
            if (foundItem == null)
            {
                Console.WriteLine("존재하지 않는 아이템은 제거할 수 없습니다.");
            }
            else
            {
                Console.WriteLine("{0}을 제거했습니다.", items[i].name);
                items[i] = null;
                inventoryValue--;
            }
        }
 
        public Item FindItem(string name)
        {
            Item foundItem = null;
            for (int i = 0; i < inventoryMaxValue; i++)
            {
                if (items[i] == null)
                {
 
                }
                else if (items[i].name == name)
                {
                    foundItem = items[i];
                    break;
                }
            }
            if (foundItem == null)
            {
                return null;
            }
            else
            {
                return foundItem;
            }
        }
 
        public void UpdateItem(ref Item item, string name)
        {
            if (item == null)
            {
                Console.WriteLine("업데이트에 실패했습니다.");
            }
            else if(item !=null)
            {
                Console.WriteLine("아이템의 이름을 \"{0}\"에서 \"{1}\"으로 수정했습니다.", item.name, name);
                item.name = name;
            }
        }
 
        public void DisplayItem()
        {
            Item temp;
            for (int i = 0; i < inventoryMaxValue - 1; i++)
            {
                if (items[i] == null)
                {
                    temp = items[i];
                    items[i] = items[i + 1];
                    items[i + 1= temp;
                }
            }
 
            Console.WriteLine("{0}개의 아이템이 있습니다.", inventoryValue);
            int j = 0;
            foreach (var item in items)
            {
                if (items[j] != null)
                {
                    Console.WriteLine("-> {0}", items[j].name);
                    j++;
                }
                else if (items[j] == null)
                {
                    j++;
                }
            }
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Start();
 
            Console.ReadKey();
        }
    }
}
 
cs


: