App Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cshNHR
{
class App
{
//생성자
public App()
{
Inventory inventory = new Inventory(2);
inventory.AddItem(new Weapon("장검"));
inventory.AddItem(new Weapon("장검"));
inventory.AddItem(new Weapon("단검"));
inventory.AddItem(new Weapon("창"));
inventory.AddItem(new Armor("가죽옷"));
inventory.AddItem(new Armor("판금갑옷"));
inventory.ItemCount();
inventory.FindByName("창");
inventory.RemoveItem("가죽옷");
inventory.PrintItemType("Weapon");
inventory.MaxUp(12);
inventory.AddItem(new Weapon("창"));
inventory.AddItem(new Armor("가죽옷"));
inventory.AddItem(new Armor("판금갑옷"));
inventory.PrintItemType("Armor");
inventory.PrintAll();
}
}
}
Inventory Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cshNHR
{
class Inventory
{
List<Item> items;
int capacity;
public Inventory(int capacity)
{
this.capacity = capacity;
items = new List<Item>();
}
public void AddItem(Item item)
{
if (items.Count == capacity)
{
Console.WriteLine("Out of space.");
}
else
{
bool isItem = false;
foreach(Item itemIn in items)
{
if (itemIn.Name == item.Name)
{
isItem = true;
}
}
if (isItem)
{
Console.WriteLine("There are duplicate items");
}
else
{
items.Add(item);
Console.WriteLine("Item({0}) has been added", item.Name);
}
}
}
public void RemoveItem(string name)
{
bool isItem = false;
foreach (Item item in items)
{
if (item != null)
{
if (item.Name == name)
{
isItem = true;
items.Remove(item);
}
}
}
if (isItem == true)
Console.WriteLine("{0} Removed", name);
else
Console.WriteLine("Item Removal Failed");
}
public void ItemCount()
{
Console.WriteLine("item count: {0}", items.Count());
}
public void MaxUp(int newCapacity)
{
Console.WriteLine("has been expanded from {0} to {1}", this.capacity, newCapacity);
this.capacity = newCapacity;
}
public void FindByName(string name)
{
bool isItem = false;
foreach(Item item in items)
{
if (item != null)
{
if (item.Name == name)
{
isItem = true;
}
}
}
if (isItem == true)
Console.WriteLine("{0} is founded", name);
else
Console.WriteLine("can not found item");
}
public void PrintItemType(string type)
{
Console.WriteLine("[Print by Type : {0}]", type);
if (type == "Weapon")
{
foreach(Item weapon in items)
{
if (weapon != null)
{
if (weapon.GetType().ToString().Contains("Weapon"))
{
Console.WriteLine("{0} | {1}", weapon.Name, type);
}
}
}
}
}
public void PrintAll()
{
Console.WriteLine("[PrintAll]");
foreach (Item item in items)
{
if (item != null)
{
Console.WriteLine("{0} | {1}", item.Name, item.GetType().ToString());
}
}
}
}
}
Item Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cshNHR
{
class Item
{
public string Name
{
get;set;
}
public Item()
{
}
}
}
Weapon Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cshNHR
{
class Weapon:Item
{
public Weapon(string name)
{
this.Name = name;
}
}
}
Armor Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cshNHR
{
class Armor:Item
{
public Armor(string name)
{
this.Name = name;
}
}
}
'C# > 수업과제' 카테고리의 다른 글
[ C# 10일차 ] 배열 직렬화/역직렬화 연습 (0) | 2023.01.13 |
---|---|
[ C# 10일차 ] 객체1 직렬화/역직렬화 연습 (0) | 2023.01.13 |
[ C# 8일차] List<T>를 이용한 Inventory 과제 (0) | 2023.01.11 |
[C# 7일차] 배열 Inventory 과제 (0) | 2023.01.10 |
[C# 5일차] Class 응용 과제&Method 활용_BRAWLSTARS(+궁극기, 스킬) (0) | 2023.01.06 |