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)
        {

        }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM