zoukankan      html  css  js  c++  java
  • 05章 体检套餐管理系统

    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;
     9             this.Price = price;
    10         }
    11         private string name;
    12         public string Name//项目名
    13         {
    14             get { return name; }
    15             set { name = value; }
    16         }
    17         private string description;
    18         public string Description//项目描述
    19         {
    20             get { return description; }
    21             set { description = value; }
    22         }
    23         private int price;
    24         public int Price//项目价格
    25         {
    26             get { return price; }
    27             set { price = value; }
    28         }
    29     }
    30 }
     1 namespace 体检套餐管理
     2 {
     3   public
     4   class HealthCheckSet
     5     {
     6         public HealthCheckSet()//初始化HealthCheckItems项的集合
     7         {
     8             items = new List<HealthCheckItem>();
     9         }
    10         public HealthCheckSet(string name, List<HealthCheckItem> items)//有项目时给集合赋值的构造函数
    11         {
    12             this.Name = name;
    13             this.items = items;
    14         }
    15         private string name;
    16         public string Name
    17         {
    18             get { return name; }
    19             set { name = value; }
    20         }
    21         private List<HealthCheckItem> items;
    22         public List<HealthCheckItem> Items//表示HealthCheckItems中的项的集合
    23         {
    24             get { return items; }
    25             set { items = value; }
    26         }
    27         private int price;
    28         public int Price//套餐价格
    29         {
    30             get { return price; }
    31         }
    32         public void CalcPrice()//计算套餐价格的方法
    33         {
    34             int totalPrice = 0;
    35             foreach (HealthCheckItem item in items)
    36             {
    37                 totalPrice += item.Price;
    38             }
    39             this.price = totalPrice;
    40         }
    41     }
    42 }

    3:定义两个List<T>单列集合,一个Dictionary<K,V>双列集合并且默认初始化一个体检套餐。

    1 HealthCheckItem height, weight, eyes, ears, gan, Bchao, heart;
    2         List<HealthCheckItem> allitems = new List<HealthCheckItem>();//保存所有的体检项目
    3         List<HealthCheckItem> items = new List<HealthCheckItem>();//保存套餐中的体检项目
    4         public Dictionary<string, HealthCheckSet> item = new Dictionary<string, HealthCheckSet>();//保存所有套餐
    5         public HealthCheckSet numA;

    4:初始化保存在allitems中的所有的体检项目以及默认套餐中的所有的体检项目。

    1         public void Show()//保存所有体检项目
     2         {
     3             height = new HealthCheckItem("身高", 5, "测量身高");
     4             weight = new HealthCheckItem("体重", 5, "检测体重");
     5             eyes = new HealthCheckItem("视力", 10, "测视力");
     6             ears = new HealthCheckItem("听力", 10, "测听力");
     7             gan = new HealthCheckItem("肝功能", 20, "测试肝功能");
     8             Bchao = new HealthCheckItem("B超", 20, "检测身体内部");
     9             heart = new HealthCheckItem("心电图", 50, "检查心脏");
    10             allitems.Add(height);
    11             allitems.Add(weight);
    12             allitems.Add(eyes);
    13             allitems.Add(ears);
    14             allitems.Add(gan);
    15             allitems.Add(Bchao);
    16             allitems.Add(heart);
    17         }
    18         public void Num1()//默认的体检套餐
    19         {
    20             items = new List<HealthCheckItem>();
    21             items.Add(height);
    22             items.Add(weight);
    23             items.Add(eyes);
    24             numA = new HealthCheckSet("入学体检", items);
    25             numA.CalcPrice();
    26             this.item.Add("入学体检", numA);
    27 
    28         }

    5:向下拉框中添加数据,由于此控件是套餐下拉框,其储存的数据是双列集合dictionary<K,V>中的key值,即代码如下

     1   public void Chushi()
     2         {
     3             this.comboBox1.Items.Clear();
     4             this.comboBox1.Items.Add("请选择");
     5 
     6             foreach (string key in this.item.Keys)
     7             {
     8                 this.comboBox1.Items.Add(key);
     9             }
    10             this.comboBox1.SelectedIndex = 0;
    11         }

    6:在下拉框的SelectedIndexChanged事件中编写如下代码,目的是点击下拉框选项后将数据加载到datagridview控件中,并且实现显示套餐名称和价格的功能。

     1 string text = this.comboBox1.Text;
     2             if (text == "请选择")
     3             {
     4                 this.label3.Text = "";
     5                 this.label5.Text = "";
     6                 this.dataGridView1.DataSource = new BindingList<HealthCheckItem>();
     7                 return;
     8             }
     9             label3.Text = this.item[text].Name;
    10             label5.Text = this.item[text].Price.ToString();
    11             load(item[text]);
    12             this.button2.Enabled = true;

    备注:load()作用是将所有的体检项目的信息加载到datagridview中

    1  public void load(HealthCheckSet set)//将所有的体检项目的信息加载到datagridview中
    2         {
    3             this.dataGridView1.DataSource = new BindingList<HealthCheckItem>(set.Items);
    4         }

    7:实现删除套餐中的项目的功能,setName是套餐下拉框中所有项的文本的集合。item[setName].Item是指HealthCheckItems中所有项的集合,datagridview控件中加载的也是HealthCheckItems项的集合,所以可以根据datagridview中被选中行的下表来删除集合中的数据。并且重新加载标签中的价格

     1  string setName = this.comboBox1.Text;
     2 
     3             if (this.dataGridView1.SelectedRows.Count == 0)
     4             {
     5                 MessageBox.Show("没有选择删除项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
     6                 return;
     7             }
     8             
     9             int index = this.dataGridView1.SelectedRows[0].Index;
    10            
    11             this.item[setName].Items.RemoveAt(index);
    12          
    13             this.item[setName].CalcPrice();
    14         
    15             load(item[setName]);
    16           
    17             this.label3.Text =numA.Name;
    18             this.label5.Text = numA.Price.ToString();
    19             MessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

    8:实现添加项目的操作,首先判断集合中是否已经存在要添加的项目了,如果要添加的项目不在集合中,则执行添加操作。其次,从allitems所有体检项目的集合中提取数据,使用foreach遍历,如果被选择的项目等于allitems中的项目,将此项目添加到HealthCheckItem项的集合中去,并且改变价格,刷新datagridview控件

     1 if (this.comboBox2.SelectedIndex == 0)
     2             {
     3                 MessageBox.Show("请选择一个项目。");
     4                 return;
     5             }
     6 
     7             string text = this.comboBox1.Text;
     8             if (text == "请选择")
     9             {
    10                 MessageBox.Show("请选择套餐!");
    11                 return;
    12             }
    13             
    14            
    15             foreach (HealthCheckItem cboitem in item[text].Items)
    16             {
    17                 if (cboitem.Name == comboBox2.SelectedItem.ToString())
    18                 {
    19                     MessageBox.Show("该项目存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    20                     return;
    21                 } 
    22             }
    23             foreach (HealthCheckItem addItem in allitems)
    24             {
    25                 if (addItem.Name == comboBox2.SelectedItem.ToString())
    26                 {
    27                     item[text].Items.Add(addItem);
    28                 }
    29                 this.item[text].CalcPrice();
    30                 load(this.item[text]);
    31                 this.label3.Text = this.item[text].Name;
    32                 this.label5.Text = this.item[text].Price.ToString();
    33 
    34                
    35             }

    9:实现添加套餐操作,通过添加dictionary<K,V>中的Key值来添加套餐,并且刷新下拉列表。并在套餐下拉框中自动定位到刚被添加的套餐。

    1  HealthCheckSet he = new HealthCheckSet();
    2             this.item.Add(textBox1.Text, he);
    3             this.Chushi();
    4             this.comboBox1.SelectedIndex = item.Count;
    5             MessageBox.Show("添加成功!");
  • 相关阅读:
    RHEL6.5安装QT5.4,设置环境变量
    Oprofile安装与使用探索
    龙芯3A上V8的编译与测试
    C#穿透session隔离———Windows服务启动UI交互程序 be
    C#获取CPU与网卡硬盘序列号及Base64和DES加密解密操作类 be
    C#读取Excel转换为DataTable be
    WPF DataGrid ScrollBar Style be
    C#操作注册表 be
    C#读取Excel转为DataTable be
    C# DataTable与Excel读取与导出 be
  • 原文地址:https://www.cnblogs.com/ainiaiwo/p/5373906.html
Copyright © 2011-2022 走看看