• 泛型实践——体检套餐系统


    体检套餐项目总结

    首先可以功能提取出两个类,一个是  HealthCheckItem (检查项目类)和HealthCheckItem(体检套餐类)。首先我们要理清两个类的关系,一项套餐中可以包含多项项目检查。

    HealthCheckItem:包括项目名称、单价和描述3个属性

     

          public HealthCheckItem(string name,int price,string description)

          {

              //this

              this.Name = name;

              this.Description = description;

              this.Price = price;

          }

    HealthCheckSet:包括要检查的项目、套餐名称、总价(所有要检查项目之和)

    所以这里我们就要考虑检查项目的类型:

    可以定义一个泛型集合来存储项目的类型,如List<HealthCheckItem>

    然后写2个构造函数对List<HealthCheckItem>

    进行初始化,如下

     

    最后该类还需要一个套餐总价计算方法

    //套餐计算方法

           public  void CalcPrice()

           {

               int totalPrice = 0;

               foreach (HealthCheckItem item in items)

               {

                   totalPrice += item.Price;

               }

               this.price =totalPrice;

           }

    窗体加载时需要初始化几个项目:

     

    添加套餐   绑定数据

     

      private void btnOK_Click(object sender, EventArgs e)

            {

                //添加

                if (string.IsNullOrEmpty(txtHealthName.Text))

                {

                    MessageBox.Show("请输入套餐名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return;//结束方法

                }

                else

                {

                    //声明一个套餐对象

                    HealthCheckSet Hch = new HealthCheckSet();

                    //将套餐对对象添加到Dictionary中

                    this.HealthSet.Add(this.txtHealthName.Text, Hch);

                    this.InitHealthSetList();

                    //下拉框显示刚添加的内容

                    this.cboSets.SelectedIndex = this.HealthSet.Count;

                    lblSetName.Text = cboSets.Text;

                    Hch.Name = cboSets.Text;

     

                    MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.txtHealthName.Text = string.Empty;

                    this.btnAdd.Enabled = true;

                    this.btnDel.Enabled = true;

                }

            }

            //绑定套餐到组合框

            public void InitHealthSetList()

            {

                //绑定前先清空

                this.cboSets.Items.Clear();

                //添加请选择

                this.cboSets.Items.Add("请选择");

                //将Dictionary的key值绑定到combobox上,作为combobox的显示值

                foreach (string key in this.HealthSet.Keys)

                {

                    this.cboSets.Items.Add(key);

                }

                //默认第一项被选中

                this.cboSets.SelectedIndex = 0;

                this.btnDel.Enabled = true;

            }

     

    //创建Item对象并保存到集合中

            public void InitItems()

            {

                height = new HealthCheckItem("身高", 6, "用于检查身高");

                weight = new HealthCheckItem("体重", 8, "用于检查体重.");

                sight = new HealthCheckItem("视力", 10, "用于检查视力.");

                hearing = new HealthCheckItem("听力", 12, "用于检查听力.");

                liverFun = new HealthCheckItem("肝功能", 50, "用于检查肝功能.");

                bWaves = new HealthCheckItem("B超", 40, "用于检查B超.");

                ekg = new HealthCheckItem("心电图", 50, "用于检查心电图.");

                bloodPressure = new HealthCheckItem("血压", 30, "用于检查血压.");

                bloodTest = new HealthCheckItem("血常规", 30, "用于检查血常规.");

     

                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);

                this.cboItems.SelectedIndex = 0;

            }

            //默认套餐

            private void InitSets()

            {

                //创建默认套餐对象

                items = new List<HealthCheckItem>();

                items.Add(height);

                items.Add(weight);

                items.Add(liverFun);

     

                setA = new HealthCheckSet("入学体检", items);

                //计算套餐价格

                setA.CalcPrice();

                this.HealthSet.Add("入学体检", setA);

     

            }

     

      //添加项目

     

            private void btnAdd_Click(object sender, EventArgs e)

            {

                string cboSetText = this.cboSets.Text;

                if (this.cboItems.SelectedIndex==0)

                {

                    MessageBox.Show("请选择一个项目");

                    return;

                }

                if (cboSetText == "请选择")

                {

                    MessageBox.Show("请选择套餐!");

                    return;

                }

                //这个要检查的项目是否在这个套餐里已经存在了   假设不存在  执行添加

                int index = this.cboItems.SelectedIndex - 1;

                if (!this.HealthSet[cboSetText].Items.Contains(AllItems[index]))

                {

                    this.HealthSet[cboSetText].Items.Add(AllItems[index]);

                    this.HealthSet[cboSetText].CalcPrice();

                    UpdateSet(this.HealthSet[cboSetText]);

                    this.lblSetName.Text = this.HealthSet[cboSetText].Name;  //刷新窗体集合A名称

                    this.lblSetPrice.Text = this.HealthSet[cboSetText].Price.ToString();    //刷新集合A价格

                    MessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                else

                {

                    MessageBox.Show("该项目存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

     

     

    //删除

     

            private void btnDel_Click(object sender, EventArgs e)

            {

                string setName = this.cboSets.Text;

                if (this.dgvHealthList.SelectedRows.Count == 0)

                {

                    MessageBox.Show("没有选择删除项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;

                }

                int index = this.dgvHealthList.SelectedRows[0].Index;

                this.HealthSet[setName].Items.RemoveAt(index);

                this.HealthSet[setName].CalcPrice();//重新计算价格

                UpdateSet(this.HealthSet[setName]);

                lblSetName.Text = setA.Name;

                string cboSetText = this.cboSets.Text;

                lblSetPrice.Text = this.HealthSet[cboSetText].Price.ToString();

                MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

               

            }

     

            }

    //当体检项目组合框某一个被选中时

            private void cboSets_SelectedIndexChanged(object sender, EventArgs e)

            {

                string setName = this.cboSets.Text;

                if (setName == "请选择")

                {

                    this.dgvHealthList.DataSource = new BindingList<HealthCheckItem>();

                    lblSetName.Text = "";

                    lblSetPrice.Text = "";

                    return;

                }

                lblSetName.Text = this.HealthSet[setName].Name;

                lblSetPrice.Text = this.HealthSet[setName].Price.ToString();

                UpdateSet(HealthSet[setName]);

                this.btnDel.Enabled = true;

            }   

            //更新套餐功能

            private void UpdateSet(HealthCheckSet set)

            {

                this.dgvHealthList.DataSource = new BindingList<HealthCheckItem>(set.Items);

            }

  • 相关阅读:
    10 个雷人的注释,就怕你不敢用!
    Java 14 之模式匹配,非常赞的一个新特性!
    poj 3661 Running(区间dp)
    LightOJ
    hdu 5540 Secrete Master Plan(水)
    hdu 5584 LCM Walk(数学推导公式,规律)
    hdu 5583 Kingdom of Black and White(模拟,技巧)
    hdu 5578 Friendship of Frog(multiset的应用)
    hdu 5586 Sum(dp+技巧)
    hdu 5585 Numbers
  • 原文地址:https://www.cnblogs.com/PGYXZ/p/4621632.html
Copyright © 2020-2023  润新知