05章 体检套餐管理系统

窗体搭建的效果图如下:

 

 

一:实现的功能主要有以下几个方面:

①:显示指定套餐的项目明细

②:向指定套餐添加检查项目信息

③:删除套餐中的项目信息

④:新建套餐

 

二:创建体检项目维护系统中的检查项目类(HealthCheckItem)、体检套餐类(HealthCheckSet)

HealthCheckItem类中的属性说明如下:

Description:项目描述

Name:项目名称

Price:项目价格

 

HealthCheckSet类中的属性说明如下:

Items:HealthCheckItem的集合。采用泛型集合List<T>作为存储HealthCheckItem的数据结构

Price:套餐价格,即Items属性中检查项目的价格之和

Name:套餐名称

 

关键代码如下:

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

namespace Chapter05
{
   public  class HealthCheckItem
    {
      
       //项目名

       private string name;
       public string Name
       {
           get { return name; }
           set { name = value; }
       }

       //价格
       private int price;
       public int Price
       {
           get { return price; }
           set { price = value; }
       }

       //描述
       private string description;
       public string Description
       {
           get { return description; }
           set { description = value; }
       }


       public HealthCheckItem(string name,int price,string description)
       {
           this.Name = name;
           this.Price = price;
           this.Description = description;
       }

    }
}

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

namespace Chapter05
{
   public  class HealthCheckSet
    {
       //无参构造中实例化items
       public HealthCheckSet()
       {
           items = new List<HealthCheckItem>();
       }

       public HealthCheckSet(string name,List<HealthCheckItem>items)
       {
           this.Name = name;
           this.Items = items;
       }

       //项目名
       private string name;
       public string Name
       {
           get { return name; }
           set { name = value; }
       }

       //检查项目
       private List<HealthCheckItem> items;
       public List<HealthCheckItem> Items
       {
           get { return items; }
           set { items = value; }
       }

      

       //套餐价格
       private int price;
       public int Price
       {
           get { return price; }
           set { price = value; }
       }

       //套餐价格计算方法
       public void CalcPrice()
       {
           int totalPrice = 0;

           foreach (HealthCheckItem item in items)
           {
               totalPrice += item.Price;
           }
           this.price = totalPrice;
       }
      
    }
}

 

三:使用集合来存储对应的数据,因为套餐 ,体检项目有多个。定义一些容器来保存数据。

        //定义几个检查项目
        HealthCheckItem height, weight, sight, hearing, liverFun, ekg, bWaves, bloodPressure, bloodTest;

        //定义一个系统默认检查套餐“入学体检”
        HealthCheckSet setA;

        //保存所有体检项目
        List<HealthCheckItem> AllItems = new List<HealthCheckItem>();

        //保存套餐中的体检项目
        List<HealthCheckItem> items = new List<HealthCheckItem>();

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

四:初始化保存在AllItems中的所有体检项目

        //初始化检查项目下拉框值
        public void InitItems()
        {
            height = new HealthCheckItem("身高",5,"用于检查身高");
            weight = new HealthCheckItem("体重", 5, "用于检查体重.");
            sight = new HealthCheckItem("视力", 10, "用于检查视力.");
            hearing = new HealthCheckItem("听力", 10, "用于检查听力.");
            liverFun = new HealthCheckItem("肝功能", 50, "用于检查肝功能.");
            bWaves = new HealthCheckItem("B超", 30, "用于检查B超.");
            ekg = new HealthCheckItem("心电图", 50, "用于检查心电图.");
            bloodPressure = new HealthCheckItem("血压", 20, "用于检查血压.");
            bloodTest = new HealthCheckItem("血常规", 20, "用于检查血常规.");

            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.Items.Add("请选择");

            foreach (HealthCheckItem item in AllItems)
            {
                cboItems.Items.Add(item.Name);
            }
           
            //默认第一项被选中
            this.cboItems.SelectedIndex = 0;

        }

 

五:生成默认套餐数据 以及 加载体检套餐下拉列表

         //生成默认套餐数据
        public 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);
        
        }
        //加载体检套餐下拉列表
        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;


        }

 

六:在Load事件中调用方法

   private void frmMain_Load(object sender, EventArgs e)
        {
           this.lblSetName.Text = "";
            this.lblSetPrice.Text = "";

            //初始化所有检查项目
            InitItems();

            //初始化默认套餐
            InitSets();

            //加载下拉列表框
            InitHealthSetList();

            btnAdd.Enabled = false;
            btnDel.Enabled = false;

           this.dgvHealthList.AutoGenerateColumns = false;

        }

七:当套餐下拉框选择项发生改变时,需要加载所选套餐下对应的体检项目

       //填充套餐的DataGridView 
        private void UpdateSet(HealthCheckSet set)
        {
            dgvHealthList.DataSource = new BindingList<HealthCheckItem>(set.Items);

        }
        //套餐列表
        private void cboSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            string setName = this.cboSets.Text;
            if (setName == "请选择")
            {
                this.dgvHealthList.DataSource = null;
                lblSetName.Text = "";
                lblSetPrice.Text = "";
                return;
            
            }
            //设置套餐名称
            lblSetName.Text = this.HealthSet[setName].Name;

            //设置套餐总价
            lblSetPrice.Text = this.HealthSet[setName].Price.ToString();
          
            //更新套餐检查项目
            UpdateSet(HealthSet[setName]);

            //删除按钮为可用状态
            btnDel.Enabled = true;
        }

八:在DataGridView中添加项目。判断是否已经存在要添加的项目,若不在要添加项目的集合中,则执行添加操作

        //添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.cboItems.SelectedIndex==0)
            {
                MessageBox.Show("请选择一个项目!");
                return;
            }
            string cboSetText = this.cboSets.Text;
            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;

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

                MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("该项目已存在!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }
        }

 

 

九:实现删除体检套餐信息

        //删除项目
        private void btnDel_Click(object sender, EventArgs e)
        {
            string setNames = this.cboSets.Text;
            if (this.dgvHealthList.SelectedRows.Count == 0)
            {
                MessageBox.Show("没有选择删除项!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }

            //获取选中项目的索引
            int index = this.dgvHealthList.SelectedRows[0].Index;

            //删除检查项
            this.HealthSet[setNames].Items.RemoveAt(index);

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

            //更新DataGridView显示
            UpdateSet(HealthSet[setNames]);
            
            //重设标签显示
            lblSetName.Text = setA.Name;

            string cboSetText = this.cboSets.Text;

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

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

 

十:在套餐列表中添加套餐,通过添加dictionary<K,V>中的Key值来添加套餐,并刷新下拉列表。

        //添加套餐
        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();
                this.HealthSet.Add(this.txtHealthName.Text, Hch);

                this.InitHealthSetList();

                //向下拉框添加显示的内容
                this.cboSets.SelectedIndex = this.HealthSet.Count;
                lblSetName.Text = cboItems.Text;
                Hch.Name = cboSets.Text;

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

十一:未添加项目时添加按钮为禁用状态,添加时为可用

        //检查项目
        private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            if (this.cboSets.Text != "请选择")
            {
                this.btnAdd.Enabled = true;

            }
            else
            {
                this.btnAdd.Enabled = false;
            }

        }

 

posted @ 2016-04-08 20:49  Fighting`  阅读(381)  评论(0编辑  收藏  举报