体检套餐管理系统 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;
            }

        }

时间: 2024-08-01 10:31:46

体检套餐管理系统 05章的相关文章

05章 体检套餐管理系统

窗体搭建的效果图如下: 一:实现的功能主要有以下几个方面: ①:显示指定套餐的项目明细 ②:向指定套餐添加检查项目信息 ③:删除套餐中的项目信息 ④:新建套餐 二:创建体检项目维护系统中的检查项目类(HealthCheckItem).体检套餐类(HealthCheckSet) HealthCheckItem类中的属性说明如下: Description:项目描述 Name:项目名称 Price:项目价格 HealthCheckSet类中的属性说明如下: Items:HealthCheckItem的

第五章.体检套餐管理系统.刁汉生.20170408

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace Le

第五章项目 体检套餐管理系统

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 体检

体检套餐管理系统

1:搭建窗体(略) 2:用户自定义类:HealthCheckItem和HealthCheckSet 1 namespace 体检套餐管理 2 { 3 public class HealthCheckItem 4 { 5 public HealthCheckItem(string name, int price, string description)//定义带参构造为三个属性赋初值 6 { 7 this.Name = name; 8 this.Description = description;

体检套餐管理系统的综合版

步骤: 定义几个类: HealthCheckItem类:检查项目 属性: public string Description { get; set; } public int Price { get; set; } public string Name { get; set; } HealthCheckItem类中的方法: //当选择套餐下拉框中的套餐时,套餐下所有检查项目都添加到dgvlist中显示 public HealthCheckItem(string name, int price,

第五章 体验套餐管理系统

体检套餐管理系统 套餐含义:体检套餐是一种形象的说法,体检的项目有很多,常规的项目就有几十种,个人体质.疾病的种类.体检者对体检项目的要求各不相同,全部的体检项目大概要几百种.普通的消费者不具备医学专业知识,所以也就无法从众多的体检项目中选择适合自己的体检项目.如果选择的项目过多,则浪费体检费用,选择的项目过少,又不能对需要检测的症状进行全面检测,很容易造成漏检.经过有多年经验的专家在对疾病预防经验的综合整理后,对体检项目进行科学分组,针对人群的年龄.体质.疾病类型.健康要求而制定的科学的体检项

体检套餐的项目

第5章 体检套餐管理系统1,课程内容回顾 第1章:深入.NET框架 .NET框架组件 CLR的作用及组成 .NET程序编译过程 .NET核心类库及命名空间 第2章 深入C#数据类型 类.对象.属性.封装 类图 结构 拆箱装箱 值类型和引用类型 静态成员和静态方法 第3章 使用集合组织相关数据 操作集合实现元素的增.删.查.遍历 ArrayList Hashtable 操作泛型集合实现元素的增.删.查.遍历 List<T> Dictionary<K,V> 泛型 第4章 深入类的方法

字典集合Dictionary&lt;K,V&gt;和构造的应用==&gt;&gt;体检套餐项目

效果 首先,我们先来准备我们需要的类 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;

第五章 套餐管理系统

第五章项目:体检套餐 窗体搭建的效果图如下: 一:实现的功能主要有以下几个方面: ①:显示指定套餐的项目明细 ②:向指定套餐添加检查项目信息 ③:删除套餐中的项目信息 ④:新建套餐 二:创建体检项目维护系统中的检查项目类(HealthCheckItem).体检套餐类(HealthCheckSet) HealthCheckItem类中的属性说明如下: Description:项目描述 Name:项目名称 Price:项目价格 HealthCheckSet类中的属性说明如下: Items:Healt