7. 西式餐點-點餐服務系統

using System.Linq.Expressions;
using System.Security.AccessControl;

namespace Q7
{
    public partial class Form1 : Form
    {

        Dictionary<GroupBox, (string, int)[]> objects = new Dictionary<GroupBox, (string, int)[]>();
        public Form1()
        {
            InitializeComponent();

            objects[groupBox1] = [
                ("香酥脆皮雞排", 250),
                ("特選沙朗牛排",380),
                ("特選菲力牛排", 430),
                ("什錦海鮮", 450),
                ("法式藍帶豬排", 300),
                ("海陸大餐", 570),
            ];
            objects[groupBox2] = [
                ("生菜沙拉",60),
                ("凱薩醬",60),
                ("和風醬",60),
                ("優格水果沙拉",60),
                ("千島醬",60),
                ("義大利醬",60),
                ];
            objects[groupBox3] = [
                ("洋蔥鱈魚肝",80),
                ("泰式嫩菲力",80),
                ("煙燻鮭魚",80),
                ("香蒜烤田螺",80),
                ("黑菌鵝肝醬",80),
                ];
            objects[groupBox4] = [
                ("雞蓉巧達湯",100),
                ("海鮮燉魚湯",100),
                ("烤洋蔥湯",100),
                ("南瓜湯",100),
                ("脆皮濃湯",100),
                ];
            objects[groupBox5] = [
                ("雞蛋布丁",30),
                ("焦糖蛋糕",50),
                ("義式布丁",40),
                ("提拉米蘇",50),
                ("柳橙水果凍",50),
                ];
            objects[groupBox7] = [
                ("沖泡熱奶茶",70),
                ("熱咖啡",70),
                ("蜂蜜柚子茶",90),
                ("拿鐵熱咖啡",70),
                ("熱金桔檸檬梅子茶",100),
                ];
            objects[groupBox6] = [
                ("可口可樂",50),
                ("冰咖啡",70),
                ("冰金桔檸檬",100),
                ("芒果汁",90),
                ("冰拿鐵",70),
                ];
            foreach (var item in objects)
            {
                var layout = new TableLayoutPanel();
                var t = new Label();
                t.Text = "品名";
                layout.Controls.Add(t);
                t = new Label();
                t.Text = "單價";
                layout.Controls.Add(t);
                t = new Label();
                t.Text = "數量";
                layout.Controls.Add(t);

                layout.ColumnCount = 3;
                layout.Width = item.Key.Width - 20;
                layout.Height = item.Key.Height - 40;
                layout.Top = 30;
                layout.Left = 10;
                foreach (var item1 in item.Value)
                {
                    var number = new NumericUpDown();
                    number.Maximum = 20;
                    number.Minimum = 0;
                    var price = new TextBox();
                    price.Text = item1.Item2.ToString();
                    var lvl = new Label();
                    lvl.Text = item1.Item1;
                    lvl.Width = 120;
                    number.AutoSize = false;
                    price.Width = number.Width = 50;
                    layout.Controls.Add(lvl);
                    layout.Controls.Add(price);
                    layout.Controls.Add(number);
                }
                item.Key.Controls.Add(layout);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item in objects.Keys)
            {
                foreach (var item1 in item.Controls[0].Controls)
                {
                    if (item1 is NumericUpDown)
                    {
                        ((NumericUpDown)item1).Value = 0;
                    }
                }
            }
            label1.Text = "等待客人點餐中";
            label1.ForeColor = Color.Gray;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int sum = 0;
            foreach (var k in objects.Keys)
            {
                var item = k.Controls[0];
                for (int i = 3; i < item.Controls.Count; i += 3)
                {
                    var price = item.Controls[i + 1];
                    var amt = item.Controls[i + 2];
                    sum += int.Parse(price.Text) * (int)((NumericUpDown)amt).Value;
                }
            }
            label1.ForeColor = Color.White;
            label1.Text = $"總價: {Math.Floor(sum * 1.05 + 0.5):f0} 元";
        }
    }
}

Last updated