1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HealthCheck
 8 {
 9     public class HealthCheckSet
10     {
11         public List<HealthCheckItems> Items { get; set; }
12         public string Name { get; set; }
13         public int Price { get; set; }
14 
15         public HealthCheckSet()
16         {
17             this.Items = new List<HealthCheckItems>();
18         }
19         /// <summary>
20         /// 计算总金额
21         /// </summary>
22         public void CalcPrice()
23         {
24             int totalPrice = 0;
25             foreach (HealthCheckItems item in this.Items)
26             {
27                 totalPrice += item.Price;
28             }
29             this.Price = totalPrice;
30         }
31         
32     }
33 }
套餐类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace HealthCheck
 8 {
 9     public class HealthCheckItems
10     {
11         public string Description { get; set; }
12         public string Name { get; set; }
13         public int Price { get; set; }
14 
15         /// <summary>
16         /// 方法重载
17         /// </summary>
18         /// <param name="description"></param>
19         /// <param name="name"></param>
20         /// <param name="price"></param>
21         public HealthCheckItems(string description,string name,int price)
22         {
23             this.Description = description;
24             this.Name = name;
25             this.Price = price;
26         }
27         public HealthCheckItems()
28         {
29         }
30 
31     }
32 }
体检项类
  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 HealthCheck
 12 {
 13     public partial class FrmMain : Form
 14     {
 15         public FrmMain()
 16         {
 17             InitializeComponent();
 18         }
 19         List<HealthCheckItems> Items;//声明Items集合用于保存套餐中的体检项
 20         List<HealthCheckItems> AllItems = new List<HealthCheckItems>();//声明AllItems集合,用于保存所有体检项
 21         HealthCheckSet Hcs;//声明HealthCheckSet对象
 22         //创建SetMeal字典
 23         public Dictionary<string, HealthCheckSet> SetMeal = new Dictionary<string, HealthCheckSet>();
 24 
 25         /// <summary>
 26         /// 初始化各体检项,套餐并将全部检查项绑定到cmbItem中
 27         /// </summary>
 28         public void InitItems()
 29         {
 30             HealthCheckItems hc1 = new HealthCheckItems("用于检查身高","身高",5);
 31             HealthCheckItems hc2 = new HealthCheckItems( "用于检查体重","体重", 5);
 32             HealthCheckItems hc3 = new HealthCheckItems( "用于检查视力","视力", 10);
 33             HealthCheckItems hc4 = new HealthCheckItems( "用于检查听力","听力", 10);
 34             HealthCheckItems hc5 = new HealthCheckItems( "用于检查肝功能","肝功能", 50);
 35             HealthCheckItems hc6 = new HealthCheckItems( "用于检查B超","B超", 30);
 36             AllItems.Add(hc1);
 37             AllItems.Add(hc2);
 38             AllItems.Add(hc3);
 39             AllItems.Add(hc4);
 40             AllItems.Add(hc5);
 41             AllItems.Add(hc6);
 42 
 43             for (int i = 0; i < AllItems.Count; i++)
 44             {
 45               cmbItem.Items.Add(AllItems[i].Name);
 46             }
 47 
 48             Items = new List<HealthCheckItems>();
 49             Items.Add(hc1);
 50             Items.Add(hc2);
 51             Items.Add(hc4);
 52             Items.Add(hc5);
 53             Hcs = new HealthCheckSet();
 54             Hcs.Items = Items;
 55             Hcs.Name = "入学体检";
 56             Hcs.CalcPrice();
 57             SetMeal.Add(Hcs.Name,Hcs);
 58             BindList();
 59         }
 60         /// <summary>
 61         /// 窗体加载
 62         /// </summary>
 63         /// <param name="sender"></param>
 64         /// <param name="e"></param>
 65         private void FrmMain_Load(object sender, EventArgs e)
 66         {
 67             InitItems();
 68         }
 69         /// <summary>
 70         /// 选中套餐之后加载所对应的各项,并更新视图
 71         /// </summary>
 72         /// <param name="sender"></param>
 73         /// <param name="e"></param>
 74         private void cmbSetMealList_SelectedIndexChanged(object sender, EventArgs e)
 75         {
 76             string Name = this.cmbSetMealList.Text;
 77             if (Name.Equals("请选择"))
 78             {
 79                 dgvHealthCheckItem.DataSource = new BindingList<HealthCheckItems>();
 80                 lblTcName.Text = "";
 81                 lblPrice.Text = "";
 82                 btnAdd.Enabled = false;
 83                 btnDelete.Enabled = false;
 84             }
 85             else          
 86             {
 87                 UpdateView(SetMeal[Name]);
 88                 btnDelete.Enabled = true;
 89 
 90             }
 91         }
 92         /// <summary>
 93         /// 更新视图
 94         /// </summary>
 95         /// <param name="Hes"></param>
 96         public void UpdateView(HealthCheckSet Hes)
 97         {
 98             dgvHealthCheckItem.DataSource = new BindingList<HealthCheckItems>(Hes.Items);
 99             cmbItem.SelectedIndex = 0;
100             lblTcName.Text = Hes.Name;
101             lblPrice.Text = Hes.Price.ToString();
102         }
103         /// <summary>
104         /// 绑定套餐类到cmbSetMealList中
105         /// </summary>
106         public void BindList()
107         {
108             this.cmbSetMealList.Items.Clear();
109             this.cmbSetMealList.Items.Add("请选择");
110             foreach (var Hc in SetMeal.Keys)
111             {
112                 cmbSetMealList.Items.Add(Hc);
113             }
114             cmbSetMealList.SelectedIndex = 0;
115         }
116         /// <summary>
117         /// 删除
118         /// </summary>
119         public void Delete()
120         {
121             //获取套餐名称
122             string Name = this.cmbSetMealList.Text;
123             //判断是否选中一行
124             if (dgvHealthCheckItem.SelectedRows.Count == 0)
125             {
126                 MessageBox.Show("请选中要删除的项", "提示");
127             }
128             else
129             {
130                 //获取选中项的索引
131                 int index = dgvHealthCheckItem.SelectedRows[0].Index;
132                 //删除选中项的集合
133                 SetMeal[Name].Items.RemoveAt(index);
134                 SetMeal[Name].CalcPrice();
135                 //更新DataGridView视图
136                 UpdateView(SetMeal[Name]);
137                 MessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK,
138 
139 MessageBoxIcon.Information);
140             }
141         }
142         /// <summary>
143         /// 单击删除
144         /// </summary>
145         /// <param name="sender"></param>
146         /// <param name="e"></param>
147         private void btnDelete_Click(object sender, EventArgs e)
148         {
149             Delete();
150         }
151         /// <summary>
152         /// 选中体检项时,添加按钮可用
153         /// </summary>
154         /// <param name="sender"></param>
155         /// <param name="e"></param>
156         private void cmbItem_SelectedIndexChanged(object sender, EventArgs e)
157         {
158             this.btnAdd.Enabled = true;
159         }
160         /// <summary>
161         /// 添加
162         /// </summary>
163         public void Insert()
164         {
165             //获取当前套餐名字
166             string Name = cmbSetMealList.Text;
167             if (cmbItem.SelectedIndex == -1)
168             {
169                 MessageBox.Show("请选择一个项目!");
170             }
171             else
172             {
173                 //获取选中项目的下标
174                 int index = cmbItem.SelectedIndex;
175                 //判断套餐中的项目集合是否有选择的项目
176                 if (!SetMeal[Name].Items.Contains(AllItems[index]))
177                 {
178                     //添加选择项目到套餐中
179                     SetMeal[Name].Items.Add(AllItems[index]);
180 
181                     //重新计算
182                     SetMeal[Name].CalcPrice();
183                     //更新视图
184                     UpdateView(SetMeal[Name]);
185                     lblPrice.Text = SetMeal[Name].Price.ToString();
186                     MessageBox.Show("添加成功!", "提示",
187 
188 MessageBoxButtons.OK, MessageBoxIcon.Information);
189                 }
190                 else
191                 {
192                     MessageBox.Show("该项目存在!", "提示",
193 
194 MessageBoxButtons.OK, MessageBoxIcon.Error);
195                 }
196             }
197 
198         }
199         /// <summary>
200         /// 单击添加
201         /// </summary>
202         /// <param name="sender"></param>
203         /// <param name="e"></param>
204         private void btnAdd_Click(object sender, EventArgs e)
205         {
206             Insert();
207         }
208         /// <summary>
209         /// 新建套餐
210         /// </summary>
211         public void NewSetMeal()
212         {
213             if (string.IsNullOrEmpty(txtSetMealName.Text.Trim()))
214             {
215                 MessageBox.Show("请输入套餐名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
216             }
217             else
218             {
219                  //获取新建套餐的名字
220                 string NewName = txtSetMealName.Text.Trim();
221                  //新建套餐类
222                  Hcs= new HealthCheckSet();
223                  Hcs.Name = NewName;
224                  //添加到套餐字典中
225                  SetMeal.Add(Hcs.Name, Hcs);
226                  //套餐选择框加载
227                  BindList();
228                  //默认选中新建套餐
229                  cmbSetMealList.SelectedIndex = SetMeal.Count;
230                  //更新视图
231                  UpdateView(SetMeal[NewName]);
232                  MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
233             }
234         }
235         /// <summary>
236         /// 单击新建套餐
237         /// </summary>
238         /// <param name="sender"></param>
239         /// <param name="e"></param>
240         private void btnInsert_Click(object sender, EventArgs e)
241         {
242             NewSetMeal();
243         }
244     }
245 }
Main

 

posted on 2017-04-09 21:13  Crazy丶忠祥  阅读(154)  评论(0编辑  收藏  举报