14일차 배열의 선언 및 초기화. cost와 name 멤버변수를 가진 클래스 product

Console Programming/C# Console 2019. 10. 10. 13:17
 
Program.cs
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Console1010
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

 
App.cs
 
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
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Console1010
{
    public class App
    {
        //배열 선언
        Product[] arrProducts;
 
        public App()
        {
            //배열 초기화
            arrProducts = new Product[5];
 
            //객체 생성
            var product1 = new Product(2500"운동화");
 
            //배열에 데이터추가
            arrProducts[4= product1;
            arrProducts[1= new Product(3300"후드티");
            //배열의 요소 출력
           //DisplayProducts();
 
            FindItemByCost(2600);
       
        }
        public void FindItemByCost(int cost)
        {
 
            foreach (Product product in arrProducts)
            {
                
                    if (product != null && product.cost == cost)
                    {
                        Console.WriteLine(product.name);
                        return;
                    }
                
            }            
            Console.WriteLine("제품을 찾을 수 없습니다");
        }
 
 
        public void DisplayProducts()
        {
            foreach (Product product in arrProducts)
            {
                if (product != null)
                {
                    Console.WriteLine(product.name);
                }
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

App에 정의된 기능

FindItemByCost(int cost) : 가격으로 일치하는 아이템을 찾아 그 이름을 출력

 

DisplayProducts() : arrProducts의 모든 제품이름을 출력

 

 

 

 

 
Product.cs
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Console1010
{
    public class Product
    {
 
        //data
        //name
        //cost
        public int cost;
        public string name;
 
        
        public Product(int cost, string name)
        {
            this.cost = cost;
            this.name = name;
        }
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

 

 

 

 

 

: