본문 바로가기

C#/복습

메서드반환 문제2

현재 교육받고 있는 학원에서 강사님이 좋은 제도를 만드셔서,

복습도 할겸 만들어본 메서드반환 문제입니다.

카페에 업로드한 것과 동일합니다.

 

문제의 요지는 아이템리스트 컬렉션 객체와 실체화된 이름 컬렉션 객체, 수량 컬렉션 객체가 있습니다.

메서드를 이용해 아이템리스트 컬렉션 객체안에 값을넣어 실체화 시켜주세요.

-----------------------------------------------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Question_2
{
    class ItemData
    {
        public string name;
        public int amount;
        public ItemData(string name, int amount)
        {
            this.name = name;
            this.amount = amount;
        }
    }
    class App
    {
        List<ItemData> ItemDatasList;
        List<string> itemNameList;
        List<int> itemAmountList;
        public App()
        {
            itemNameList = new List<string>();
 
            itemNameList.Add("김밥");
            itemNameList.Add("순대");
 
            itemAmountList = new List<int>();
 
            itemAmountList.Add(3);
            itemAmountList.Add(4);
 
            ItemDatasList = this.AdditemDataList(itemNameList,itemAmountList);
            
            foreach (ItemData element in ItemDatasList)
            {
                Console.WriteLine("{0}X{1}",element.name,element.amount);   //김밥X3 순대X4
            }
        }
 
 
        public List<ItemData> AdditemDataList(List<string> itemNameList, List<int> itemAmountList)
        {
            List<ItemData> newItemDataList = new List<ItemData>();
 
            for (int i = 0; i < itemNameList.Count; i++)
            {
                ItemData itemData = new ItemData(itemNameList[i], itemAmountList[i]);
                newItemDataList.Add(itemData);
            }
            return newItemDataList;
        }
    }
}
 
 
 

-----------------------------------------------------------------------------------------------------------------------------------

출력값

-----------------------------------------------------------------------------------------------------------------------------------