• 출력 결과

 

 

 

  • Main
using itemlist;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {

            Inventory inven = new Inventory();
            inven.AddItem("장검");
            inven.AddItem("단검");
            inven.AddItem("활");


            inven.ShowAllItems();
            ////1. 장검
            ////2. 단검
            ////3. 활
            ////4. [비어있음]
            ////5. [비어있음]
            ///
          
            Console.WriteLine("FindItem 실행");
            Item item = inven.FindItem("단검");
            Console.WriteLine($"=>찾았음: {item.ItemName}");

            int cnt = inven.Count();

            Console.WriteLine(); 
            Console.WriteLine($"▶등록된 아이템 수량: {cnt}"); 
            Console.WriteLine(); 

            Console.WriteLine("RemoveItemByName 실행");
            inven.RemoveItemByName("단검"); // 리스트 중 장검 찾아 삭제하기 // 값은 찾아서 삭제했는데 // 인덱스가 남음


           Console.WriteLine();
           Console.WriteLine();
            Console.WriteLine("삭제 확인을 위한 ShowAllItems 실행");
            inven.ShowAllItems(); //...???
            //1. 단검
            //2. 활
            cnt = inven.Count();
            Console.WriteLine($"▶등록된 아이템 수량: {cnt}"); // 장검 삭제해서 2
        }
    }
}

 

 

  • Item
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace itemlist
{
    public class Item
    {
        //멤버 변수
        public string ItemName;


        //생성자
        public Item(string ItemName)
        {
            this.ItemName = ItemName;
        }
    }
}

 

  • Inventory 24.08.15

1. 인덱스 값 출력 실현
2. 확인 코드들 추가

문제> 선택한 인덱스 값과 인덱스 자체를 지워야 하는데...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace itemlist
{

    public class Inventory
    {

        string[] itemNames;
        public int cnt = 5;
        public int idx = 0;
       


        //생성자
        public Inventory()
        {
            // Console.WriteLine("생성자 확인");
            itemNames = new string[cnt];
        }



        // 아이템 추가
        public void AddItem(string itemName)
        {

            itemNames[idx] = itemName;
            idx++;
         
            // Console.WriteLine("인덱스 기능 확인용 애드 인덱스"+idx);
            //for (int i = 0; i < itemNames.Length; i++)
            //{
            //    if (itemNames[i] == null)
            //    {
            //        itemNames[i] = itemName;
            //        break;
            //    }
            //}
        }



        //배열 리스트 값 모두 출력
        public void ShowAllItems()
        {
    
            Console.WriteLine("========보유 중인 아이템 리스트========");
            for (int i = 0; i < cnt; i++)
            {
                if (itemNames[i] != null)
                {
                    Console.WriteLine($"{i + 1}.{itemNames[i]}");
                   
                }
                else
                {
                    Console.WriteLine($"{i + 1}. [비어있음]");
                    continue;
                }
            }
            Console.WriteLine("========================================");
            Console.WriteLine();
        }



        public Item FindItem(string itemName)
        {
            Console.WriteLine($"찾는 아이템 이름: {itemName}");

            Item item = new Item(itemName);
            int a = 0;

            for (int i = 0; i < itemNames.Length; i++)
            {
                if (itemNames[i] == item.ItemName)
                {
                    item.ItemName = itemNames[i];
                    Console.WriteLine($"해당 아이템 idx:{a}");
                }
                a++;
            }
            return item;
        }


        //리스트 중 null이 아닌 요소를 반환
        public int Count()
        {
            int Count = 0;
         
            for (int i = 0; i < itemNames.Length; i++)
            {
                if (itemNames[i] != null)
                {
                    Count++;
                }
            }
            return Count;
        }



        //리스트 중 인자와 같은 이름의 아이템이 있으면 인덱스와 함께 삭제
        public void RemoveItemByName(string itemName)
        {

            Console.WriteLine($"=>> 삭제할 아이템 이름:{itemName}");

            for (int i = 0; i < cnt; i++)
            {

                if (itemNames[i] != null) // 아이템 값이 없으면 총 길이인 삭제해
                    // 해당 인덱스도 삭제해

                if (itemName == itemNames[i] )
                {
                    idx = i;
                    Console.WriteLine($"해당 아이템 idx:{idx}");
                        itemNames[i] = null;
                        idx--;
                }
            }
        }
        }
    }

 

 

🔽  ver.1) 

더보기
  • Main
using itemlist;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {

            Inventory inven = new Inventory();
            inven.AddItem("장검");
            inven.AddItem("단검");
            inven.AddItem("활");


            inven.ShowAllItems();
            ////1. 장검
            ////2. 단검
            ////3. 활
            ////4. [비어있음]
            ////5. [비어있음]

            Item item = inven.FindItem("장검");
            Console.WriteLine(item.ItemName);

            int cnt = inven.Count();
            Console.WriteLine($"▶등록된 아이템 수량: {cnt}"); 

            inven.RemoveItemByName("장검"); // 리스트 중 장검이 있으면 삭제

            cnt = inven.Count();
            Console.WriteLine($"▶등록된 아이템 수량: {cnt}"); // 장검 삭제해서 2

            inven.ShowAllItems(); //...???
            //1. 단검
            //2. 활

        }
    }
}

 

  • Inventory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace itemlist
{

    public class Inventory
    {

        string[] itemNames;
        int cnt = 5;


        //생성자
        public Inventory()
        {
            // Console.WriteLine("생성자 확인");
            itemNames = new string[cnt];
        }



        // 아이템 추가
        public void AddItem(string itemName)
        {

            for (int i = 0; i < itemNames.Length; i++)
            {
                if (itemNames[i] == null)
                {
                    itemNames[i] = itemName;
                    break;
                }
            }
        }



        //배열 리스트 값 모두 출력
        public void ShowAllItems()
        {
            Console.WriteLine();
            Console.WriteLine("========보유 중인 아이템 리스트========");

            for (int i = 0; i < itemNames.Length; i++)
            {
                if (itemNames[i] == null)
                {
                    Console.WriteLine($"{i + 1}. [비어있음]");
                    continue;
                }
                else
                {
                    Console.WriteLine($"{i + 1}.{itemNames[i]}");
                }
            }
            Console.WriteLine("========================================");
            Console.WriteLine();
        }



        public Item FindItem(string itemName)
        {

            Item item = new Item(itemName);

            for (int i = 0; i < itemNames.Length; i++)
            {

                if (itemNames[i] == item.ItemName)
                {
                    item.ItemName = itemNames[i];
                }

            }
            return item;
        }


        //리스트 중 null이 아닌 요소를 반환
        public int Count()
        {
            int Count = 0;
            Console.WriteLine();
            for (int i = 0; i < itemNames.Length; i++)
            {
                if (itemNames[i] != null)
                {
                    Count++;
                }
            }

            return Count;
        }



        //리스트 중 인자와 같은 이름의 아이템이 있으면 삭제
        public void RemoveItemByName(string itemName)
        {
            
            for (int i = 0; i < itemNames.Length; i++)
            {
                if ( itemName == itemNames[i])
                {
                    itemNames[i] = null; 
                    continue;
                   
                }
            }
        }
    }
}

 

 

  • 출력 결과

 

 

1. 중간에 껴있는 "장검"이 "장검"과 같은 배열 값이 있으면 출력해라?

   몇 번째 아이템인지 확인하고 싶은데 따로 번호를 부여하지 않았다.

2. RemoveItemByName 에서 리스트 중 "장검"을 삭제하고 null을 넣었더니 배열 값은 3 >> 2로 변화했지만 

    ShowAllItems로 리스트를 출력할 때 null이 있으면 [비어있음]이라고 바꾸라는 명령이 먹혀 삭제되지 않았다.

    1. 단검 / 2. 활이 되어야 한다.

 

3. 이걸 리스트로 만들어야지!

 

 

 

(수정 끝나면 따로 정리할 거)

...기능을 만들면서 생각하는 게 아니라 어떤 출력 결과가 나올지 인자를 먼저 정하고 매서드와 매개 변수를 만드는 것

...public Item FindItem(string itemName) : 클래스 자체가 사용자 지정 타입이기에 가능

...자동으로 붙길래 return을 써서 작동은 하는데 리턴을 언제, 어떻게 쓰는 건지 아직 잘 모르겠다. 더 많이 써봐야겠다.

 

+ Recent posts