zoukankan      html  css  js  c++  java
  • 体检套餐项目

    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 }
    复制代码
    复制代码
    namespace 体检套餐管理
    {
      public
      class HealthCheckSet
        {
            public HealthCheckSet()//初始化HealthCheckItems项的集合
            {
                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//表示HealthCheckItems中的项的集合
            {
                get { return items; }
                set { items = value; }
            }
            private int price;
            public int Price//套餐价格
            {
                get { return price; }
            }
            public void CalcPrice()//计算套餐价格的方法
            {
                int totalPrice = 0;
                foreach (HealthCheckItem item in items)
                {
                    totalPrice += item.Price;
                }
                this.price = totalPrice;
            }
        }
    }
    复制代码

    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("添加成功!");

    10:结束项目!

  • 相关阅读:
    提高代码质量:如何编写函数
    如何写自我评价
    写简历注意事项
    Android开发注意细节
    Android:onNewIntent()触发机制及注意事项
    Atom与markdown
    Java程序性能优化总结
    Java中的继承与组合
    Fragment生命周期总结
    C# 生成随机姓名
  • 原文地址:https://www.cnblogs.com/zhangzhenzhen/p/5373545.html
Copyright © 2011-2022 走看看