字典集合Dictionary<K,V>和构造的应用==>>体检套餐项目

效果

首先,我们先来准备我们需要的类

1.检查项目类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第五章_体检套餐管理系统_
{
    //项目类
   public class HealthCheckItem
    {
       //项目描述
        public string  Description { get; set; }
       //项目名称
        public string  Name { get; set; }
       //项目价格
        public int Price { get; set; }

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

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

2.套餐类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第五章_体检套餐管理系统_
{

    //套餐类
   public class HealthCheckSet
    {
        //HealthCheckItem的集合
        public List<HealthCheckItem> Item { get; set; }

       //套餐价格
        public int Price { get; set; }

       //套餐名称
        public string Name { get; set; }

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

       //带参构造
        public HealthCheckSet(string name,List<HealthCheckItem> item)
        {
            this.Name = name;
            this.Item = item;
        }
        //计算总价格
        public void CalcPrice()
        {
            int totalPrice = 0;
            foreach (HealthCheckItem item in this.Item)
            {
                totalPrice += item.Price;
            }
            this.Price = totalPrice;
        }
    }
}

然后我们就可以来实现主页面的一些功能键了

1.主界面的初始工作

        //建立所有 检查项目 集合
       // List<HealthCheckItem> Alllist = new List<HealthCheckItem>();
        Dictionary<string, HealthCheckItem> Alllist = new Dictionary<string, HealthCheckItem>();

        //建立 套餐中的 检查项目 集合
        List<HealthCheckItem> list = new List<HealthCheckItem>();

        //使用字典保存套餐集合
        Dictionary<string, HealthCheckSet> dictionary = new Dictionary<string, HealthCheckSet>();

        //初始化检查项目
        HealthCheckItem item, item2, item3, item4, item5, item6, item7;

        //定义一个默认套餐
        HealthCheckSet moren;

        //初始化检查项目的方法
        public void main() 
        {
             item = new HealthCheckItem("身高","用于检查身高",10);
             item2 = new HealthCheckItem("体重","用于检查体重",5);
             item3 = new HealthCheckItem("视力","用于检查视力",15);
             item4 = new HealthCheckItem("听力","用于检查听力",20);
             item5 = new HealthCheckItem("肝功能","用于检查肝功能",100);
             item6 = new HealthCheckItem("B超","用于检查B超", 10);
             item7 = new HealthCheckItem("心电图","用于检查心电图",100);


             Alllist.Add(item.Name,item);
             Alllist.Add(item2.Name,item2);
             Alllist.Add(item3.Name,item3);
             Alllist.Add(item4.Name, item4);
             Alllist.Add(item5.Name,item5);
             Alllist.Add(item6.Name,item6);
             Alllist.Add(item7.Name,item7);


            //dictionary.Add(item.Name,item);

        }
      
        //生成默认套餐数据
        public void yuan() 
        {
            list.Add(item);
            list.Add(item2);
            list.Add(item3);

            moren = new HealthCheckSet("入学体检",list);
            //计算价格
            moren.CalcPrice();

            this.dictionary.Add("入学体检", moren);

        }

        //套餐列表 下拉框的加载方法
        public void combox() 
        {
            cbm_sum.Items.Clear();
            cbm_sum.Items.Add("--请选择--");
            foreach (string item in dictionary.Keys)
            {
                cbm_sum.Items.Add(item);
            }
            //默认第一项为选中
            cbm_sum.SelectedIndex = 0;
        }
        //检查项目 下拉框的加载方法
        public void combox2()
        {
            cmb_xiang.Items.Clear();
            cmb_xiang.Items.Add("--请选择--");
            foreach (string item in Alllist.Keys)
            {
                cmb_xiang.Items.Add(item);
            }
            //默认第一项为选中
            cmb_xiang.SelectedIndex = 0;
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            main(); //初始化检查项目
            yuan();  //生成默认套餐数据
            combox();  //套餐列表 下拉框的加载
            combox2();//检查项目 下拉框的加载

        }
   //填充套餐的DataGridView的方法
        public void UpdateSet(HealthCheckSet set) 
        {
            if (set.Item == null)
            {
                //给DataGridView的数据源赋空值
                dgv.DataSource = new BindingList<HealthCheckItem>();
                return;
            }
            else 
            {
                dgv.DataSource = new BindingList<HealthCheckItem>(set.Item);
            }
            
        }

 

2.添加套餐按钮

  //添加套餐
        private void but_add_Click(object sender, EventArgs e)
        {
            if (txt_name.Text!="")
            {
               //判断字典中是否有你想要添加的套餐
                if (dictionary.Keys.Contains(txt_name.Text))
                {
                   
                        MessageBox.Show("已经有该套餐了", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                        return;
                }
                    else
                    {
                        //给health实例化
                        List<HealthCheckItem> hao = new List<HealthCheckItem>();
                        HealthCheckSet health = new HealthCheckSet();
                        health.Item = hao;
                        health.Name = "";
                        health.Price = 0;
                        this.dictionary.Add(txt_name.Text, health);
                        combox();
                        cbm_sum.Text = txt_name.Text;
                        txt_name.Text = "";

                    }
            }
            else
            {
                MessageBox.Show("添加的不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
           
        }

3.选择(改变)套餐

  //选择套餐
        private void cbm_sum_SelectedIndexChanged(object sender, EventArgs e)
        {


            string setName = cbm_sum.Text;
            if (cbm_sum.Text=="--请选择--")
            {
                //给DataGridView的数据源赋空值
                dgv.DataSource = new BindingList<HealthCheckItem>();

                lab_xianshiname.Text = "";
                cmb_xiang.Text = "";
                lab_xianshiprice.Text = "";
                but_new.Enabled = false;
                return;

            }
            else
            {
                lab_xianshiname.Text = setName;
                if ( dictionary[setName]!=null)
                {
                    //根据套餐名给DataGridView绑定数据
                    UpdateSet(dictionary[setName]);
                }
                else
                {
                    //给DataGridView的数据源赋空值
                    dgv.DataSource = new BindingList<HealthCheckItem>();
                }
                
                //根据套餐名给其中的检查项求总价格
                lab_xianshiprice.Text = dictionary[setName].Price.ToString();
            }

           
        }

4.添加检查项目按钮

//添加项目
        private void but_new_Click(object sender, EventArgs e)
        {
            string name = cmb_xiang.Text;
            //根据你选择的套餐找到相应的项目集合,同时判断聚合中是否有你想要添加的项
            if (!dictionary[cbm_sum.Text].Item.Contains(Alllist[name]))//没有,添加
            {
                dictionary[cbm_sum.Text].Item.Add(Alllist[name]);
                MessageBox.Show("添加成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                dgv.DataSource = new BindingList<HealthCheckItem>(dictionary[cbm_sum.Text].Item);
                dictionary[cbm_sum.Text].CalcPrice();
                //根据套餐名给其中的检查项求总价格
                lab_xianshiprice.Text = dictionary[cbm_sum.Text].Price.ToString();
            }
            else//有,则提示
            {
                MessageBox.Show("已经有该项目的存在了", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
          
            
        }

5.删除按钮

        //删除
        private void but_shan_Click(object sender, EventArgs e)
        {
            string key = dgv.SelectedRows[0].Cells[1].Value.ToString();
            this.dictionary[cbm_sum.Text].Item.Remove(Alllist[key]);

            dgv.DataSource = new BindingList<HealthCheckItem>(dictionary[cbm_sum.Text].Item);
            but_shan.Enabled = false;//删除按钮的禁用
         
        }
public string name; //选中 private void dgv_CellClick(object sender, DataGridViewCellEventArgs e) { if (dgv.SelectedRows.Count!=1||dgv.SelectedRows[0].Cells[1].Value==null) { MessageBox.Show("请你正确的选择一行!","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error); return; } else { name = dgv.SelectedRows[0].Cells[1].Value.ToString(); but_shan.Enabled = true;//删除按钮的可用 } }

6.添加按钮的可用或禁用

 //添加按钮的 是否可用(检查项目下拉框的SelectedIndexChanged事件)
        private void cmb_xiang_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cmb_xiang.Text == "--请选择--"||cbm_sum.Text=="--请选择--")
            {
                 but_new.Enabled = false;//禁用
            }
            else
            {
                but_new.Enabled = true;//可用
            }
            
        }

这样,我们的项目就完成了,有一些可以优化的,忘大家别忘了,没有一样东西可以是永远的经典哟!

 

posted @ 2016-02-28 15:17  昵称加载失败~  阅读(871)  评论(0编辑  收藏  举报
返回顶部小火箭
欢迎来到我的博客世界。
看我七十二变,变
x