体检套餐管理系统

                                          体检套餐系统

                             项目总结

                     

             此类为HealthCheckItem(检查项目)类

                                           //HealthCheckItem类
                                           //作用是检查项目

        private int price;                 //检查项目的价格
        private string description;        //检查项目的描述
        private string name;               //检查项目的名称
        public string Name                 
        {
            get { return name; }
            set { name = value; }
        }   
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public int Price
        {
            get { return price; }
            set { price = value; }
        }


        public HealthCheckItem(string name, int price, string description)
        {
            this.Name = name;
            this.Description = description;
            this.Price = price;
        }                                

                                                  此类为套餐类TaoCan(体检套餐

    public class TaoCan                         //体检套餐类
    {
        private int price;                      //套餐总价格
        private List<HealthCheckItem> items;    //套餐集合
        private string name;                    //套餐名称

        public TaoCan()
        {
            items = new List<HealthCheckItem>();
        }
        public TaoCan(string name, List<HealthCheckItem> items)
        {
            this.Name = name;
            this.items = items;
        }


        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public List<HealthCheckItem> Items
        {
            get { return items; }
            set { items = value; }
        }

        public int Price
        {
            get { return price; }
            set { price = value; }
        }
        public void CalcPrice()
        {
            int totalPrice = 0;
            foreach (HealthCheckItem item in items)
            {
                totalPrice += item.Price;
            }
            this.price = totalPrice;
        }
    }

                                          以下代码为:体检套餐管理系统窗体

        //在此将一下的几个添加项目定义成全局变量;
        HealthCheckItem height, weight, sight, hearing, liverFun, ekg, bWaves, bloodPressure, bloodTest;

        TaoCan setA;
        //存储所以检查项目
        List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
        //存储套餐中的检查项目
        List<HealthCheckItem> items = new List<HealthCheckItem>();
        //保存医院中支持的所有TC,为了绑定左侧的套餐下拉框而编写
        public Dictionary<string, TaoCan> HealthSet = new Dictionary<string, TaoCan>();
        //此方法添加几个检查项目
        public void InitItems()
        {

            height = new HealthCheckItem("身高", 5, "用于检查身高");
            weight = new HealthCheckItem("体重", 5, "用于检查体重.");
            sight = new HealthCheckItem("视力", 10, "用于检查视力.");
            hearing = new HealthCheckItem("听力", 10, "用于检查听力.");
            liverFun = new HealthCheckItem("肝功能", 50, "用于检查肝功能.");
            bWaves = new HealthCheckItem("B超", 30, "用于检查B超.");
            ekg = new HealthCheckItem("心电图", 50, "用于检查心电图.");
            bloodPressure = new HealthCheckItem("血压", 20, "用于检查血压.");
            bloodTest = new HealthCheckItem("血常规", 20, "用于检查血常规.");

            AllItems.Add(height);
            AllItems.Add(weight);
            AllItems.Add(sight);
            AllItems.Add(hearing);
            AllItems.Add(liverFun);
            AllItems.Add(bWaves);
            AllItems.Add(ekg);
            AllItems.Add(bloodPressure);
            AllItems.Add(bloodTest);
        }
        //添加一个套餐
        private void InitSets()
        {
            items = new List<HealthCheckItem>();
            items.Add(height);
            items.Add(weight);
            items.Add(liverFun);

            setA = new TaoCan("入学体检", items);
            setA.CalcPrice();
            HealthSet.Add("入学体检", setA);

        }
        public void InitHealthSetList()
        {
            cboSets.Items.Clear();
            cboSets.Items.Add("请选择");
            foreach (string key in HealthSet.Keys)
            {
                cboSets.Items.Add(key);
            }
            cboSets.SelectedIndex = 0;
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            lblSetName.Text = "";
            lblSetPrice.Text = "";
            btnAdd.Enabled = false;
            btnDelete.Enabled = false;

            InitItems();
            InitSets();
            InitHealthSetList();
            dgvList.AutoGenerateColumns = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("请输入套餐名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                TaoCan Hch = new TaoCan();
                HealthSet.Add(txtName.Text, Hch);
                InitHealthSetList();
                //将新加入的套餐默认选中
                cboSets.SelectedIndex = HealthSet.Count;
                lblSetName.Text = cboSets.Text;
                Hch.Name = cboSets.Text;

                MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //更新套餐检查项目(职责分离)
        private void UpdateSet(TaoCan set)
        {
            dgvList.DataSource = new BindingList<HealthCheckItem>(set.Items);
        }

        private void cboSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            string setName = cboSets.Text;
            if (setName == "请选择")
            {
                dgvList.DataSource = null;
                lblSetName.Text = "";
                lblSetPrice.Text = "";
                return;
            }
            //设置套餐名称
            lblSetName.Text = HealthSet[setName].Name;
            //设置套餐总价
            lblSetPrice.Text = HealthSet[setName].Price.ToString();
            //设置清楚按钮为可用状态
            UpdateSet(HealthSet[setName]);
            //设置清楚按钮为可用状态
            btnDelete.Enabled = true;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string Name = cboSets.Text;
            if (dgvList.SelectedRows.Count == 0)
            {
                MessageBox.Show("没有选择删除项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int index = dgvList.SelectedRows[0].Index;
            HealthSet[Name].Items.RemoveAt(index);
            HealthSet[Name].CalcPrice();
            UpdateSet(HealthSet[Name]);
            lblSetName.Text = setA.Name;
            string cboSetText = cboSets.Text;
            lblSetPrice.Text = HealthSet[cboSetText].Price.ToString();
            MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboItems.Text != "请选择")
            {
                btnAdd.Enabled = true;
            }
            else
            {
                btnAdd.Enabled = false;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cboItems.SelectedIndex == 0)
            {
                MessageBox.Show("请选择一个项目");
                return;
            }
            string cboSetText = cboSets.Text;
            if (cboSetText == "请选择")
            {
                MessageBox.Show("请选择套餐!");
                return;
            }
            int index = cboItems.SelectedIndex - 1;
            if (!HealthSet[cboSetText].Items.Contains(AllItems[index]))
            {
                HealthSet[cboSetText].Items.Add(AllItems[index]);
                HealthSet[cboSetText].CalcPrice();
                UpdateSet(HealthSet[cboSetText]);
                lblSetName.Text = HealthSet[cboSetText].Name;
                lblSetPrice.Text = HealthSet[cboSetText].Price.ToString();
                MessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("已存在请勿重复添加", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
原文地址:https://www.cnblogs.com/liuguangyiduan/p/4803341.html