public Form1()
{
InitializeComponent();
}
//用于保存所有的单个项目
HealthCheckItem height, weight, sight, hearing, liverFun;
//AllItems用于保存所有的套餐项目
Dictionary<string, HealthCheckItem> AllItems = new Dictionary<string, HealthCheckItem>();
//定义一个套餐
HealthChechSet set;
//定义一个套餐内的套餐项目列表
Dictionary<string, HealthCheckItem> item = new Dictionary<string, HealthCheckItem>();
//定义一个套餐集合
Dictionary<string, HealthChechSet> allset = new Dictionary<string, HealthChechSet>();
//用于初始化所有套餐项
public void allIe()
{
height = new HealthCheckItem("用于检查身高", "身高", 50);
weight = new HealthCheckItem("用于检查体重", "体重", 60);
sight = new HealthCheckItem("用于检查视力", "视力", 10);
hearing = new HealthCheckItem("用于检查听力", "听力", 10);
liverFun = new HealthCheckItem("用于检查肝功能", "肝功能", 50);
AllItems.Add(height.Name, height);
AllItems.Add(weight.Name, weight);
AllItems.Add(sight.Name, sight);
AllItems.Add(hearing.Name, hearing);
AllItems.Add(liverFun.Name, liverFun);
}
//初始化一个套餐
public void InitHealth()
{
item.Add(height.Name, height);
item.Add(weight.Name, weight);
set = new HealthChechSet("入学体检", item);
set.CqlcPrice();
allset.Add("入学体检", set);
}
//将初始化套餐信息绑定到下拉框中
public void Iniset() {
this.cboSets.Items.Clear();
this.cboSets.Items.Add("请选择");
foreach (string item in allset.Keys)
{
this.cboSets.Items.Add(item);
}
this.cboSets.SelectedIndex = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
this.lblPrice.Text = "";
this.lblNamen.Text = "";
allIe();
InitHealth();
Iniset();
}
//刷新重新加载
public void UpdateSet(HealthChechSet set)
{
this.dgvList.DataSource = new BindingList<HealthCheckItem>(set.Items.Values.ToList());
}
//选择套餐
private void cboSets_SelectedIndexChanged(object sender, EventArgs e)
{
string name = this.cboSets.Text;
if (name == "请选择")
{
this.dgvList.DataSource = new BindingList<HealthCheckItem>();
this.lblPrice.Text = "";
this.lblNamen.Text = "";
return;
}
this.lblNamen.Text = name;
this.lblPrice.Text = allset[name].Price.ToString();
UpdateSet(allset[name]);
}
//添加套餐项目
private void btnAdd_Click(object sender, EventArgs e)
{
if (this.cboxiang.SelectedIndex == 0)
{
MessageBox.Show("请选择套餐项");
return;
}
string name = this.cboSets.Text;
if (name == "请选择")
{
MessageBox.Show("请选择套餐");
return;
}
if (!allset[name].Items.Keys.ToList().Contains(this.cboxiang.Text))
{
allset[name].Items.Add(this.cboxiang.Text, AllItems[this.cboxiang.Text]);
allset[name].CqlcPrice();
this.lblNamen.Text = name;
this.lblPrice.Text = allset[name].Price.ToString();
UpdateSet(allset[name]);
}
else
{
MessageBox.Show("该项已经存在!");
}
}
//添加套餐
private void btnAddName_Click(object sender, EventArgs e)
{
if (this.txtAddName.Text.Trim() != null && this.txtAddName.Text.Trim() != "")
{
HealthChechSet set = new HealthChechSet();
allset.Add(this.txtAddName.Text.Trim(), set);
Iniset();
this.cboSets.SelectedIndex = allset.Count;
}
else
{
MessageBox.Show("请输入添加的套餐名称");
}
}
//移除套餐项目
private void btnDetele_Click(object sender, EventArgs e)
{
string name = this.dgvList.SelectedRows[0].Cells["_Name"].Value.ToString();
string healthname = this.cboSets.Text;
allset[healthname].Items.Remove(name);
UpdateSet(allset[healthname]);
}
//项目类
public class HealthCheckItem
{
private string _description; //项目描述
private string _name; //项目名称
private int _price; //项目价格
public HealthCheckItem(string Description, string Name, int Price)
{
this.Description = Description;
this.Name = Name;
this.Price = Price;
}
#region 属性
public string Description
{
get { return _description; }
set { _description = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Price
{
get { return _price; }
set { _price = value; }
}
#endregion
}
//套餐类
public class HealthChechSet
{
private Dictionary<string,HealthCheckItem> _items; //存储HealthCheckItem集合
private int _price; //套餐价格
private string _name; //套餐名称
public HealthChechSet(string name, Dictionary<string,HealthCheckItem> _items)
{
this.Name = name;
this.Items = _items;
}
public HealthChechSet()
{
_items=new Dictionary<string,HealthCheckItem>();
}
public void CqlcPrice()
{
int totalPrice = 0;
foreach (HealthCheckItem item in this.Items.Values)
{
totalPrice += item.Price;
}
this._price = totalPrice;
}
#region 属性
public Dictionary<string,HealthCheckItem> Items
{
get { return _items; }
set { _items = value; }
}
public int Price
{
get { return _price; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
#endregion
}