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


    该项目主要练习泛型集合List<T>和Dictionary<K,V>的用法,以该项目为依托,让我们对内存中数据的存在结构更加清晰

    效果

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

    提取出两个类,一个是  HealthCheckItem (检查项目类)HealthCheckSet(体检套餐类).

    首先我们要理清两个类的关系,一项套餐中可以包含多项项目检查.

    1.检查项目类

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

    例如:肝功能、用于检查肝功能、60

    然后写一个带参构造函数,对这三个字段赋值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Demo04体检项目
    {
       public  class HealthCheckItem
        {
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private string description;
    
            public string Description
            {
                get { return description; }
                set { description = value; }
            }
            private int price;
    
            public int Price
            {
                get { return price; }
                set { price = value; }
            }
            public HealthCheckItem(string name, string description, int price)
            {
                this.Name = name;
                this.Description = description;
                this.Price = price;
            }
          
        }
    }

    2.检查套餐类

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

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

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

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

    进行初始化

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

    体检项目变更时,需要调用CalcPrice()方法重新计算总价

     public void CalcPrice()
       {
           int totalPrice = 0;
           foreach (HealthCheckItem item in items)
           {
               totalPrice += item.Price; ;
    
           }
           this.price += totalPrice;
       }

    套餐类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Demo04体检项目
    {
       public  class HealthCheckSet
        {
            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 List<HealthCheckItem> items;
    
        public  List<HealthCheckItem> Items
        {
           get { return items; }
           set { items = value; }
         }
        public HealthCheckSet()
        {
            items = new List<HealthCheckItem>();
        }
       public HealthCheckSet(string name, List<HealthCheckItem> item)
        {
        this.Name = name;
        this.Items= item;
          }
       public void CalcPrice()
       {
           int totalPrice = 0;
           foreach (HealthCheckItem item in items)
           {
               totalPrice += item.Price; ;
    
           }
           this.price += totalPrice;
       }
        }
    }

    3.在窗体中做一些初始化工作

    我们需要使用集合来存储对应的数据,因为套餐 有多个,体检项目也有很多.定义一些容器来保存数据.

           //定义几个检查项目
            HealthCheckItem height,weight,sight,hearing,liverFun,ekg,bWaves,bloodPressure,bloodTest;
          
            //定义1个系统默认检查套餐"入学体检"
            HealthCheckSet setA;
    
            //保存所有的体检项目
            //List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
          public  Dictionary<string,HealthCheckItem> AllItems = new Dictionary<string,HealthCheckItem>();
            //保存套餐中的体检项目
            List<HealthCheckItem> items = new List<HealthCheckItem>();
    
            //使用字典保存套餐集合
            public Dictionary<string, HealthCheckSet> HealthSet = new Dictionary<string, HealthCheckSet>();

    4.初始化所有检查项目

     public void InitItems()
            {
                height = new HealthCheckItem("身高", "用于检查身高", 10);
                weight = new HealthCheckItem("体重", "用于检查体重", 5);
                sight = new HealthCheckItem("视力", "用于检查视力", 15);
                hearing = new HealthCheckItem("听力", "用于检查听力", 20);
                liverFun = new HealthCheckItem("肝功能", "用于检查肝功能", 100);
                ekg = new HealthCheckItem("B超", "用于检查B超", 10);
                bWaves = new HealthCheckItem("心电图", "用于检查心电图", 100);
                bloodPressure = new HealthCheckItem("血压", "用于检查血压", 20);
                bloodTest = new HealthCheckItem("血常规", "用于检查血常规", 20);
    
                AllItems.Add(height.Name, height);
                AllItems.Add(weight.Name,weight);
                AllItems.Add(sight.Name,sight);
                AllItems.Add(hearing.Name,hearing);
                AllItems.Add(liverFun.Name,liverFun);
                AllItems.Add(ekg.Name,ekg);
                AllItems.Add(bWaves.Name,bWaves);
                AllItems.Add(bloodPressure.Name,bloodPressure);
                AllItems.Add(bloodTest.Name,bloodTest);
            }

    5.生成默认套餐数据

            public void InitSets()
            {
                //创建1种默认套餐对象
                items = new List<HealthCheckItem>();
                items.Add(height);
                items.Add(weight);
                items.Add(liverFun);
    
                setA = new HealthCheckSet("入学体检", items);
                //计算套餐价格
                setA.CalcPrice();
                this.HealthSet.Add("入学体检", setA);
            }

      6.套餐列表 下拉框的加载方法

    public void InitHealthSetList()
            {
                //首先清空套餐下拉列表
                this.cboList.Items.Clear();
                //添加请选择
                this.cboList.Items.Add("请选择");
                //将Dictionary的key值绑定到combobox上,作为combobox的显示值
                foreach (string  key in this.HealthSet.Keys)
                {
                    this.cboList.Items.Add(key);
                }
    //默认第一项为选中
    this.cboList.SelectedIndex = 0; }

    7.检查项目 下拉框的加载方法

    public void HealthList()
            {
                cboxm.Items.Clear();
                cboxm.Items.Add("请选择");
                foreach (string item in AllItems.Keys)
                {
                    this.cboxm.Items.Add(item);
                }
                this.cboxm.SelectedIndex = 0;
            }

    8.在Load的事件里调用方法

    private void Form1_Load(object sender, EventArgs e)
            {
                //初始化所有检查项目,和套餐无关
                InitItems();
                //初始化默认套餐
                InitSets();
                //加载套餐下拉列表框
                InitHealthSetList();
                //加载项目下拉列表
                HealthList();
            }

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

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

    10.在DataGirdView中添加项目

     private void btnInsert_Click(object sender, EventArgs e)
            {
    
                if (this.cboxm.SelectedIndex == 0)
                {
                    MessageBox.Show("请选择一个项目");
                    return;
                }
                string cboset = this.cboxm.Text;
                if (cboset == "请选择")
                {
                    MessageBox.Show("请选择套餐");
                    return;
                }
                //int index = this.cboxm.SelectedIndex - 1;
                if (!this.HealthSet[cboList.Text].Items.Contains(AllItems[cboset]))
                {
                    this.HealthSet[cboList.Text].Items.Add(AllItems[cboset]);
                    this.HealthSet[cboList.Text].CalcPrice();
                    //dgvList.DataSource = new BindingList<HealthCheckItem>(this.HealthSet[cboList.Text].Items);
                    UpdateSet(this.HealthSet[cboList.Text]);
                    lblSetName.Text = this.HealthSet[cboList.Text].Name;
                    lblSetprice.Text = this.HealthSet[cboList.Text].Price.ToString();
    
                }
                else
                {
                    MessageBox.Show("已经有该项目的存在了", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            }

    11.在DataGirdView中删除项目

    private void btndelete_Click(object sender, EventArgs e)
            {
                string key = dgvList.SelectedRows[0].Cells[1].Value.ToString();
                this.HealthSet[cboList.Text].Items.Remove(AllItems[key]);
                dgvList.DataSource=new BindingList<HealthCheckItem>(this.HealthSet[cboList.Text].Items);
                btndelete.Enabled = false;//删除按钮的禁用
            }
    
            public string name;
            private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (dgvList.SelectedRows.Count != 1 || dgvList.SelectedRows[0].Cells[1].Value == null)
                {
                    MessageBox.Show("请你正确的选择一行!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    name = dgvList.SelectedRows[0].Cells[1].Value.ToString();
                    btndelete.Enabled = true;//删除按钮的可用
                }
            }

    12.在套餐列表中添加套餐

    private void btnAdd_Click(object sender, EventArgs e)
            {
    
                if (txtName.Text.Trim() == "")
                {
                    MessageBox.Show("请输入套餐名称!");
                    return;
                }
                else
                {
    
                    //声明一个数组
                    HealthCheckSet hch = new HealthCheckSet();
    
                    this.HealthSet.Add(this.txtName.Text, hch);
                    this.InitHealthSetList();
                    //向下拉框显示添加的内容
                    this.cboList.SelectedIndex = this.HealthSet.Count;
                    lblSetName.Text = cboxm.Text;
                    hch.Name = cboList.Text;
                    MessageBox.Show("添加成功!");
                }
            }

    13.未添加项目时添加按钮为禁用,添加时为可用

     private void cboxm_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (cboxm.Text == "请选择" || cboList.Text == "请选择")
                {
                    btnInsert.Enabled = false;//禁用
                }
                else
                {
                    btnInsert.Enabled = true;//可用
                }
            }

    最后呢,这个项目已经分析完了.

  • 相关阅读:
    世界十大黑客简介
    Workerman
    获取当前页面完整路径的方法:
    冒泡排序
    伪静态规则
    PHP如何打印出curl 模块交互的 http 请求与响应 header?
    转 浅谈 PHP 与手机 APP 开发(API 接口开发)
    银联chinapay支付接口实现详解
    PHP webservice 接口实例
    spl_autoload_register方法
  • 原文地址:https://www.cnblogs.com/jingpeipei/p/5368028.html
Copyright © 2020-2023  润新知