体检套餐管理系统 -- Dictionary<K,V>双列集合

时间:2022-05-04
本文章向大家介绍体检套餐管理系统 -- Dictionary<K,V>双列集合,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本文章为 Dictionary<K,V>双列集合开发项目,如需要List<T>单列集合开发的此项目,请到楼主博客园寻找

博客网址:http://www.cnblogs.com/lsy131479/

窗体

一.首先创建项目类

 public class HealthCheckItem
    {
        //项目描述
        private string description;
        //项目名称
        private string name;
        //项目价格
        private int price;

        //无参构造
        public HealthCheckItem()
        {
        }

        //有参构造
        public HealthCheckItem(string description, string name, int price)
        {
            this.description = description;
            this.name = name;
            this.price = price;
        }

        public string Description { get => description; set => description = value; }
        public string Name { get => name; set => name = value; }
        public int Price { get => price; set => price = value; }
    }

二.创建套餐类

/// <summary>
    /// 套餐类
    /// </summary>
   public class HealthCheckSet
    {
        //套餐名
        private string name;
        //套餐总价格
        private int price;
        //存储套餐内的项目
         private Dictionary<string, HealthCheckItem> items = new Dictionary<string, HealthCheckItem>();

        //无参构造
        public HealthCheckSet()
        {
        }
        //有参构造
        public HealthCheckSet(string name)
        {
            this.name = name;
            
        }

        

        public string Name { get => name; set => name = value; }
        public int Price { get => price; set => price = value; }
        public Dictionary<string, HealthCheckItem>{ get => items; set => items = value; }
    }

三.主窗体代码

   /// <summary>
    /// 体检套餐管理系统 -- Dictionary<K,V>双列集合
    /// </summary>
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //删除多余列
            this.dataGridView1.AutoGenerateColumns = false;
            //清楚多余行
            this.dataGridView1.AllowUserToAddRows = false;

            /*
             * 套餐下拉框初始化
             * */
            HealthCheckSet set0 = new HealthCheckSet("请选择");
            HealthCheckSet set1 = new HealthCheckSet("入学体检");
            set.Add(set0.Name, set0);
            set.Add(set1.Name, set1);
            AddExamCbo();
        }

        //刷新项目下拉框
        public void AddExamCbo()
        {
            BindingSource source = new BindingSource();
            source.DataSource = set.Keys;
            this.cboExams.DataSource = source;
        }
        //套餐集合
        Dictionary<string, HealthCheckSet> set = new Dictionary<string, HealthCheckSet>();
        //初始化项目集合
        Dictionary<string, HealthCheckItem> allItems = new Dictionary<string, HealthCheckItem>();
        public void AddPhyCbo()
        {
            /*
             * 初始化(刷新)项目下拉框
             * */
            allItems.Clear();
            HealthCheckItem item1 = new HealthCheckItem("用于检查身高。", "身高", 5);
            HealthCheckItem item2 = new HealthCheckItem("用于检查体重。", "体重", 5);
            HealthCheckItem item3 = new HealthCheckItem("用于检查肝功能。", "肝功能", 50);
            HealthCheckItem item4 = new HealthCheckItem("用于检查视力。", "视力", 5);
            HealthCheckItem item5 = new HealthCheckItem("用于检查听力。", "听力", 5);
            HealthCheckItem item6 = new HealthCheckItem("用于检查B超。", "B超", 80);
            HealthCheckItem item7 = new HealthCheckItem("用于检查心电图。", "心电图", 100);
            allItems.Add(item1.Name, item1);
            allItems.Add(item2.Name, item2);
            allItems.Add(item3.Name, item3);
            allItems.Add(item4.Name, item4);
            allItems.Add(item5.Name, item5);
            allItems.Add(item6.Name, item6);
            allItems.Add(item7.Name, item7);
            this.cboPhy.DisplayMember = "name";
            BindingSource source = new BindingSource();
            source.DataSource = allItems.Keys;
            this.cboPhy.DataSource = source;
        }

        private void cboExams_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
             * 改变下拉框下标,判断下标。改变状态
             * */
            this.lblNames.Text = this.cboExams.Text;
            RenovateDgv();
            Btn();
            if (this.cboExams.SelectedIndex > 0)
            {
                this.btnAdd.Enabled = true;
                AddPhyCbo();
            }
            else
            {
                this.btnAdd.Enabled = false;
                this.cboPhy.DataSource = null;
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            //非空验证
            if (txtNewName.Text == "" || txtNewName.Text == null)
            {
                MessageBox.Show("请输入套餐名称!");
                return;
            }
            /*
             * 集合添加套餐名
             * 并刷新下拉框
             * */
            HealthCheckSet set3 = new HealthCheckSet(this.txtNewName.Text);
            set.Add(set3.Name, set3);
            AddExamCbo();
            this.cboExams.Text = set3.Name;
            MessageBox.Show("添加成功!");
        }

        public void AddDgv()
        {
            //ToList()将双列集合转为单列集合
            //Contains()在单列集合中寻找是否已存在该项
            if (set[cboExams.Text].Items.Keys.ToList().Contains(this.cboPhy.Text))
            {
                MessageBox.Show("您已添加过此项!");
                return;
            }
            /*
             * 向套餐内添加项目
             * */
            set[cboExams.Text].Items.Add(cboPhy.Text, allItems[cboPhy.Text]);
            Btn();
            MessageBox.Show("添加成功!");

        }
        public void RenovateDgv()
        {
            /*
             * 刷新datagridview
             * */
            BindingSource source = new BindingSource();
            source.DataSource = set[cboExams.Text].Items.Values;
            dataGridView1.DataSource = source;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            /*
             * 添加套餐项目
             * */
            AddDgv();
            RenovateDgv();
        }

        public void Btn()
        {
            /*
             * 由集合内是否有元素,来改变删除按钮的状态
             * */
            if (set[cboExams.Text].Items.Count > 0)
            {
                this.btnDel.Enabled = true;
            }
            else
            {
                this.btnDel.Enabled = false;
            }

            /*
             * 计算套餐总金额
             * 并刷新控件
             * */
            set[cboExams.Text].Price = 0;
            foreach (KeyValuePair<string, HealthCheckItem> it in set[cboExams.Text].Items)
            {
                set[cboExams.Text].Price += it.Value.Price;
            }
            this.lblPrices.Text = set[cboExams.Text].Price.ToString();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            /*
             * 由键来移除指定的集合元素
             * 并刷新控件显示的值与状态
             * */
            set[cboExams.Text].Items.Remove(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString().Trim());
            RenovateDgv();
            Btn();
            MessageBox.Show("删除成功!");
        }
    }