using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace HealthCheck
{
public class HealthCheckSet
{
public List<HealthCheckItems> Items { get; set; }
public string Name { get; set; }
public int Price { get; set; } public HealthCheckSet()
{
this.Items = new List<HealthCheckItems>();
}
/// <summary>
/// 计算总金额
/// </summary>
public void CalcPrice()
{
int totalPrice = ;
foreach (HealthCheckItems item in this.Items)
{
totalPrice += item.Price;
}
this.Price = totalPrice;
} }
}

套餐类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace HealthCheck
{
public class HealthCheckItems
{
public string Description { get; set; }
public string Name { get; set; }
public int Price { get; set; } /// <summary>
/// 方法重载
/// </summary>
/// <param name="description"></param>
/// <param name="name"></param>
/// <param name="price"></param>
public HealthCheckItems(string description,string name,int price)
{
this.Description = description;
this.Name = name;
this.Price = price;
}
public HealthCheckItems()
{
} }
}

体检项类

 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 HealthCheck
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
List<HealthCheckItems> Items;//声明Items集合用于保存套餐中的体检项
List<HealthCheckItems> AllItems = new List<HealthCheckItems>();//声明AllItems集合,用于保存所有体检项
HealthCheckSet Hcs;//声明HealthCheckSet对象
//创建SetMeal字典
public Dictionary<string, HealthCheckSet> SetMeal = new Dictionary<string, HealthCheckSet>(); /// <summary>
/// 初始化各体检项,套餐并将全部检查项绑定到cmbItem中
/// </summary>
public void InitItems()
{
HealthCheckItems hc1 = new HealthCheckItems("用于检查身高","身高",);
HealthCheckItems hc2 = new HealthCheckItems( "用于检查体重","体重", );
HealthCheckItems hc3 = new HealthCheckItems( "用于检查视力","视力", );
HealthCheckItems hc4 = new HealthCheckItems( "用于检查听力","听力", );
HealthCheckItems hc5 = new HealthCheckItems( "用于检查肝功能","肝功能", );
HealthCheckItems hc6 = new HealthCheckItems( "用于检查B超","B超", );
AllItems.Add(hc1);
AllItems.Add(hc2);
AllItems.Add(hc3);
AllItems.Add(hc4);
AllItems.Add(hc5);
AllItems.Add(hc6); for (int i = ; i < AllItems.Count; i++)
{
cmbItem.Items.Add(AllItems[i].Name);
} Items = new List<HealthCheckItems>();
Items.Add(hc1);
Items.Add(hc2);
Items.Add(hc4);
Items.Add(hc5);
Hcs = new HealthCheckSet();
Hcs.Items = Items;
Hcs.Name = "入学体检";
Hcs.CalcPrice();
SetMeal.Add(Hcs.Name,Hcs);
BindList();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_Load(object sender, EventArgs e)
{
InitItems();
}
/// <summary>
/// 选中套餐之后加载所对应的各项,并更新视图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbSetMealList_SelectedIndexChanged(object sender, EventArgs e)
{
string Name = this.cmbSetMealList.Text;
if (Name.Equals("请选择"))
{
dgvHealthCheckItem.DataSource = new BindingList<HealthCheckItems>();
lblTcName.Text = "";
lblPrice.Text = "";
btnAdd.Enabled = false;
btnDelete.Enabled = false;
}
else
{
UpdateView(SetMeal[Name]);
btnDelete.Enabled = true; }
}
/// <summary>
/// 更新视图
/// </summary>
/// <param name="Hes"></param>
public void UpdateView(HealthCheckSet Hes)
{
dgvHealthCheckItem.DataSource = new BindingList<HealthCheckItems>(Hes.Items);
cmbItem.SelectedIndex = ;
lblTcName.Text = Hes.Name;
lblPrice.Text = Hes.Price.ToString();
}
/// <summary>
/// 绑定套餐类到cmbSetMealList中
/// </summary>
public void BindList()
{
this.cmbSetMealList.Items.Clear();
this.cmbSetMealList.Items.Add("请选择");
foreach (var Hc in SetMeal.Keys)
{
cmbSetMealList.Items.Add(Hc);
}
cmbSetMealList.SelectedIndex = ;
}
/// <summary>
/// 删除
/// </summary>
public void Delete()
{
//获取套餐名称
string Name = this.cmbSetMealList.Text;
//判断是否选中一行
if (dgvHealthCheckItem.SelectedRows.Count == )
{
MessageBox.Show("请选中要删除的项", "提示");
}
else
{
//获取选中项的索引
int index = dgvHealthCheckItem.SelectedRows[].Index;
//删除选中项的集合
SetMeal[Name].Items.RemoveAt(index);
SetMeal[Name].CalcPrice();
//更新DataGridView视图
UpdateView(SetMeal[Name]);
MessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
/// 单击删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
Delete();
}
/// <summary>
/// 选中体检项时,添加按钮可用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbItem_SelectedIndexChanged(object sender, EventArgs e)
{
this.btnAdd.Enabled = true;
}
/// <summary>
/// 添加
/// </summary>
public void Insert()
{
//获取当前套餐名字
string Name = cmbSetMealList.Text;
if (cmbItem.SelectedIndex == -)
{
MessageBox.Show("请选择一个项目!");
}
else
{
//获取选中项目的下标
int index = cmbItem.SelectedIndex;
//判断套餐中的项目集合是否有选择的项目
if (!SetMeal[Name].Items.Contains(AllItems[index]))
{
//添加选择项目到套餐中
SetMeal[Name].Items.Add(AllItems[index]); //重新计算
SetMeal[Name].CalcPrice();
//更新视图
UpdateView(SetMeal[Name]);
lblPrice.Text = SetMeal[Name].Price.ToString();
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("该项目存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} }
/// <summary>
/// 单击添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
Insert();
}
/// <summary>
/// 新建套餐
/// </summary>
public void NewSetMeal()
{
if (string.IsNullOrEmpty(txtSetMealName.Text.Trim()))
{
MessageBox.Show("请输入套餐名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//获取新建套餐的名字
string NewName = txtSetMealName.Text.Trim();
//新建套餐类
Hcs= new HealthCheckSet();
Hcs.Name = NewName;
//添加到套餐字典中
SetMeal.Add(Hcs.Name, Hcs);
//套餐选择框加载
BindList();
//默认选中新建套餐
cmbSetMealList.SelectedIndex = SetMeal.Count;
//更新视图
UpdateView(SetMeal[NewName]);
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
/// 单击新建套餐
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnInsert_Click(object sender, EventArgs e)
{
NewSetMeal();
}
}
}

Main

S2T40,第五章-LMLPHP

05-11 22:10