728x90
(복습 내용)
0. 장검
1. 단검
2. 활
3. [ ]
4. [ ]
--------------------------------
찾을 아이템: 장검
아이템 이름: 장검, 찾는 이름: 장검
찾았다!: 장검
--------------------------------
0. [ ]
1. 단검
2. 활
3. [ ]
4. [ ]
<Item 클래스>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Item
{
public string name;
//생성자
public Item(string name)
{
this.name = name;
}
}
}
<Inventory 클래스>
using Starcraft;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace starcraft
{
internal class Inventory
{
public int capacity;
public Item[] items;
public int index;
//생성자
public Inventory(int capacity)
{
this.capacity = capacity;
this.items = new Item[this.capacity];
}
//아이템 넣기 메서드
public void AddItem(Item item)
{
//용량 초과시 오류문
int lastindex = this.capacity - 1;
if(this.index > lastindex)
{
Console.WriteLine("공간이 부족합니다.");
return;
}
//아이템 넣기
//Console.WriteLine("-> {0}", this); <- 오류나서 확인해봄
this.items[index] = item;
index++;
}
//모든 아이템 출력 메서드
public void PrintAllItem()
{
for(int i =0;i< this.items.Length; i++)
{
if (items[i] == null)
{
Console.WriteLine("{0}. [ ]", i);
}
else
{
Console.WriteLine("{0}. {1}", i, items[i].name);
}
}
}
//아이템 이름 찾고 가져오기 메서드
public Item GetItemByName(string searchName)
{
Console.WriteLine("찾을 아이템: {0}", searchName);
//배열의 모든 요소(Item)의 이름 출력
for (int i = 0; i < items.Length; i++)
{
Item item = this.items[i];
if (items[i] != null)
{
Console.WriteLine("아이템 이름: {0}, 찾는 이름: {1}",items[i].name,searchName);
//배열 i 인덱스 요소 아이템의 이름과 찾고자 하는 아이템의 이름과 비교
if (items[i].name == searchName)
{
Console.WriteLine("찾았다!: {0}", item.name);
items[i] = null;
return item;
}
}
}
return null;
}
}
}
<App>
using starcraft;
using Starcraft;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자 메서드
public App()
{
//인벤토리 변수 inventory 정의
//Inventory inven;
//인벤토리 인스턴스 생성후 변수에 할당
Inventory inven = new Inventory(5);
//아이템 인스턴스 생성후 인벤토리에 넣기
//Item item0 = new Item("장검");
//Item item1 = new Item("단검");
//Item item2 = new Item("활");
//inven.AddItem(item0);
//inven.AddItem(item1);
//inven.AddItem(item2);
// <==== 굳이 변수 3개 안쓰는거 만들지 말고 바로 넣자!!!
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("단검"));
inven.AddItem(new Item("활"));
//인벤토리의 모든 아이템 출력
inven.PrintAllItem();
Console.WriteLine("--------------------------------");
//찾을 아이템 입력하고 인벤토리에서 빼기
string searchName = "장검";
inven.GetItemByName(searchName);
Console.WriteLine("--------------------------------");
//인벤토리 모든 아이템 재출력(잘 빠졌나 확인)
inven.PrintAllItem();
}
}
}


'KDT > C# 프로그래밍' 카테고리의 다른 글
23/07/25 다차원 배열2 (0) | 2023.07.25 |
---|---|
23/07/25 다차원배열 (0) | 2023.07.25 |
23/07/26 인벤토리 생성 복습 (0) | 2023.07.25 |
23/07/25 배열 복습2 (0) | 2023.07.25 |
23/07/25 배열 복습 (0) | 2023.07.25 |