using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 深入.NET___第五章____体检套餐项目
{
public class Health
{
public string Name { get; set; }
public string Description { get; set; }
public int Price { get; set; }
public Health()
{
}
public Health(string name, string ds, int price)
{
this.Name = name;
this.Description = ds;
this.Price = price;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 深入.NET___第五章____体检套餐项目
{
public class CheckSet
{
public string Name { get; set; }
public int Price { get; set; }
public List<Health> items { get; set; }
public CheckSet()
{
items = new List<Health>();
}
public CheckSet(string name, List<Health> item)
{
this.Name = name;
this.items = item;
}
public void CalPrice()
{
int total = 0;
foreach (Health h in items)
{
total = total + h.Price;
}
this.Price = total;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 深入.NET___第五章____体检套餐项目
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
//采用泛型集合保存 所有 的体检项目
List<Health> AllItems = new List<Health>();
//采用泛型集合保存 套餐 中的的体检项目
List<Health> items = new List<Health>();
//使用字典保存套餐集合
public Dictionary<string, CheckSet> HealthSet = new Dictionary<string, CheckSet>();
Health sg, ti, gan, B, shili;
CheckSet set = new CheckSet();
private void label4_Click(object sender, EventArgs e)
{
}
private void FrmMain_Load(object sender, EventArgs e)
{
this.label4.Text = "";
this.label5.Text = "";
ADD();
Student();
}
public void ADD()
{
sg = new Health("身高", "检查身高", 5);
ti = new Health("体重", "检查体重", 5);
gan = new Health("肝功能", "检查肝功能", 50);
B = new Health("B超", "检查大肚子", 30);
shili = new Health("视力", "检查视力", 50);
AllItems.Add(sg);
AllItems.Add(ti);
AllItems.Add(gan);
AllItems.Add(B);
AllItems.Add(shili);
// this.dataGridView1.DataSource = new BindingList<Health>(AllItems);
this.comboBox2.Items.Add("请选择");
foreach (Health item in AllItems)
{
comboBox2.Items.Add(item.Name);
}
this.comboBox2.SelectedIndex = 0;
}
public void Student()
{
items = new List<Health>();
items.Add(sg);
items.Add(ti);
items.Add(gan);
set = new CheckSet("入学体检", items);
//计算套餐价格
set.CalPrice();
this.HealthSet.Add("入学体检", set);
//清空套餐下拉列表
this.comboBox1.Items.Clear();
//添加请选择
this.comboBox1.Items.Add("请选择");
//将 Dictionary的Key值 绑定到COMBOX1
foreach (string key in HealthSet.Keys)
{
this.comboBox1.Items.Add(key);
}
//默认选择第一项
this.comboBox1.SelectedIndex = 0;
}
public void UpdateSet(CheckSet set)
{
this.dataGridView1.DataSource = new BindingList<Health>(set.items);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string sNAME = this.comboBox1.Text;
if (sNAME == "请选择")
{
this.dataGridView1.DataSource = null;
this.label4.Text = "";
this.label5.Text = "";
return;
}
//设置套餐名称
this.label4.Text = this.HealthSet[sNAME].Name;
//设置套餐总价
this.label5.Text = this.HealthSet[sNAME].Price.ToString();
UpdateSet(HealthSet[sNAME]);
}
private void button2_Click(object sender, EventArgs e)
{
if (this.comboBox2.SelectedIndex == 0)
{
MessageBox.Show("请选择一个项目");
return;
}
//声明一个套餐来接受 套餐 列表的文字
string CBO = comboBox1.Text;
if (CBO == "请选择")
{
MessageBox.Show("请选择套餐!");
return;
}
// 判断现有套餐是否存在
int index = this.comboBox2.SelectedIndex - 1;
if (!this.HealthSet[CBO].items.Contains(AllItems[index]))
{
this.HealthSet[CBO].items.Add(AllItems[index]);
this.HealthSet[CBO].CalPrice();
UpdateSet(this.HealthSet[CBO]);
//刷新窗体集合名称
this.label4.Text = this.HealthSet[CBO].Name;
//刷新窗体集合的 价格
this.label5.Text = this.HealthSet[CBO].Price.ToString();
MessageBox.Show("添加成功!!!", "提示");
}
else
{
MessageBox.Show("该项目已存在", "提示");
}
}
//删除
private void button3_Click(object sender, EventArgs e)
{
string setName = this.comboBox1.Text;
if (this.dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("没有选择删除项");
return;
}
//获取选中的索引
int index = this.dataGridView1.SelectedRows[0].Index;
this.HealthSet[setName].items.RemoveAt(index);
//重新计算价格
this.HealthSet[setName].CalPrice();
UpdateSet(HealthSet[setName]);
this.label4.Text = set.Name;
this.label5.Text = set.Price.ToString();
MessageBox.Show("删除成功");
}
public void AddSet()
{
this.comboBox1.Items.Clear();
this.comboBox1.Items.Add("请选择");
foreach (string item in HealthSet.Keys)
{
this.comboBox1.Items.Add(item);
}
this.comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
// this.comboBox1
if (this.textBox1.Text == "")
{
MessageBox.Show("请输入要添加的套餐名称");
return;
}
CheckSet se = new CheckSet();
this.HealthSet.Add(this.textBox1.Text, se);
this.AddSet();
// this.comboBox1.Items.Clear();
//this.comboBox1.Items.Add("请选择");
// foreach (string item in HealthSet.Keys)
// {
// this.comboBox1.Items.Add(item);
// }
this.comboBox1.SelectedIndex = this.HealthSet.Count;
MessageBox.Show("添加成功");
}
}
}