效果图如下:
实现的功能主要有以下几个方面:
- 显示指定套餐的项目明细
- 向指定套餐添加检查项目信息
- 删除套餐中的项目信息
- 新建套餐
具体代码及注释如下:
1:创建体检项目维护系统中的检查项目类(HealthCheckItem)、体检套餐类(HealthCheckSet)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 体检套餐管理系统
{
//检查项目类
class HealthCheckItem
{
private string description; //项目描述
private string name; //项目名称
private int price; //项目价格
public string Description { get => description; set => description = value; }
public string Name { get => name; set => name = value; }
public int Price { get => price; set => price = value; }
//带参构造函数
public HealthCheckItem(string name,int price,string dec)
{
this.Description = dec;
this.Name = name;
this.Price = price;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 体检套餐管理系统
{
//体检套餐类
class HealthCheckSet
{
private List<HealthCheckItem> items; //检查项目
private int price; //套餐价格,检查项目的价格之和
private string name; //套餐名称
public int Price { get => price; set => price = value; }
public string Name { get => name; set => name = value; }
public List<HealthCheckItem> Items { get => items; set => items = value; }
//套餐价格计算方法
public void CalcPrice()
{
int totalPrice = 0;
//循环累加体检项目的价格,获取套餐价格
foreach (HealthCheckItem item in this.items)
{
totalPrice += item.Price;
}
this.Price = totalPrice; //赋值给当前套餐价格
}
//无参构造实例化items
public HealthCheckSet()
{
items = new List<HealthCheckItem>();
}
//带参构造方法
public HealthCheckSet(string name,List<HealthCheckItem> items)
{
Name = name;
Items = items;
}
}
}
2:使用集合储存对应的数据,具体看代码和注释
注释一看就明白
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 体检套餐管理系统
{
public partial class FrmMadin : Form
{
//采用泛型集合List保存所有的体检项目
List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
//采用泛型集合List保存套餐中的体检项目
List<HealthCheckItem> items = new List<HealthCheckItem>();
//定义几个检查项目,用于初始化
HealthCheckItem a1, a2, a3, a4, a5;
//定义一个系统默认检查套餐“入学体检”
HealthCheckSet ruxue;
//用字典保存套餐集合
Dictionary<string,HealthCheckSet> Heal = new Dictionary<string,HealthCheckSet>();
public FrmMadin()
{
InitializeComponent();
}
//窗体加载事件
private void FrmMadin_Load(object sender, EventArgs e)
{
label6.Text = "";
label7.Text = "";
//调用绑定检查项目下拉框方法
BangDing();
//调用绑定检查套餐下拉框方法
TaoCanBang();
button2.Enabled = false;
button3.Enabled = false;
}
//删除事件
private void button3_Click(object sender, EventArgs e)
{
DelectSet();
}
//添加事件
private void button2_Click(object sender, EventArgs e)
{
TianJia();
}
//添加套餐事件
private void button1_Click(object sender, EventArgs e)
{
TianJaiTaoCan();
}
//初始化几个检查项目,并绑定下拉框
public void BangDing()
{
a1 = new HealthCheckItem("身高",5,"用于检查身高");
a2 = new HealthCheckItem("体重",5,"用于检查体重");
a3 = new HealthCheckItem("听力",15,"用于检查听力");
a4 = new HealthCheckItem("B超",30,"用于检查B超");
a5 = new HealthCheckItem("心电图",50,"用于检查心脏");
//添加到泛型集合中
AllItems.Add(a1);
AllItems.Add(a2);
AllItems.Add(a3);
AllItems.Add(a4);
AllItems.Add(a5);
//添加“请选择”
this.comboBox2.Items.Add("请选择");
//循环向CombBox控件中添加数据
foreach (HealthCheckItem item in AllItems)
{
comboBox2.Items.Add(item.Name);
}
//默认选中第一项
comboBox2.SelectedIndex = 0;
}
//初始化套餐,及绑定套餐列表
public void TaoCanBang()
{
//创建泛型集合,保存套餐中的体检项目
items = new List<HealthCheckItem>();
items.Add(a1);
items.Add(a2);
items.Add(a4);
//创建一个检查套餐对象
ruxue = new HealthCheckSet("入学体检",items);
//计算套餐价格
ruxue.CalcPrice();
//向套餐集合中添加一个检查套餐对象
Heal.Add("入学体检", ruxue);
//调用加载体检套餐下拉列表方法
JiaZaiTiJian();
}
//加载体检套餐下拉列表方法
public void JiaZaiTiJian()
{
//加载前,先清除下拉框数据
comboBox1.Items.Clear();
//绑定套餐下拉列表
comboBox1.Items.Add("请选择");
//循环向套餐下拉框中添加数据
foreach (string item in Heal.Keys)
{
comboBox1.Items.Add(item);
}
//默认选中第一项
comboBox1.SelectedIndex = 0;
}
/// <summary>
/// 绑定dataGridView,填充数据
/// </summary>
private void UpdateSet(HealthCheckSet hls)
{
//绑定dataGridView,填充数据
dataGridView1.DataSource = new BindingList<HealthCheckItem>(hls.Items);
}
//当套餐下拉框发生改变时,加载出相应的检查项目
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//接收下拉框控件中的文本
string cboName = comboBox1.Text;
if (cboName == "请选择")
{
dataGridView1.DataSource = null;
label6.Text = "";
label7.Text = "";
return;
}
//设置套餐名称,通过Key键访问套餐名称
label6.Text = Heal[cboName].Name;
//设置套餐价格,
label7.Text = Heal[cboName].Price.ToString();
//调用绑定dataGridView方法
UpdateSet(Heal[cboName]);
button3.Enabled = true;
button2.Enabled = true;
}
/// <summary>
/// 删除检查项目
/// </summary>
public void DelectSet()
{
//判断是否选中检查项目
if (dataGridView1.SelectedRows.Count > 0)
{
//获取用户选中的索引
int ioo = dataGridView1.SelectedRows[0].Index;
//用户选择的套餐文本
string name = comboBox1.Text;
//通过Key键,判断套餐,再通过套餐找到检查项目,最后通过索引删除检查项目。
Heal[name].Items.RemoveAt(ioo);
//重新计算价格
Heal[name].CalcPrice();
//重新绑定
UpdateSet(Heal[name]);
//重新接收总价格
label7.Text = Heal[name].Price.ToString();
}
else
{
MessageBox.Show("请选中一项检查项目");
}
}
/// <summary>
/// 添加检查项目方法
/// </summary>
public void TianJia()
{
//判断是否选中检查项目中的项
if (comboBox2.SelectedIndex == 0)
{
MessageBox.Show("请选择一个添加项目");
return;
}
//接收套餐名称
string cboSetText = comboBox1.Text;
//接收选中的检查项目的索引
int index = comboBox2.SelectedIndex - 1;
//判断要添加的检查项目,是否已经存在,存在返回true,不存在返回false
if (!this.Heal[cboSetText].Items.Contains(AllItems[index]))
{
//向该套餐中添加检查项目
this.Heal[cboSetText].Items.Add(AllItems[index]);
//重新计算价格
Heal[cboSetText].CalcPrice();
//设置套餐名称,通过Key键访问套餐名称
label6.Text = Heal[cboSetText].Name;
//设置套餐价格,
label7.Text = Heal[cboSetText].Price.ToString();
//重新绑定数据
UpdateSet(Heal[cboSetText]);
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("该检查项目已存在!");
}
}
//添加套餐方法
public void TianJaiTaoCan()
{
if (textBox1.Text.Equals(string.Empty))
{
MessageBox.Show("请输入套餐名称!");
return;
}
else
{
//循环判断要添加的套餐是否重复
foreach (string item in Heal.Keys)
{
if (item.Equals(textBox1.Text))
{
MessageBox.Show("套餐名称重复");
return;
}
}
//创建体检套餐类
HealthCheckSet hs = new HealthCheckSet();
//添加到泛型集合中
Heal.Add(textBox1.Text,hs);
//调用加载体检套餐下拉列表的方法
JiaZaiTiJian();
Heal[textBox1.Text].Name = textBox1.Text;
}
}
}
}