using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HealthCheck
{
/// <summary>
/// 检查项目
/// </summary>
public class HealthCheckItem
{
//项目名称
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 double price;
public double Price
{
get { return price; }
set { price = value; }
}
//方法重载
public HealthCheckItem(string name,string description,double price)
{
this.Name = name;
this.Price = price;
this.Description = description;
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HealthCheck { /// <summary> /// 体检套餐 /// </summary> public class HealthCheckSet { //套餐价格 private double price; //套餐价格 public double Price { get { return price; } set { price = value; } } //套餐名称 private string name; //套餐名称 public string Name { get { return name; } set { name = value; } } //集合 private List<HealthCheckItem> items; public List<HealthCheckItem> Items { get { return items; } } //方法重载 public HealthCheckSet() { items = new List<HealthCheckItem>(); } public HealthCheckSet(string name,List<HealthCheckItem> items) { this.Name = name; this.items = items; } //套餐价格计算方法 public void CalcPrice() { double totalPrice = 0; foreach (HealthCheckItem item in items) { totalPrice += item.Price; } this.price = totalPrice; } } }
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();
this.dgvHealth.AutoGenerateColumns = false;
}
//定义几个体检项目,身高,体重,听力,视力,肝功能,心电图,B超
HealthCheckItem height, weight, hearing, view, liver, heart, bWaves;
//定义一个默认的入学体检套餐
HealthCheckSet set;
//用List保存所有的体检项目
List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
//用List保存套餐中的体检项目
List<HealthCheckItem> items = new List<HealthCheckItem>();
//用Dictionary保存套餐集合
private Dictionary<string, HealthCheckSet> Health = new Dictionary<string, HealthCheckSet>();
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_Load(object sender, EventArgs e)
{
this.lblCheckList.Text = "";
this.lbltotalPrice.Text = "";
this.btnAdds.Enabled = false;
this.btnDelete.Enabled = false;
this.cboCheck.Text = "请选择";
//初始化检查项目
this.Add();
//初始化默认套餐
this.AddSet();
//初始下拉框套餐列表
this.AddCboSet();
}
/// 选择套餐列表下拉框事件
private void cboList_SelectedIndexChanged(object sender, EventArgs e)
{
string setName = cboList.Text;
if (setName == "请选择")
{
this.dgvHealth.DataSource = new BindingList<HealthCheckItem>();
lblCheckList.Text = "";
lbltotalPrice.Text = "";
return;
}
//设置套餐名称
lblCheckList.Text = this.Health[setName].Name;
//设置套餐总价
lbltotalPrice.Text = this.Health[setName].Price.ToString();
//更新套餐检查项目
UpdateSet(Health[setName]);
//设置删除按钮为可用状态
this.btnDelete.Enabled = true;
}
//更新套餐检查项目
private void UpdateSet(HealthCheckSet set)
{
this.dgvHealth.DataSource = new BindingList<HealthCheckItem>(set.Items);
}
/// 添加体检信息
public void Add()
{
height = new HealthCheckItem("身高","查身高",50);
weight = new HealthCheckItem("体重","查体重",22);
hearing = new HealthCheckItem("听力","查听力",33);
view = new HealthCheckItem("视力","查视力",44);
liver = new HealthCheckItem("肝功能","查肝功能",66);
heart = new HealthCheckItem("心电图","查心电图",100);
bWaves = new HealthCheckItem("B超","查B超",30);
AllItems.Add(height);//添加身高信息
AllItems.Add(weight);//添加体重信息
AllItems.Add(hearing);//添加听力信息
AllItems.Add(view);//添加视力信息
AllItems.Add(liver);//添加肝功能信息
AllItems.Add(heart);//添加心电图信息
AllItems.Add(bWaves);//添加B超信息
}
/// 设置默认套餐
public void AddSet()
{
//创建默认套餐对象
items = new List<HealthCheckItem>();
items.Add(height);
items.Add(weight);
items.Add(liver);
set = new HealthCheckSet("入学体检", items);
//计算套餐价格
set.CalcPrice();
this.Health.Add("入学体检",set);
}
/// 添加套餐按钮事件
private void btnAdd_Click(object sender, EventArgs e)
{
if (txtName.Text.Trim() == "")
{
MessageBox.Show("请输入套餐名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//创建套餐类对象
HealthCheckSet hs = new HealthCheckSet();
//将要添加的套餐名添加到Dictionary中
this.Health.Add(txtName.Text.Trim(),hs);
this.AddCboSet();
this.cboList.SelectedIndex = this.Health.Count;
MessageBox.Show("添加成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
//将套餐名称添加到下拉框
public void AddCboSet()
{
this.cboList.Items.Clear();
this.cboList.Items.Add("请选择");
foreach (string item in Health.Keys)
{
this.cboList.Items.Add(item);
}
this.cboList.SelectedIndex = 0;
}
/// <summary>
/// 添加套餐事件
private void btnAdds_Click(object sender, EventArgs e)
{
if (cboCheck.SelectedIndex == 0)
{
MessageBox.Show("请选择一个项目!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
//声明一个变量来接收套餐列表的文字
string cboList = this.cboList.Text;
if (cboList == "请选择")
{
MessageBox.Show("请选择套餐!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
//声明一个变量来接收检查项目下拉框的索引值
int index = this.cboCheck.SelectedIndex - 1;
//判断是否存在此项目并添加检查项目,重新计算价格和更新
if (!this.Health[cboList].Items.Contains(AllItems[index]))
{
//添加检查项目
this.Health[cboList].Items.Add(AllItems[index]);
//计算价格
this.Health[cboList].CalcPrice();
UpdateSet(Health[cboList]);
this.lblCheckList.Text = Health[cboList].Name;
this.lbltotalPrice.Text = Health[cboList].Price.ToString();
MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("该项目已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 删除按钮事件
private void btnDelete_Click(object sender, EventArgs e)
{
//声明一个临时变量来接收检查项目下拉框的值
string check = this.cboList.Text;
//判断是否选中DataGridView
if (this.dgvHealth.SelectedRows.Count == 0)
{
MessageBox.Show("请选择删除项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//获取选择的索引值
int index = this.dgvHealth.SelectedRows[0].Index;
//删除检查项目
this.Health[check].Items.RemoveAt(index);
//重新计算价格
this.Health[check].CalcPrice();
UpdateSet(Health[check]);
this.lblCheckList.Text = set.Name;
this.lbltotalPrice.Text = set.Price.ToString();
MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// 检查项目下拉框选择事件
private void cboCheck_SelectedIndexChanged(object sender, EventArgs e)
{
//如果下拉框选择的是轻选择添加项目的按钮不能使用
if (this.cboCheck.Text != "请选择")
{
this.btnAdds.Enabled = true;
}
else
{
this.btnAdds.Enabled = false;
}
}
}
}