第五章 体检套餐

本文介绍了一个体检套餐管理系统的实现细节,包括系统中的主要类:体检项目(HealthCheckItem)和体检套餐(HealthCheckSet)。通过实例化这些类,可以创建不同的体检项目并组合成套餐,同时实现了动态增删体检项目及更新套餐总价的功能。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Day05_体检套餐管理系统
{
    class HealthCheckItem
    {
        public string Decription { get; set; }
        public string Name { get; set; }
        public int  Price { get; set; }

        public  HealthCheckItem()
        {

        }
        public  HealthCheckItem(string decription, string name, int price)
        {
            this.Decription = decription;
            this.Name = name;
            this.Price = price;
        }
    }

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Day05_体检套餐管理系统
{
    class HealthCheckSet
    {
        public List<HealthCheckItem> Items { get; set; }
        public string  Name { get; set; }
        public int  Price { get; set; }

        public HealthCheckSet()
        {

        }
        public  HealthCheckSet(string name,List<HealthCheckItem> items)//int price构两个函数
        {
            this.Items = items;
            this.Name = name;
          
        }



        public void CalcPrice()
        {
            int totalPrice = 0;
            foreach(HealthCheckItem item in this.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 Day05_体检套餐管理系统
{
    public partial class Form1 : Form
    {
        //采用泛型集合List保存所有的体检项目
        List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
        //采用泛型集合List保存套餐中的体检项目
        List<HealthCheckItem> items = new List<HealthCheckItem>();


        Dictionary<string, HealthCheckSet> HealthSet = new Dictionary<string, HealthCheckSet>();

        public Form1()
        {
            InitializeComponent();
        }

            //选择"套餐列表"下拉列表事件
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string setName = this.cboSets.Text;
            if (setName == "请选择")
            {
                this.dgvHealthList.DataSource = new BindingList<HealthCheckItem>();
                lalSetName.Text = "";
                lalSetPrice.Text = "";
                return;
            }
            //设置套餐名称
            lalSetName.Text=this.HealthSet[setName].Name;
            //设置套餐价格
            lalSetPrice.Text = this.HealthSet[setName].Price.ToString();
            //更新套餐检查项目
            UpdateSet(HealthSet[setName]);
            //设置“删除”按钮为可用状态
            this.btnDel.Enabled = true;
        }
        //更新套餐检查项目
        private void UpdateSet(HealthCheckSet set)
        {
            this.dgvHealthList.DataSource = new BindingList<HealthCheckItem>(set.Items);
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            string wantAdd = this.cboSets.Text;
            int index = this.cboSetTex.SelectedIndex ;
            MessageBox.Show(index.ToString());


            //添加操作的关键代码

            if (!this.HealthSet[wantAdd].Items.Contains(AllItems[index]))
            {

                //添加检查项目
                this.HealthSet[wantAdd].Items.Add(AllItems[index]);
                //重新计算总价格
                this.HealthSet[wantAdd].CalcPrice();
                //更新
                UpdateSet(this.HealthSet[wantAdd]);
                //刷新窗体集合A名称
                this.lalSetName.Text = this.HealthSet[wantAdd].Name;
                //刷新集合A价格
                this.lalSetPrice.Text = this.HealthSet[wantAdd].Price.ToString();
                MessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("该项目存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }




        public void liebiao()
        {
            HealthCheckItem height = new HealthCheckItem("身高", "用于检查身高", 20);
            HealthCheckItem weight = new HealthCheckItem("体重", "用于检查体重", 30);
            HealthCheckItem vision = new HealthCheckItem("视力", "用于检查视力", 50);
            HealthCheckItem hearing = new HealthCheckItem("听力", "用于检查听力", 50);
            HealthCheckItem function = new HealthCheckItem("肝功能", "用于检查肝功能", 100);
            HealthCheckItem Bultrasonography = new HealthCheckItem("B超", "用于检查B超", 200);
            HealthCheckItem electrocardiogram = new HealthCheckItem("心电图", "用于检查心电图", 300);
            items.Add(height);
            items.Add(weight);
            items.Add(vision);
            items.Add(hearing);
            items.Add(function);
            items.Add(Bultrasonography);
            items.Add(electrocardiogram);
            foreach (HealthCheckItem item in this.items)
            {
                this.cboSetTex.Items.Add(item.Name);
            }
            this.cboSetTex.SelectedIndex = 0;
        }

        private void cboSets_SelectedIndexChanged(object sender, EventArgs e)
        {
            string setName = this.cboSets.Text;
            if (setName == "请选择")
            {
                this.dgvHealthList.DataSource = new BindingList<HealthCheckItem>();
                label5.Text = "";
                label7.Text = "";
                return;
            }
            else
            {
                label5.Text = this.HealthSet[setName].Name;
                HealthSet[setName].CalcPrice();
                label7.Text = this.HealthSet[setName].Price.ToString();

                UpdateSet(HealthSet[setName]);

                this.btnDel.Enabled = true;
            }
        }






        public void Showruxue()
        {
            HealthCheckItem height = new HealthCheckItem("身高", "用于检查身高", 20);
            HealthCheckItem weight = new HealthCheckItem("体重", "用于检查体重", 30);
            HealthCheckItem vision = new HealthCheckItem("视力", "用于检查视力", 50);
            AllItems.Add(height);
            AllItems.Add(weight);
            AllItems.Add(vision);
            HealthCheckSet health = new HealthCheckSet("入学体检", AllItems);
            HealthSet.Add("入学体检", health);
            this.cboSets.Items.Add("请选择");
            this.cboSets.SelectedIndex = 0;
            foreach (var item in HealthSet)
            {
                this.cboSets.Items.Add(item.Key);
            }

        }



        private void Form1_Load(object sender, EventArgs e)
        {
            //启动
            liebiao();
            Showruxue();
        }

        //删除按键
        private void btnDel_Click(object sender, EventArgs e)
        {
            string sename = this.cboSets.Text;

            if (MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                int index = this.dgvHealthList.SelectedRows[0].Index;
                if (this.dgvHealthList.SelectedRows.Count > 0)
                {
                    this.HealthSet[sename].Items.RemoveAt(index);
                    label5.Text = this.HealthSet[sename].Name;
                    HealthSet[sename].CalcPrice();
                    label7.Text = this.HealthSet[sename].Price.ToString();
                    UpdateSet(HealthSet[sename]);
                }
                else
                {
                    MessageBox.Show("请选中一行", "提示");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sename = this.textBox1.Text;
            int count = this.cboSets.Items.Count;
            if (!HealthSet.Keys.ToList().Contains(sename))
            {
                List<HealthCheckItem> health = new List<HealthCheckItem>();
                HealthCheckSet hea = new HealthCheckSet(sename,health);
                HealthSet.Add(sename,hea);
                this.cboSets.Items.Add(sename);
                this.cboSets.SelectedIndex = count;
                UpdateSet(HealthSet[sename]);
            }
            else {
                MessageBox.Show("该套餐已经存在","提示");
            }
        }
    }
        }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值