C#/클래스 예제

[C#, Class] 배달 어플 구현하기

어묵 2022. 4. 9. 02:13
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
class Program
    {
        static void Main(string[] args)
        {
            #region 가게 등록하기
            List<Shop> shops = new List<Shop>();
            shops.Add(
                new Shop("까스까스 돈까스"902330)
                    .AddMenu("왕돈까스"8000)
                    .AddMenu("매운돈까스"9000)
                    .AddMenu("치즈돈까스"10000)
                    .AddMenu("돈까스세트"12000)
                    .AddMenu("새우튀김"3000)
                );
            shops.Add(
                new Shop("하오하오 니하오"902330)
                    .AddMenu("짜장면"6000)
                    .AddMenu("짬뽕"7000)
                    .AddMenu("볶음밥"7000)
                    .AddMenu("삼선볶음밥"9000)
                    .AddMenu("카스"4500)
                    .AddMenu("후레시"4000)
                );
 
            Console.WriteLine("가게 리스트 ---");
            for (int i = 0; i < shops.Count; i++)
            {
                Console.WriteLine($"{i + 1}. {shops[i].name}");
            }
            Console.WriteLine();
            Console.Write("원하는 가게를 골라주세요 : ");
            int shopIndex = int.Parse(Console.ReadLine());
            Console.Clear();
            #endregion
 
            #region 메뉴 고르기
            Shop selectShop = shops[shopIndex - 1];
            Dictionary<FoodMenu, int> foods = new Dictionary<FoodMenu, int>();
            int foodIndex = -1;
            while (foodIndex != 0)
            {
                for (int i = 0; i < selectShop.foods.Count; i++)
                {
                    Console.WriteLine($"{i + 1}. {selectShop.foods[i].name}, {selectShop.foods[i].price}원");
                }
                Console.WriteLine();
                Console.WriteLine("0. 메뉴 담기 완료");
                Console.Write("원하는 메뉴를 골라주세요 : ");
                foodIndex = int.Parse(Console.ReadLine());
                if (foodIndex != 0) {
                    if (foods.ContainsKey(selectShop.foods[foodIndex - 1]))
                    {
                        foods[selectShop.foods[foodIndex - 1]]++;
                    }
                    else
                    {
                        foods.Add(selectShop.foods[foodIndex - 1], 1);
                    }
                }
                Console.Clear();
            }
            #endregion
 
            #region 메뉴 주문하기
            Console.WriteLine("주문 상품");
            int sum = 0;
            foreach (var item in foods)
            {
                sum += item.Key.price * item.Value;
                Console.WriteLine($"{item.Key.name} x {item.Value} : {item.Key.price * item.Value}원");
            }
            Console.WriteLine("-------------------------------");
            Console.WriteLine($"종합 : {sum}원");
            #endregion
        }
    }
 
    class Shop
    {
        public string name; // 가게 이름
        public int openTime; // 오픈 시간
        public int closeTime; // 마감 시간
        public List<FoodMenu> foods; //
 
        public Shop(string name, int openHour, int openMinute, int closeHour, int closeMinute)
        {
            this.name = name;
            openTime = openHour * 60 + openMinute;
            closeTime = closeHour * 60 + closeMinute;
            foods = new List<FoodMenu>();
            // List 초기화
        }
 
        public Shop AddMenu(string name, int price)
        {
            foods.Add(new FoodMenu(name, price));
            return this;
        }
    }
 
    class FoodMenu
    {
        public string name; // 음식 이름
        public int price; // 음식 가격
 
        public FoodMenu(string name, int price)
        {
            this.name = name;
            this.price = price;
        }
    }
cs