C# 体检套餐管理系统
大家下午好呀~昨天写好一个体检套餐管理系统的项目作业 也是询问了大佬、完成的项目。欢迎大家评论区留言哦~
- 显示指定套餐的项目明细
- 向指定套餐添加检查项目信息
- 删除套餐中的项目信息
- 新建套餐
1、实现窗体搭建
这里就是我们要实现的功能,新增套餐成功了可以在套餐列表查看到新增的套餐名字,每个套餐有检查项目,你选择了哪个项目就可以添加到列表中,并且会显示套餐名和总价,可以对已经添加的项目进行删除。
2、创建体检项目维护系统中的检查项目类、体检套餐类
检查项目类(HealthCheckItem)
//检查项目类
class HealthCheckItem
{
//项目描述
private string description;
public string Description
{
get {
return description; }
set {
description = value; }
}
//项目名称
private string name;
public string Name
{
get {
return name; }
set {
name = value; }
}
//项目价格
private int price;
public int Price
{
get {
return price; }
set {
price = value; }
}
//带参构造方法
public HealthCheckItem(string name, int price, string description)
{
this.Name = name;
this.Description = description;
this.Price = price;
}
//无参构造方法
public HealthCheckItem()
{
}
体检套餐类(HealthCheckSet)
//体检套餐类
class HealthCheckSet
{
//HealthCheckItem的集合
private Dictionary<string,HealthCheckItem> items = new Dictionary<string,HealthCheckItem>();
public Dictionary<string,HealthCheckItem> Items
{
get {
return items; }
set {
items = value; }
}
//套餐名称
private string name;
public string Name
{
get {
return name; }
set {
name = value; }
}
//套餐价格 总价格
private int price;
public int Price
{
get {
return price; }
set {
price = value; }
}
public HealthCheckSet()
{
}
//有参构造方法
public HealthCheckSet(string name)
{
this.name = name;
}
//计算总价格
public void CalcPrice()
{
int totalPrice = 0;
foreach (HealthCheckItem item in this.items.Values)
{
totalPrice += item.Price;
}
this.Price = totalPrice;
}
3、查看、删除、添加体检套餐信息以及新建套餐(使用Dictionary<K,V>替换List实现功能)
public frmMain()
{
InitializeComponent();
}
//套餐列表的下拉事件
private void cbo_item_SelectedIndexChanged(object sender, EventArgs e)
{
refreshInfo();
}
public void refreshInfo()
{
lv_info.Items.Clear();
Dictionary<string,HealthCheckItem> item2 = lists[cbo_item.Text].Items;
foreach (HealthCheckItem item in item2.Values)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = item.Name;
lvi.SubItems.Add(item.Price.ToString());
lvi.SubItems.Add(item.Description);
lv_info.Items.Add(lvi);
}
//计算总价格
lists[cbo_item.Text].CalcPrice();
设置套餐名称
lab_name.Text = lists[cbo_item.Text].Name;
设置套餐总价
lab_price.Text = lists[cbo_item.Text].Price.ToString();
}
//储存所有套餐类
Dictionary<string, HealthCheckSet> lists = new Dictionary<string, HealthCheckSet>();
//采用泛型集合List保存所有的体检项目
Dictionary<string, HealthCheckItem> AllItems = new Dictionary<string, HealthCheckItem>();
//用于初始化所有套餐项
public void Init()
{
HealthCheckSet hcs = new HealthCheckSet("");
hcs.Name = "入学体检";
//hcs.Items.Add(new HealthCheckItem("身高", 5, "用于检查身高."));
// hcs.Items.Add(new HealthCheckItem("肝功能", 50, "用于检查肝功能."));
hcs.CalcPrice();
lists.Add(hcs.Name,hcs);
updateHealth();
AllItems.Add("身高", new HealthCheckItem("身高", 5, "用于检查身高."));
AllItems.Add("体重",new HealthCheckItem("体重", 5, "用于检查体重."));
AllItems.Add("肝功能",new HealthCheckItem("肝功能", 50, "用于检查肝功能."));
AllItems.Add("B超",new HealthCheckItem("B超", 30, "用于检查B超."));
foreach (HealthCheckItem item in AllItems.Values)
{
cbo_jiancha.Items.Add(item.Name);
}
}
//更新套餐
public void updateHealth()
{
cbo_item.Items.Clear();
foreach (HealthCheckSet item in lists.Values)
{
cbo_item.Items.Add(item.Name);
}
}
//增加套餐
private void btn_add_Click(object sender, EventArgs e)
{
string name = this.txt_name.Text;
HealthCheckSet add = new HealthCheckSet(name);
lists.Add(name,add);
updateHealth();
MessageBox.Show("添加成功!");
}
//窗体加载事件
private void frmMain_Load(object sender, EventArgs e)
{
this.lab_name.Text = "";
this.lab_price.Text = "";
Init();
}
private void btn_add2_Click(object sender, EventArgs e)
{
string name = cbo_jiancha.Text;
lists[cbo_item.Text].Items.Add(name, AllItems[name]);
refreshInfo();
}
//删除
private void btn_delete_Click(object sender, EventArgs e)
{
string name = lv_info.SelectedItems[0].Text;
foreach(HealthCheckItem item in lists[cbo_item.Text].Items.Values)
{
if (item.Name.Equals(name))
{
lists[cbo_item.Text].Items.Remove(item.Name);
refreshInfo();
return;
}
}
}
4、启动
这里就是今天的分享啦欢迎评论区留言~~