C#/수업내용

Study_010 - 과일, 과일바구니 클래스 배열

McRobbin 2020. 4. 16. 12:00
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    public class FruitBasket
    {
        Fruit[] FruitList;
        int AddIndex;
 
        public FruitBasket(int capacity)
        {
            this.FruitList = new Fruit[capacity];
            AddIndex = 0;
        }
        
        public void AddFruit()
        {
            Console.Write("좋아하는 과일을 입력하세요.");
            this.AddFruit(new Fruit(Console.ReadLine()));
        }
 
        public void AddFruit(Fruit fruit)
        {
            if (AddIndex != FruitList.Length)
            {
                FruitList[AddIndex] = fruit;
                AddIndex++;
                Console.WriteLine("{0}을 장바구니에 담았습니다. ", fruit.FruitName);
            }
            else
            {
                Console.WriteLine("장바구니가 가득 찼습니다.");
            }
        }
 
        public void AddFruit(string FruitName)
        {
            this.AddFruit(new Fruit(FruitName));
        }
 
        public void PrintFruitList()
        {
            Console.WriteLine("장바구니에 담긴 과일=================\n");
            int index = 0;
            foreach (Fruit fruit in this.FruitList)
            {
                if (fruit == null)
                    Console.WriteLine("{0}, null, [    ]", index++);
                else
                {
                    Console.WriteLine("{0}, {1}, [{2}]", index++,
                        fruit,
                        fruit.FruitName);
                }
            }
        }
 
        public void PrintFruitListReverse()
        {
            Console.WriteLine("장바구니에 담긴 과일=================\n");
            int index = this.AddIndex;
            foreach (Fruit fruit in this.FruitList)
            {
                --index;
                if (fruit == null)
                    Console.WriteLine("{0}, null, [    ]", index);
                else
                {
                    Console.WriteLine("{0}, {1}, [{2}]", index,
                        this.FruitList[index],
                        this.FruitList[index].FruitName);
                }
            }
        }
    }
}
 
 
 

 

과일 넣는 메서드를 string 과일이름, Fruit 과일, 아무 매개변수를 받지 않을 때 직접 입력 받도록 해서

3가지로 구현했습니다.

무든 과일의 출력과 역순출력 둘다 구현했습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    public class Fruit
    {
        public string FruitName;
 
        public Fruit(string name) => this.FruitName = name;
    }
}
 
 
 

 

과일 클래스 입니다. 메서드는 없습니다.

 

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
 
    public class App
    {
        public App()
        {
            Console.WriteLine("2020-04-16\n=======================================");
 
 
 
            /*
            //아이템 배열 출력 예제.
            Item[] ItemArray = new Item[5];
 
            ItemArray[0] = new Item("장검");
            ItemArray[1] = new Item("활");
            ItemArray[2] = new Item("단검");
 
            //for문으로 출력.
            Console.WriteLine("for문으로 출력.");
            for(int i = 0; i < ItemArray.Length; i++)
            {
                Console.WriteLine("인덱스 : {0}, 인스턴스 : {1}, 이름 : {2}", i,
                    ItemArray[i],
                    (ItemArray[i] == null) ? "가질 수 없음." : ItemArray[i].ItemName);
            }
 
            //foreach로 출력.
            Console.WriteLine("foreach문으로 출력.");
            int index = 0;
            foreach(Item item in ItemArray)
            {
                Console.WriteLine("인덱스 : {0}, 인스턴스 : {1}, 이름 : {2}", index++,
                    item,
                    (item == null) ? "가질 수 없음." : item.ItemName);
            }
            */
 
 
 
 
 
 
 
 
            /*
            //좋아하는 과일 출력 배열 예제. 스트링.
            string[] fruitsArray = new string[3];
 
            for(int i = 0; i < fruitsArray.Length; i++)
            {
                Console.Write("좋아하는 과일을 입력해주세요. ");
                fruitsArray[i] = Console.ReadLine();
            }
 
            foreach (string fruit in fruitsArray)
                Console.WriteLine(fruit);
            Console.WriteLine("를 좋아하시는 군요.");
            */
 
 
 
 
 
 
 
 
            /*
            //과일 출력 배열 클래스.
            Fruit[] FruitArray = new Fruit[3];
 
            for(int i = 0; i < FruitArray.Length; i++)
            {
                Console.Write("좋아하는 과일을 입력해주세요. ");
                FruitArray[i] = new Fruit(Console.ReadLine());
            }
 
 
            int index = 3;
            foreach(Fruit fruit in FruitArray)
            {
                index--;
                Console.WriteLine("{0}, {1}, {2}", index, FruitArray[index], FruitArray[index].FruitName);
            }
            Console.WriteLine("를 좋아하시는 군요.");
            */
 
 
 
 
 
 
 
 
 
 
            //과일 장바구니 클래스 배열.
            FruitBasket basket = new FruitBasket(3);
            basket.AddFruit("복숭아");
            basket.AddFruit();
            basket.AddFruit(new Fruit("사과"));
 
            basket.PrintFruitList();
            basket.PrintFruitListReverse();
        }
    }
}
 
 
 

 

App 생성자 내용입니다. 마음대로 변경해가며 사용하시면 되겠습니다.