C#体检套餐项目

使用泛型集合写的一个小项目

  1.要实现新建体检套餐,并且如果已经有了该体检套餐就不能再次新建,

  2.要实现套餐列表动态更新,没添加一个体检套餐,在套餐列表里就自动添加一项;

  3.向当前套餐类表里添加检查项目,一个体检套餐里不可以有重复的体检项目;

  4.动态计算套餐当前价格;

  5.动态的将套餐列表当前套餐的体检项目显示在dgvlist中;

  6.实现删除体检项目: 

下面是实现的效果图:

新建体检套餐:

 

给体检套餐添加体检项目并计算套餐价格:

删除选中的体检项目:

在套餐列表中选择体检套餐可以查看具体的体检项目和套餐价格:

不能添加重复的套餐,每个套餐不能有重复的体检项目:

下面是代码:

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

namespace 体检套餐系统
{
    public class HC
    {
        //体检项目类
        public HC()
        {
        }
        //代参构造方法用于初始化成员变量
        public HC(string name, string desc, int price)
        {
            this.Name = name;
            this.Price = price;
            this.Desc = desc;
        }
        public string Name { get; set; }
        public string Desc { get; set; }
        public int Price { get; set; }
    }
}

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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //声明一个HC类型的集合,保存初始化后的体检项目
        List<HC> hc1 = new List<HC>()
        {
           new HC("身高","用于检查身高",12),
           new HC("体重","用于检查体重",10),
           new HC("肝功能","用于检查肝功能",50),
           new HC("B超","用于检查身体内部",120),
           new HC("心电图","用于检查心电图",150),
           new HC("听力","用于检查听力",20),
        };
        //声明一个双列集合,用于保存体检套餐,套餐名为key,体检项目为value;
        Dictionary<string, List<HC>> hc2 = new Dictionary<string, List<HC>>();

        private void Form1_Load(object sender, EventArgs e)
        {
            //把体检项目的名称绑定在下拉列表cbo2中
            foreach (HC item in hc1)
            {
                cbo2.Items.Add(item.Name);
            }
        }
        //将体检套餐的名称绑定在下拉列表cbo1中
        private void info()
        {
            cbo1.Items.Clear();
            foreach (string item in hc2.Keys)
            {
                cbo1.Items.Add(item);
            }
        }
        //新建体检套餐的方法
        private void 新建_Click(object sender, EventArgs e)
        {
            int error = 0;
            foreach (string item in hc2.Keys)
            {
                if (item == txt1.Text)
                {  error = 1;
                  
                }
            }
            if(txt1.Text!=""&&error!=1)
            {
                hc2.Add(txt1.Text, new List<HC>());
                info();
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if (error == 1)
            {
                MessageBox.Show("已经有该套餐了不能再次添加", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("套餐名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            
            }
        }
        //向选中的体检套餐添加体检项目的方法
        private void button2_Click(object sender, EventArgs e)
        {
            int error = 0;
            foreach (string item in hc2.Keys)
            {
                if (item == cbo1.Text)
                {
                    for (int i = 0; i < hc2[item].Count; i++)
                    {
                        if (hc2[item][i].Name == cbo2.Text)
                        {
                            error = 1;
                        }
                    }
                }
            }
            HC h = new HC();
            if (cbo1.Text != "" && cbo2.Text !=""&&error==0)
            {
                foreach (HC item in hc1)
                {
                    if (item.Name == cbo2.Text)
                    {
                        h = item;
                    }
                }
                foreach (string item in hc2.Keys)
                {
                    if (item == cbo1.Text)
                    {
                        hc2[item].Add(h);
                        
                    }
                }
            }
            else if (error == 1)
            {
                MessageBox.Show("不能有重复的体检项目", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("请补全体检套餐信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            info1();
        }
      
        private void cbo1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            info1();
        }
        //动态更新体检套餐的方法
        private void info1()
        {
            int money = 0;

            foreach (string item in hc2.Keys)
            {
                if (cbo1.SelectedItem.ToString() == item)
                {
                    for (int i = 0; i <hc2[item].Count; i++)
                    {
                        money += hc2[item][i].Price;
                    }
                    dataGridView1.DataSource = new BindingList<HC>(hc2[item]);
                    lb1.Text = cbo1.SelectedItem.ToString();
                    lb2.Text = money.ToString();
                }
            }
          
        }
        //删除的方法
        private void button3_Click(object sender, EventArgs e)
        {
          
                foreach (string item in hc2.Keys)
                {
                    if (item == cbo1.SelectedItem.ToString())
                    {
                        if (dataGridView1.SelectedRows.Count >= 0)
                        {
                            for (int i = 0; i < hc2[item].Count; i++)
                            {
                                if (hc2[item][i].Name == dataGridView1.SelectedRows[0].Cells[0].Value.ToString())
                                {
                                    DialogResult dr = MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                                    if (dr == DialogResult.Yes)
                                    {
                                        hc2[item].RemoveAt(i);
                                        info1();
                                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                    }
                                }
                            }
                        }
                    }
                   
            }
        }

        private void txt1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

 

 
posted @ 2016-08-28 11:50  大头杀马特  阅读(5311)  评论(23编辑  收藏  举报