ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2020-04-15 [인벤토리 구축]
    C#/과제 2020. 4. 15. 23:53
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace practice009
    {
        class APP
        {
            public APP()
            {
                Inventory inventory = new Inventory(5); // 새로운 인벤토리를 생성 
     
                inventory.InsertItem(new Item("롱소드"));            /// items[0]
                inventory.InsertItem(new Item("롱보우"));            /// items[1]
                inventory.InsertItem(new Item("클레이모어"));        /// items[2]
                Item axe = inventory.InsertItem(new Item("도끼"));   /// items[3]
                inventory.InsertItem(new Item("너클"));              /// items[4]
     
                inventory.Getitem(axe);
                inventory.PrintItemlist();
     
            }
        }
    }
    ==================================================================================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace practice009
    {
        class Item
        {
            public string name;
            public string founditem;
            public string getitem;
            public Item(string name)
            {
                this.name = name;
                //Console.WriteLine("{0} 생성", this.name);
            }
     
     
        }
    }
    =================================================================================
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace practice009
    {
        class Inventory
        {
     
            public Item[] items;
            public int itemidx;
     
            public Inventory(int capacity)
            {
                //아이템 배열 생성 필요 
                items = new Item[capacity]; // 입력 값만큼 배열값 생성 
                Console.WriteLine("인벤토리 생성 완료");
            }
     
            public Item InsertItem(Item item)
            {
                items[itemidx++= item;
                Console.WriteLine("아이템 {0} 넣음"item.name);
                return item;
            }
     
          
     
            public void PrintItemlist()
            {
                //for (int i = 0; i < this.items.Length; i++)
                //{
                //    Console.WriteLine(this.items[i].name);   
                //}
     
                foreach (Item item in items)
                {
                    Console.WriteLine(item.name);
                }
      
            }
     
            public Item FindItem(string name)
            {
                Item founditem = null;
                foreach (Item item in items)
                {
                    if (item.name == name)
                    {
                        Console.WriteLine("찾는것 : {0}  / 인벤토리 안 : {1} 발견"item.name, name);
                    }
                    else
                    {
                        Console.WriteLine("찾는것 : {0} / 인벤토리 안 : {1} 발견못함"item.name, name);
                    }
     
                }
                return founditem;
     
                //for (int i = 0; i < items.Length; i++)
                //{
     
                //    if (items[i].name == name)
                //    {
                //        Console.WriteLine("찾는것 : {0}  , 인벤토리 안 : {1} 발견", items[i].name, name);
                //    }
                //    else if (items[i].name != name)
                //    {
                //        Console.WriteLine("찾는것 : {0} / 인벤토리 안 : {1} 발견못함", items[i].name, name);
                //    }
     
                //}
     
     
                //Console.WriteLine("반환값 {0}" , Array.IndexOf(items, name, itemidx));
                //Console.WriteLine("indexOf 메소드: {0}", Array.IndexOf(reading, 12));
                // Console.WriteLine("반환값 {0}", Array.IndexOf(items, name));
            }
     
     
     
            public Item Getitem(Item item)
            {
           
                for (int i = 0; i < items.Length; i++)
                {
     
                    if (items[i].name == item.name)
                    {
                        Console.WriteLine("찾는것 : {0}  , 인벤토리 안 : {1} 발견", items[i].name, item.name);
                        items[i].name = "[    ]";
                    }
     
                }
     
                return item;
            }
     
        
     
     
            //특정 배열 요소를 삭제한다면 null 이 되면 될거 같음
            // finditem에서 찾을걸 삭제하면 ? 특정값 삭제하면 될거같음 
            // 우선  null 이 되면 0 1 2 3 4 중 뒤 값과 비교해서 null 이라면 교환 형식으로다가 
     
            // 넣었던 아이템이 없어져야 함 
            // 근데 null 되면 계쏙 출력 안됨 
            // 널리퍼런스, 런에 엑세스 하려할떄 나는건데... 
     
     
            // for 돌려서 null 뜨는걸 뒤로 밀기?
        }
    }
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

     

     

     

     

     

     

     * 특정 배열 값이 null이 되면 오류가 뜨면서 안되서 방법을 못찾겠음 

    이부분은 추후 확인시 댓글로 후술 

     

    'C# > 과제' 카테고리의 다른 글

    객체지향 3요소 / 5 원칙 둘러보고 간단 서술  (0) 2020.04.23
    2020-04-18/19 [1차원 배열 활용]  (0) 2020.04.20
    2020-04-14  (1) 2020.04.14
    연습 [2020-04-12]  (2) 2020.04.12
    2020-04-10 [할것] _ 기본 폼 완성  (1) 2020.04.10
Designed by Tistory.