体检套餐管理项目

1.体检套餐管理项目页面

2:用户自定义类:HealthCheckItem和HealthCheckSet

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace FrmPhysical
 8 {
 9    public  class HealthCheckItem    //检查项目类
10     {
11         public string  Description { get; set; }//项目描述
12         public string  Name { get; set; }//项目名称
13         public int  Price { get; set; }//项目价格
14 
15         //无参构造函数
16         public HealthCheckItem()
17         {
18         }
19 
20         //带参构造函数
21         public HealthCheckItem(string name, string description, int price)
22         {
23             this.Name = name;
24             this.Description = description;
25             this.Price = price;
26 
27         }
28 
29     }
30 }
namespace 体检套餐管理
{
     public class HealthCheckItem
    {
        public HealthCheckItem(string name, int price, string description)//定义带参构造为三个属性赋初值
        {
            this.Name = name;
            this.Description = description;
            this.Price = price;
        }
        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; }
        }
    }
}

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace FrmPhysical
 8 {
 9     public class HealthCheckSet   //体检套餐类
10     {
11         //HealthCheckItem的集合
12         public List<HealthCheckItem> Item { get; set; }
13 
14         //套餐价格
15         public int Price { get; set; }
16 
17         //套餐名称
18         public string  Name { get; set; }
19 
20         //无参构造
21         public HealthCheckSet() { }
22 
23         //带参构造函数
24         public HealthCheckSet(string name,List<HealthCheckItem> item) 
25         {
26             this.Name = name;
27             this.Item = item;
28         }
29 
30         //计算总价格
31         public void CalcPrice()
32         {
33             int totaPrice = 0;
34             foreach (HealthCheckItem  item in this.Item )
35             {
36                 totaPrice += item.Price;
37             }
38             this.Price = totaPrice;
39         }
40 
41 
42     }
43 }
 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 }

 

 

  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 FrmPhysical
 12 {
 13     public partial class FrmPhysical : Form
 14     {
 15         public FrmPhysical()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         private void FrmPhysical_Load(object sender, EventArgs e)
 21         {
 22             #region 主窗体调用方法
 23             main();//初始化检查项目的方法
 24             yuan();//生成默认套餐数据
 25             combox();//套餐列表 下拉框的加载方法
 26             combox2(); //检查项目 下拉框的加载方法
 27            
 28             
 29             #endregion
 30         }
 31         #region  初始化检查项目的方法
 32          //初始化检查项目的方法
 33 
 34         //建立所有 检查项目 集合
 35         Dictionary<string, HealthCheckItem> AllList = new Dictionary<string, HealthCheckItem>();
 36 
 37         //初始化检查项目
 38         HealthCheckItem item, item2, item3, item4, item5, item6, item7;
 39         public void main()
 40         {//调用检查项目类的带参构造函数
 41             item = new HealthCheckItem("身高","用于检查身高",5);
 42             item2 = new HealthCheckItem("体重", "用于检查体重",5);
 43             item3 = new HealthCheckItem("视力", "用于检查视力",15);
 44             item4 = new HealthCheckItem("听力", "用于检查听力",20);
 45             item5 = new HealthCheckItem("肝功能", "用于检查肝功能",50);
 46             item6 = new HealthCheckItem("B超", "用于检查B超",30);
 47             item7 = new HealthCheckItem("心电图", "用于检查心电图",100);
 48 
 49             //所有检查项目 集合
 50             AllList.Add(item.Name,item);
 51             AllList.Add(item2.Name, item2);
 52             AllList.Add(item3.Name, item3);
 53             AllList.Add(item4.Name, item4);
 54             AllList.Add(item5.Name, item5);
 55             AllList.Add(item6.Name, item6);
 56             AllList.Add(item7.Name, item7);
 57         }
 58         #endregion
 59 
 60         #region 生成默认套餐数据
 61         //生成默认套餐数据
 62 
 63         //建立 套餐中的 检查项目 集合
 64         List<HealthCheckItem> List = new List<HealthCheckItem>();
 65 
 66         //使用字典保存套餐集合
 67         Dictionary<string, HealthCheckSet> dictionary = new Dictionary<string, HealthCheckSet>();
 68 
 69         //定义一个默认套餐
 70         HealthCheckSet moren;
 71         public void yuan()
 72         {
 73             //套餐中的 检查项目 集合
 74             List.Add(item);
 75             List.Add(item2);
 76             List.Add(item3);
 77             //定义一个默认套餐
 78             moren = new HealthCheckSet("入学体检",List);
 79             //调用方法    定义一个默认套餐.计算价格
 80             moren.CalcPrice();
 81             //使用字典保存套餐集合
 82             this.dictionary.Add("入学体检", moren);
 83             
 84 
 85         }
 86 
 87         #endregion
 88 
 89         #region  套餐列表 下拉框的加载方法
 90         public void combox()
 91         {
 92             cboList.Items.Clear();
 93             cboList.Items.Add("--请选择--");
 94             foreach (string  item in dictionary .Keys )
 95             {
 96                 cboList.Items.Add(item);
 97             }
 98             cboList.SelectedIndex = 0;//默认第一项为选择
 99         }
100         #endregion
101 
102         #region 检查列表 下拉框的加载方法
103         public void combox2()
104         {
105             cboProject.Items.Clear();
106             cboProject.Items.Add("--请选择--");
107             foreach (string item in AllList.Keys )
108             {
109                 cboProject.Items.Add(item);
110             }
111             cboProject.SelectedIndex = 0;//默认第一项为选中
112         }
113         #endregion
114 
115         #region  填充套餐的DataGridView
116         public void Updateset(HealthCheckSet set)
117         {
118             if (set.Item == null)
119             {
120                 //给DataGridView的数据源赋空值
121                 dataGridView1.DataSource = new BindingList<HealthCheckItem>();
122                 return;
123             }
124             else
125             {
126                 dataGridView1.DataSource = new BindingList<HealthCheckItem>(set.Item);
127             }
128         }
129         #endregion
130         public const string CAPTION = "操作提示";
131         private void btnAdd_Click(object sender, EventArgs e)
132         {
133             #region 添加套餐
134             if (txtName.Text != "")
135             {
136                 if (dictionary.Keys.Contains(txtName.Text))
137                 {
138                     MessageBox.Show("已经有该套餐了", CAPTION, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
139                     return;
140                 }
141                 else
142                 {
143                     //给health实例化
144                     List<HealthCheckItem> A = new List<HealthCheckItem>();
145                     HealthCheckSet health = new HealthCheckSet();
146                     health.Item = A;
147                     health.Name = "";
148                     health.Price = 0;
149                     this.dictionary.Add(txtName.Text, health);
150                     combox();
151                     cboList.Text = txtName.Text;
152                     txtName.Text = "";
153 
154                 }
155             }
156             else
157             {
158                 MessageBox.Show("添加的不能为空!",CAPTION,MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
159 
160             }
161 
162             #endregion
163         }
164 
165         private void btnDelete_Click(object sender, EventArgs e)
166         {
167             #region 删除
168             string text = this.cboList.Text;
169             string key = dataGridView1.SelectedRows[0].Cells["名称"].Value.ToString();            
170             this.dictionary[cboList.Text].Item.Remove(AllList[key]);
171             dictionary[text].CalcPrice();
172             label2.Text = dictionary[text].Price.ToString();
173             dataGridView1.DataSource = new BindingList<HealthCheckItem>(dictionary[cboList.Text].Item);
174             btnDelete.Enabled = false;//删除按钮的禁用
175             #endregion
176         }
177         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
178         {
179 
180         }
181 
182         private void btnAdder_Click(object sender, EventArgs e)
183         {
184             #region 下面 添加项目
185             string name = cboProject.Text;
186             if (!dictionary[cboList.Text].Item.Contains(AllList[name]))
187             {
188                 dictionary[cboList.Text].Item.Add(AllList[name]);
189                 MessageBox.Show("添加成功!", CAPTION, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
190                 dataGridView1.DataSource = new BindingList<HealthCheckItem>(dictionary[cboList.Text].Item);
191                 dictionary[cboList.Text].CalcPrice();
192                 //根据套餐名给其中的检查项求总价格
193                 label2.Text = dictionary[cboList.Text].Price.ToString();
194             }
195             else
196             {
197                 MessageBox.Show("已经有该项目的存在了",CAPTION,MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
198             }
199             #endregion
200         }
201         private void cboProject_SelectedIndexChanged(object sender, EventArgs e)
202         {
203             #region 添加按钮的  是否可用
204             if (cboProject.Text == "--请选择--" || cboList.Text == "--请选择--")
205             {
206                 btnAdder.Enabled = false;
207             }
208             else
209             {
210                 btnAdder.Enabled = true;
211             }
212             #endregion
213         }
214         public string name;
215         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
216         {
217             #region 选中
218             if (dataGridView1.SelectedRows.Count != 1 || dataGridView1.SelectedRows[0].Cells[1].Value == null)
219             {
220                 MessageBox.Show("请你正确的选择一行!", CAPTION, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
221                 return;
222             }
223             else 
224             {
225                 name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
226                 btnDelete.Enabled = true;//删除按钮的可用
227                
228             }
229             #endregion
230         }
231 
232         private void cboList_SelectedIndexChanged(object sender, EventArgs e)
233         {
234             #region 选择套餐
235             string setName = cboList.Text;
236             if (cboList.Text == "--请选择--")
237             {
238                 //
239                 dataGridView1.DataSource = new BindingList<HealthCheckItem>();
240                 label1.Text = "";
241                 cboProject.Text = "";
242                 label2.Text = "";
243                 btnAdder.Enabled = false;
244                 return;
245             }
246             else
247             {
248                 label1.Text = setName;
249                 if (dictionary[setName] != null)
250                 {
251                     //根据套餐名给DataGridView绑定数据
252                     Updateset(dictionary[setName]);
253                 }
254                 else
255                 {
256                     //给DataGridView的数据源赋空值
257                     dataGridView1.DataSource = new BindingList<HealthCheckItem>();
258 
259                 }
260                 //根据套餐名给其中的检查项求总价格
261                 label2.Text = dictionary[setName].Price.ToString();
262             }
263             #endregion
264         }
265 
266        
267     }
268 }

 

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

 

posted @ 2016-04-10 09:40  吴玄坤  阅读(354)  评论(0编辑  收藏  举报