首页 > 代码库 > 第五章使用Dictionary替换List<t>实现功能

第五章使用Dictionary替换List<t>实现功能

    /// <summary>     /// 体检项目类     /// 保存一个体检项目包括项目名、描述、单价     /// 例如:肝功能、用于检察肝功能、60     /// </summary>    public  class HealthCheckItem2     {

       public HealthCheckItem2(string name, string description,decimal price)        {            this.Name = name;            this.Description = description;            this.Price = price;

       }

        private string name;         /// <summary>         /// 项目名称         /// </summary>         public string Name         {             get { return name; }             set { name = value; }         }         /// <summary>         /// 项目描述         /// </summary>         private string description;         public string Description         {             get { return description; }             set { description = value; }         }

        /// <summary>         /// 项目价格         /// </summary>         private decimal price;         public decimal Price         {             get { return price; }             set { price = value; }         }

    }

}

---------------------------------------------------------------------------------------------------------

    /// <summary>     /// 体检套餐类     /// 每个套餐包括要检查的项目、套餐名称、总价     /// </summary>    public  class HealthCheckSet2     {

         public HealthCheckSet2(string name, Dictionary<string, HealthCheckItem2> items)         {             this.Name = name;             this.Items = items;         }          public HealthCheckSet2()          {              items = new Dictionary<string, HealthCheckItem2>();          }

        /// <summary>         /// 套餐名称         /// </summary>         private string name;         public string Name         {             get { return name; }             set { name = value; }         }         /// <summary>         /// 检查项目         /// </summary>         private Dictionary<string, HealthCheckItem2> items;         public Dictionary<string, HealthCheckItem2> Items         {             get { return items; }             set { items = value; }         }         /// <summary>         /// 套餐价格         /// </summary>         private decimal price;         public decimal Price         {             get { return price; }             set { price = value; }            

        }         /// <summary>         /// 套餐计算方法         /// </summary>         public void CalcPrice()         {             int totalPrice = 0;             foreach (HealthCheckItem2 item in items.Values)             {                 totalPrice += (int)item.Price;             }             this.Price = totalPrice;                     }

    }

}

----------------------------------------------------------------------------------------------------------

namespace Admin {     public partial class btn_add : Form     {         public btn_add()         {             InitializeComponent();         }         //定义几个检查项目         HealthCheckItem2 thitem, thitem1, thitem2, thitem3, thitem4, thitem5, thitem6, thitem7, thitem8;         //定义1个系统默认检查套餐“入学体检”         HealthCheckSet2 kset;         //保存原始的 AllItems数据         Dictionary<string, HealthCheckItem2> AllItems = new Dictionary<string, HealthCheckItem2>();         //保存套餐中的Item数据         Dictionary<string, HealthCheckItem2> items = new Dictionary<string, HealthCheckItem2>();         //保存项目中的Item数据         Dictionary<string, HealthCheckItem2> head = new Dictionary<string, HealthCheckItem2>();

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

        public Dictionary<string, HealthCheckItem2> hast = new Dictionary<string, HealthCheckItem2>();

        /// <summary>         /// 创建所有检查项目,并保存在AllItems集合中         /// </summary>         public void InitItems()         {             thitem = new HealthCheckItem2("身高", "用于检查身高", 5);             thitem1 = new HealthCheckItem2("体重", "用于检查体重", 5);             thitem2 = new HealthCheckItem2("视力", "用于检查视力", 15);             thitem3 = new HealthCheckItem2("听力", "用于检查听力.", 10);             thitem4 = new HealthCheckItem2("肝功能", "用于检查肝功能",10);             thitem5 = new HealthCheckItem2("B超", "用于检查B超", 10);             thitem6 = new HealthCheckItem2("心电图", "用于检查心电图.", 10);             thitem7 = new HealthCheckItem2("血压", "用于检查血压.", 20);             thitem8 = new HealthCheckItem2("血常规", "用于检查血常规.", 20);             AllItems.Add(thitem.Name ,thitem);             AllItems.Add(thitem1.Name, thitem1);             AllItems.Add(thitem2.Name, thitem2);             AllItems.Add(thitem3.Name, thitem3);             AllItems.Add(thitem4.Name, thitem4);             AllItems.Add(thitem5.Name, thitem5);             AllItems.Add(thitem6.Name, thitem6);             AllItems.Add(thitem7.Name, thitem7);             AllItems.Add(thitem8.Name, thitem8);

        }         /// <summary>         /// 生成默认套餐数据         /// </summary>         public void InitSets()         {

            //创建1种默认套餐对象             items = new Dictionary<string, HealthCheckItem2>();             items.Add(thitem.Name,thitem);             items.Add(thitem1.Name,thitem1);             items.Add(thitem2.Name,thitem2);             kset = new HealthCheckSet2("贵宾", items);             //计算套餐价格             kset.CalcPrice();             this.HealthSet.Add("贵宾", kset);

        }

           

        /// <summary>         /// 加载体检套餐         /// </summary>         public void InitHealthSetList()         {             //清空套餐项             this.cmb_taocan.Items.Clear();             this.cmb_taocan.Items.Add("请选择");             foreach(string key in this.HealthSet.Keys)             {                 this.cmb_taocan.Items.Add(key);

            }             this.cmb_taocan.SelectedIndex = 0;

        }

        /// <summary>         /// 窗体加载事件         /// </summary>         /// <param name="sender"></param>         /// <param name="e"></param>         private void Frm_admin2_Load(object sender, EventArgs e)         {

            //禁止添加列             dgv_list.AutoGenerateColumns = false;             this.lbl_name.Text = "";             this.lbl_price.Text = "";             this.btn_insert1.Enabled = false;             this.btn_delete.Enabled = false;             //初始化所有检查项目             InitItems();             //初始化默认套餐             InitSets();             //加载套餐列表             InitHealthSetList();

        }

        /// <summary>         /// 填充套餐的DataGridView         /// </summary>         /// <param name="set"></param>         public void Updateset(HealthCheckSet2 set)         {             this.dgv_list.DataSource = new BindingList<HealthCheckItem2>(set.Items.Values.ToList());                    }         //选择“套餐”下拉列表更改时发生的事件         private void cmb_taocan_SelectedIndexChanged(object sender, EventArgs e)         {             string setname = this.cmb_taocan.Text;             if(setname=="请选择")             {                 this.dgv_list.DataSource = new BindingList<HealthCheckItem2>();                 lbl_name.Text = "";                 lbl_price.Text = "";                 return;

            }             //设置套餐名称             lbl_name.Text = this.HealthSet[setname].Name;             //设置套餐总价             lbl_price.Text = this.HealthSet[setname].Price.ToString();             //更新套餐检查项目             Updateset(HealthSet[setname]);             //设置删除按钮为“可用状态”             this.btn_delete.Enabled = true;

        }         //删除的事件         private void btn_delete_Click(object sender, EventArgs e)         {             string setname = this.cmb_taocan.Text;             if(this.dgv_list.SelectedRows.Count==0)             {                 MessageBox.Show("没有删除项","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);                 return;             }             //获取选中项的索引             int index = this.dgv_list.SelectedRows[0].Index;             //获取选中一行             string key = this.dgv_list.SelectedRows[0].Cells[0].Value.ToString();             //删除检查项             this.HealthSet[setname].Items.Remove(key);

           //重新计算价格             this.HealthSet[setname].CalcPrice();             //更新DaaGridView显示             Updateset(HealthSet[setname]);             //重设标签显示             this.lbl_name.Text = kset.Name;             this.lbl_price.Text = kset.Price.ToString();             MessageBox.Show("删除成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

        }         //根据是否选择体检项目,设置“添加”显示状态         private void cmb_xianmu_SelectedIndexChanged(object sender, EventArgs e)         {

            if (this.cmb_xianmu.Text != "请选择")             {                 this.btn_insert1.Enabled = true;             }             else             {                 this.btn_insert1.Enabled = false;

            }

        }         //添加检查项目         private void btn_insert1_Click(object sender, EventArgs e)         {             //输入判断             if(this.cmb_xianmu.SelectedIndex==0)             {                 MessageBox.Show("请选择一个项目");                 return;             }             string cmbsetText = this.cmb_taocan.Text;             if(cmbsetText=="请选择")             {                 MessageBox.Show("请选择套餐");                 return;             }             //添加操作             int index = this.cmb_xianmu.SelectedIndex - 1;             string add = this.cmb_xianmu.Text;             if (!this.HealthSet[cmbsetText].Items.Keys.ToList().Contains(add))             {                 //添加检查项                 this.HealthSet[cmbsetText].Items.Add(add,AllItems[add]);                 //重新计算总价格                 this.HealthSet[cmbsetText].CalcPrice();                 //更新                 Updateset(this.HealthSet[cmbsetText]);                 //刷新窗体集合名称                 this.lbl_name.Text = this.HealthSet[cmbsetText].Name;                               ////刷新集合价格                 this.lbl_price.Text = this.HealthSet[cmbsetText].Price.ToString();                 MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);             }             else             {                 MessageBox.Show("该项目已存在","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }         }

        //新建套餐

        private void button1_Click(object sender, EventArgs e)         {

            if (string.IsNullOrEmpty(this.txt_name.Text.Trim()))             {                 MessageBox.Show("请输入套餐名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                 return;                   }             HealthCheckSet2 Hch = new HealthCheckSet2();             this.HealthSet.Add(this.txt_name.Text.Trim(), Hch);             this.InitHealthSetList();             this.cmb_taocan.SelectedIndex = this.HealthSet.Count;             MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

       }

    }

}

 

第五章使用Dictionary替换List<t>实现功能