c#/수업 과제

원격수업 c# 과제(인벤토리)

yeeendy 2023. 1. 25. 21:34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study230125
{
    class App
    {
        //생성자
        public App()
        {
            Inventory inventory = new Inventory(5);
            inventory.AddItem(new Weapon("장검", 12));
            inventory.AddItem(new Weapon("도끼", 14));
            inventory.AddItem(new Weapon("창", 13));
            inventory.AddItem(new Weapon("장검", 12)); //이미 가지고 있다면 추가X
            inventory.AddItem(new Armor("투구", 3));
            inventory.AddItem(new Armor("갑옷", 5));
            inventory.CountItem();
            //inventory.AddItem(new Weapon("너클", 3)); //capacity 를 초과하면 넣을수 없다
            Console.WriteLine();
            inventory.PrintAllItem();
            inventory.CountItem();
            inventory.RemoveItem("장검");
            inventory.CountItem();

            inventory.AddCapacity(3);
            inventory.SearchItem("창");
            inventory.ItemType("Weapon");
            inventory.ItemType("Armor");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study230125
{
    class Inventory
    {
        public int capacity;
        public Item[] items;
        public int index = 0;

        //생성자
        public Inventory(int capacity)
        {
            this.capacity = capacity;
            this.items = new Item[capacity];
            Console.WriteLine("capacity : {0} 인벤토리 생성", items.Length);
        }
        public void AddItem(Item item)
        {
            bool isItem = false;
            foreach(Item item2 in items)
            {
                if(item2 != null)
                {
                    if(item2.Name == item.Name)
                    {
                        Console.WriteLine("이미 {0} 아이템은 가지고 있습니다.", item2.Name);
                        isItem = true;
                        break;
                    }
                }
            }
            if(isItem == false)
            {
                this.items[index] = item;
                this.index++;
                Console.WriteLine("{0}을(를) 추가", item.Name);

            }
        }
        public void RemoveItem(string name)
        {
            for(int i =0; i<items.Length; i++)
            {
                items[i] = null;
                Console.WriteLine("{0}을(를) 삭제", name);
                break;
            }
        }
        public void CountItem()
        {
            int cnt = 0;
            for(int i=0; i<items.Length; i++)
            {
                if (items[i] != null)
                {
                    cnt++;
                }
                else
                    continue;
            }
            Console.WriteLine("inventory : {0}", cnt);
        }
        public void SearchItem(string name)
        {
            bool isItem = false;
            foreach(Item item1 in items)
            {
                if(item1 != null)
                {
                    if (item1.Name == name)
                    {
                        isItem = true;
                        Console.WriteLine("{0}이(가) 있습니다.", name);
                        break;
                    }
                    else 
                        continue;
                }
            }
            if (!isItem)
                Console.WriteLine("아이템이 없습니다.");
            Console.WriteLine();
        }
        public void ItemType(string type)
        {
            for(int i = 0; i<items.Length; i++)
            {
                if(items[i] != null)
                {
                    if (items[i].GetType().ToString().Contains(type))
                    {
                        Console.WriteLine("Type : {0}, Name : {1}", type, items[i].Name);
                    }
                }
            }
            Console.WriteLine();
        }
        public void AddCapacity(int add)
        {
            int capacity0 = 0;
            for(int i = 0; i<items.Length; i++)
            {
                if (items[i] != null)
                    capacity0++;
            }
            Console.WriteLine("add_capacity : {0}, capacity : {1}", add, capacity0 + add);
        }
        public void PrintAllItem()
        {
            for(int i = 0; i<items.Length; i++)
            {
                if (items[i] != null)
                    Console.WriteLine(items[i].Name);
                else if (items[i] == null)
                    Console.WriteLine("[    ]");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study230125
{
    class Weapon : Item
    {
        //생성자
        public Weapon(string name, int capacity)
        {
            this.Name = name;
            this.Capacity = capacity;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study230125
{
    class Armor : Item
    {
        //생성자
        public Armor(string name, int capacity) 
        {
            this.Name = name;
            this.Capacity = capacity;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study230125
{
    class Item
    {
        public string Name
        {
            get;set;
        }
        public int Capacity
        {
            get;set;
        }
    }
}

이환국 님 블로그 참고

 

아직 스스로 문제를 못 풀겠어서 최대한 이해하려고 노력하며 쳐보았다....

 

'c# > 수업 과제' 카테고리의 다른 글

역직렬화  (0) 2023.01.13
직렬화  (0) 2023.01.13
List<T> 를 이용한 인벤토리 만들기  (0) 2023.01.11
인벤토리 만들기2  (0) 2023.01.09
버스트  (0) 2023.01.06