前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >体检套餐管理系统 -- List<T>单列集合

体检套餐管理系统 -- List<T>单列集合

作者头像
房上的猫
发布2018-03-14 12:41:52
1.6K1
发布2018-03-14 12:41:52
举报
文章被收录于专栏:个人随笔个人随笔

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

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

窗体

一.首先定义项目类

 
 /// <summary>
    /// 项目类
    /// </summary>
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 List<HealthCheckItem> items = new List<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 List<HealthCheckItem> Items { get => items; set => items = value; }
    }

 三.主窗体代码

  /// <summary>
        /// 体检套餐管理系统 -- List<T>单列集合
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();
        }

        //存储套餐类的集合
        List<HealthCheckSet> Set = new List<HealthCheckSet>();
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //删除多余的列
            this.dgvHealth.AutoGenerateColumns = false;
            //删除多余的行
            this.dgvHealth.AllowUserToAddRows = false;

            /*
             * 初始化套餐类集合
             * 调用刷新datagridview的方法
             * */
            HealthCheckSet set0 = new HealthCheckSet("请选择");
            HealthCheckSet set1 = new HealthCheckSet("入学体检");
            Set.Add(set0);
            Set.Add(set1);
            AddExamCbo();
        }

        //存储所有套餐内项目的集合
        public List<HealthCheckItem> allItems = new List<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.AddRange(new HealthCheckItem[] { item1, item2, item3, item4, item5, item6, item7 });
            /*
             * 绑定刷新项目下拉框
             * */
            this.cboPhy.DisplayMember = "name";
            this.cboPhy.DataSource = new BindingList<HealthCheckItem>(allItems);
        }

        public void AddExamCbo()
        {
            /*
             * 绑定刷新套餐下拉框
             * */
            this.cboExams.DataSource = new BindingList<HealthCheckSet>(Set); ;
            this.cboExams.DisplayMember = "name";
        }

        private void cboExams_SelectedIndexChanged(object sender, EventArgs e)
        {
            //调用绑定刷新datagridview方法
            AddDgv();
            //调用刷新金额的方法
            CalcPrice();
            //调用删除按钮状态方法
            Btn();

            /*
             * 如果不是请选择也就是0下标还原初始状态(按钮为禁用,项目下拉框无项目)
             * 反之,按钮解除禁用状态,项目下拉框从新填充数据
             * */
            if (this.cboExams.SelectedIndex > 0)
            {
                AddPhyCbo();
                this.btnAdd.Enabled = true;
                this.lblNames.Text = cboExams.Text;
            }
            else
            {
                this.btnAdd.Enabled = false;
                this.btnDel.Enabled = false;
                this.lblNames.Text = cboExams.Text;
                this.cboPhy.DataSource = null;

            }
        }

        public void AddDgv()
        {
            /*
             * 将选中套餐的项绑定刷新datagridview
             * */
            foreach (HealthCheckSet item in Set)
            {
                if (item.Name.Equals(cboExams.Text))
                {
                    this.dgvHealth.DataSource = new BindingList<HealthCheckItem>(item.Items);
                    return;
                }
            }
        }

        public void item()
        {
            foreach (HealthCheckSet item in Set)
            {
                if (item.Name.Equals(cboExams.Text))
                {
                    foreach (HealthCheckItem it in item.Items)
                    {
                        if (it.Name.Equals(this.cboPhy.Text))
                        {
                            /*
                             * 判定是否已存在要添加的项
                             * */
                            MessageBox.Show("您已添加过此项!");
                            return;
                        }
                    }
                    foreach (HealthCheckItem it in allItems)
                    {
                        if (it.Name.Equals(this.cboPhy.Text))
                        {
                            /*
                             * 如果通过验证,可以添加,则将该项添加到集合中,并刷新datagridview与套餐价格
                             * */
                            item.Items.Add(it);
                            AddDgv();
                            CalcPrice();
                            MessageBox.Show("添加成功!");

                            return;
                        }
                    }
                    return;
                }
            }
        }
        public void CalcPrice()
        {
            foreach (HealthCheckSet item in Set)
            {
                if (item.Name.Equals(cboExams.Text))
                {
                    /*
                     * 计算套餐金额
                     * */
                    item.Price = 0;
                    foreach (HealthCheckItem it in item.Items)
                    {
                        item.Price += it.Price;
                    }
                    //将套餐金额赋值给控件,并显示
                    this.lblPrices.Text = item.Price.ToString();
                    return;
                }
            }
            
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //添加套餐项目

            item();
            Btn();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            //非空验证
            if (txtNewName.Text == "" || txtNewName.Text == null)
            {
                MessageBox.Show("请输入套餐名称!");
                return;
            }

            /*
             * 将通过验证的套餐名称添加到集合并刷新套餐下拉框
             * */
            HealthCheckSet set = new HealthCheckSet(this.txtNewName.Text);
            Set.Add(set);

            AddExamCbo();
            this.cboExams.Text = this.txtNewName.Text;
            MessageBox.Show("添加成功!");
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            foreach (HealthCheckSet item in Set)
            {
                if (item.Name.Equals(cboExams.Text))
                {
                    for (int i = 0; i < item.Items.Count; i++)
                    {
                        if (item.Items[i].Name.Equals(this.dgvHealth.SelectedRows[0].Cells[0].Value.ToString()))
                        {
                            /*
                             * 删除要删除的项并刷新相关控件
                             * */
                            item.Items.RemoveAt(i);
                            AddDgv();
                            CalcPrice();
                            this.lblPrices.Text = item.Price.ToString();
                            MessageBox.Show("删除成功!");
                            Btn();
                            return;
                        }
                    }
                    return;
                }
            }
        }
        public void Btn()
        {
            foreach (HealthCheckSet item in Set)
            {
                if (item.Name.Equals(cboExams.Text))
                {
                    /*
                     * 判断删除按钮的状态,如果集合里有数据为启用,无数据为禁用
                     * */
                    if (item.Items.Count > 0)
                    {
                        this.btnDel.Enabled = true;
                    }
                    else
                    {
                        this.btnDel.Enabled = false;
                    }
                    return;
                }
            }
            
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档