C#/수업과제
C# 21.03.16.과제 7
HappyFrog
2021. 3. 16. 16:20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class App
{
//생성자
public App()
{
//List를 사용해서 인벤토리 제작
Console.WriteLine("App");
List<string> inventory;
inventory = new List<string>();
//아이템 삽입
inventory.Add("신발");
inventory.Add("장갑");
int n = 0;
foreach (string item in inventory)
{
Console.WriteLine("");
Console.WriteLine("{0}번칸 // {1}", n + 1, inventory[n]);
n++;
}
//아이템 검색
int i = 0;
Console.WriteLine("============================================================");
Console.WriteLine("어떤 아이템을 찾을까요? 아이템 이름을 입력하세요.");
Console.WriteLine("============================================================");
string findName = Console.ReadLine();
foreach (string itemName in inventory)
{
Console.WriteLine("============================================================");
Console.WriteLine("{0}을 찾습니다.", findName);
if (inventory[i] == findName)
{
Console.WriteLine("============================================================");
Console.WriteLine("{0}번칸에서 {1}을 찾았습니다.", i + 1, findName);
Console.WriteLine("============================================================");
break;
}
else
{
Console.WriteLine("");
Console.WriteLine("{0}번칸에는 {1}이 없습니다. 검색중입니다...", i + 1, findName);
i++;
}
}
//아이템 삭제
//*오류 (Y/N)에서 둘 중 말고 다른 문자나 키를 입력하면 "잘못 입력하셨습니다."부분이 무한루프에 걸린다.
Console.WriteLine("삭제할 아이템이 있습니까? (Y/N)");
Console.WriteLine("============================================================");
string decision = Console.ReadLine();
int itemAmount = inventory.Count;
int d = 0;
while (d < 1)
{
if (decision == "Y")
{
Console.WriteLine("어느 칸의 아이템을 삭제하시겠습니까? (숫자)");
string itemName = Console.ReadLine();
int itemNameNum = Convert.ToInt32(itemName) - 1;
if (itemNameNum < inventory.Count)
{
Console.WriteLine("============================================================");
Console.WriteLine("{0}번칸의 {1}을 삭제합니다.", itemNameNum + 1, inventory[itemNameNum]);
Console.WriteLine("============================================================");
inventory[itemNameNum] = null;
itemAmount--;
break;
}
else
{
Console.WriteLine("============================================================");
Console.WriteLine("빈 칸입니다. 다시 입력하세요.");
Console.WriteLine("============================================================");
continue;
}
}
else if (decision == "N")
{
Console.WriteLine("아이템 삭제를 취소합니다.");
break;
}
else
{
Console.WriteLine("잘못 입력하셨습니다. 다시 입력하세요.");
continue;
}
}
//정렬을 하려고했는데 자꾸 오류나서 일단 주석걸어둠
/*Console.WriteLine("정렬을 시작합니다.");
for (int x = 0; 0 < inventory.Count; x++)
{
if (x < inventory.Count - 1)
{
if (inventory[x] == null && inventory[x + 1] != null)
{
inventory[x] = inventory[x + 1];
Console.WriteLine("{0}칸으로 {1}칸의 아이템이 이동합니다.", x, x+1);
inventory[x + 1] = null;
continue;
}
else if (inventory[x] != null && inventory[x=1] == null)
{
Console.WriteLine("{0}칸은 비어있습니다...", x + 1);
continue;
}
}
else if (x == inventory.Count - 1)
{
if (inventory[x] == null)
{
Console.WriteLine("{0}칸은 비어있습니다...", x + 1);
break;
}
}
}*/
//아이템 수정
int correctRepeat = 0;
while (correctRepeat < 1)
{
Console.WriteLine("아이템을 수정하시겠습니까? (Y/N)");
string correct = Console.ReadLine();
if (correct == "Y")
{
Console.WriteLine("어느칸의 아이템을 수정하시겠습니까? (숫자)");
string correctNum = Console.ReadLine();
int correctNumber = Convert.ToInt32(correctNum) - 1;
if (correctNumber <= itemAmount)
{
Console.WriteLine("아이템의 이름을 수정하세요.");
string correctName = Console.ReadLine();
Console.WriteLine("{0}의 이름이 {1}으로 수정되었습니다.", inventory[correctNumber], correctName);
inventory[correctNumber] = correctName;
break;
}
else
{
Console.WriteLine("빈 칸을 눌렀습니다. 다시 입력하세요");
continue;
}
}
else if (correct == "N")
{
Console.WriteLine("수정을 취소합니다.");
break;
}
else
{
Console.WriteLine("잘못 입력했습니다. 다시 입력하세요.");
continue;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Item
{
public Item()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study003
{
class Inventory
{
public string itemName;
public Inventory(string itemName)
{
this.itemName = itemName;
}
}
}
================================================
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study003
{
class App
{
//생성자
public App()
{
//Dictionary<int, Product> 변수 선언
Dictionary<int, Product> products;
//컬렉션 인스턴스화
products = new Dictionary<int, Product>();
//Product 생성 (ID, 이름, 가격)
Product adSkip = new Product(03024, "광고 1초스킵", 1200);
//딕셔너리에 요소 추가 (키, 값: Product객체)
products.Add(adSkip.GetID(), adSkip);
//요소의 키로 검색
Product foundProduct = products[03024];
//foreach문으로 요소 출력
//KeyValuePair<int, Product>
foreach (KeyValuePair<int, Product> pair in products)
{
Console.WriteLine("{0} {1} {2}", foundProduct.GetID(), foundProduct.GetName(), foundProduct.GetPrice());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study003
{
class Product
{
public int ID;
public string name;
public int price;
public Product(int ID, string name, int price)
{
this.ID = ID;
this.name = name;
this.price = price;
}
public int GetID()
{
return this.ID;
}
public string GetName()
{
return this.name;
}
public string GetPrice()
{
return String.Format("{0:#,###}원", this.price);
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Item
{
public string name;
public int price;
public Item(int ID, string name, int price)
{
this.name = name;
this.price = price;
}
public string GetName(int ID)
{
return this.name;
}
public string GetPrice()
{
return String.Format("{0:#,###}gold", this.price);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class App
{
//생성자
public App()
{
//Dictionary<int, Product> 변수 선언
Dictionary<int, Item> item;
//컬렉션 인스턴스화
item = new Dictionary<int, Item>();
//Product 생성 (ID, 이름, 가격)
Item doranRing = new Item(1, "도란의 반지", 450);
Item doranSword = new Item(2, "도란의 검", 450);
//딕셔너리에 요소 추가 (키, 값: Product객체)
item.Add(1, doranRing);
item.Add(2, doranSword);
//요소의 키로 검색
Item foundItem1 = item[1];
Item foundItem2 = item[2];
Console.WriteLine(foundItem1.name);
Console.WriteLine(foundItem2.name);
//foreach문으로 요소 출력
//KeyValuePair<int, Product>
int i = 1;
foreach (KeyValuePair<int, Item> pair in item)
{
if (foundItem2.GetName(2) == item[i].name)
{
Console.WriteLine("{0} : {1}", foundItem1.name, foundItem1.GetPrice());
break;
}
else
{
Console.WriteLine("찾지 못했습니다.");
i++;
}
}
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
public enum eGrade
{
Magic, Mythic
}
class Item
{
public string name;
public eGrade grade;
public Item(string name, eGrade grade)
{
this.name = name;
this.grade = grade;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class App
{
//생성자
public App()
{
//Dictionary<int, Product> 변수 선언
Dictionary<string, Item> items;
//컬렉션 인스턴스화
items = new Dictionary<string, Item>();
//Product 생성 (ID, 이름, 가격)
Item item1 = new Item("모코코 당근", eGrade.Magic);
Item item2 = new Item("모코코 프라이팬", eGrade.Mythic);
//딕셔너리에 요소 추가 (키, 값: Product객체)
items.Add("모코코 당근", item1);
items.Add("모코코 프라이팬", item2);
//요소의 키로 검색
Item foundItem1 = items["모코코 당근"];
Console.WriteLine(foundItem1.name);
Item foundItem2 = items["모코코 프라이팬"];
Console.WriteLine(foundItem2.name);
//foreach문으로 요소 출력
//KeyValuePair<int, Product>
string name = Console.ReadLine();
foreach (KeyValuePair<string, Item> pair in items)
{
if (items[name] == items["모코코 당근"])
{
Console.WriteLine("{0}을 찾았습니다. 아이템 등급 : {1}", items[name].name, items[name].grade);
break;
}
else if (items[name] == items["모코코 프라이팬"])
{
Console.WriteLine("{0}을 찾았습니다. 아이템 등급 : {1}", items[name].name, items[name].grade);
break;
}
else
{
items[name] = null;
Console.WriteLine("결과값이 없습니다.");
break;
}
}
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class Book
{
public long ISBN;
public string name;
public Book(long ISBN, string name)
{
this.ISBN = ISBN;
this.name = name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework000
{
class App
{
//생성자
public App()
{
//Dictionary<int, Product> 변수 선언
Dictionary<long, Book> books;
//컬렉션 인스턴스화
books = new Dictionary<long, Book>();
//Product 생성 (ID, 이름, 가격)
Book book1 = new Book(9788995688908, "스키너의 심리상자 열기");
Book book2 = new Book(9788955860641, "스키너의 심리상자 닫기");
long a = 9788995688908;
long b = 9788955860641;
//딕셔너리에 요소 추가 (키, 값: Product객체)
books.Add(a, book1);
books.Add(b, book2);
//요소의 키로 검색
Book foundBook1 = books[a];
Book foundBook2 = books[b];
//foreach문으로 요소 출력
//KeyValuePair<int, Product>
foreach(KeyValuePair<long,Book> pair in books)
{
Console.Write("{0} : {1} // ", pair.Key, pair.Value.name);
}
}
}
}