第5章体检套餐管理系统

namespace Admin {   


    // 体检项目类
    // 保存一个体检项目包括项目名、描述、单价
    // 例如:肝功能、用于检察肝功能、60

public class HealthCheckItem     {

  // 构造函数 

    public HealthCheckItem(string name,string deecription, decimal price) 

                this.Name = name;            

                this.Description = deecription; 

                  this.Price = price;

}   

//无参的构造函数   

public HealthCheckItem()    {         }       

              

     // 描述                    

 private string descripti

 public string Description  {get { return description; }

                                        set { description = value; }   

                                       }                                                       

      // 项目名称                                          

  private string name;       

  public string Name   {    get { return name; } 

                                  set { name value; }  } 

   // 价格  

private decimal price;        

public decimal Price  { get { return price;}

                               set { price = value;}   }   

    }

}

---------------------------------------------------------------------------------------------------------------

        // 体检套餐类
    // 每个套餐包括要检查的项目、套餐名称、总

   public  class HealthCheckSet     {  

// 体检套餐类       

public HealthCheckSet()        {            items = new List<HealthCheckItem>();        }      

  public HealthCheckSet(string name,List<HealthCheckItem> items)        {         

   this.Name = name;           

this.Items = items;        }

    private List<HealthCheckItem> items;       

public List<HealthCheckItem> Items       

  { get { return items; }           

  set { items = value; }         }           

// 套餐名称              

private string name;

  public string Name   {get { return name; } 

set { name = value; }         }  

//套餐价格            

private decimal price;       

  public decimal Price         {           

  get { return price; }            

set { price = value; }         }            

// 套餐价格计算方法          

public void CalcpRice()         {            

int totalprice = 0;            

foreach(HealthCheckItem item in items)  {  

totalprice += (int)item.Price;   }            

this.Price= totalprice;

  }       

}

---------------------------------------------窗体---------------------------------------------------------------

namespace Admin {   public partial class Form1 : Form     {      

   public Form1()         {           

  InitializeComponent();        

}

        //定义几个检查项目        

HealthCheckItem item, item1, item2, item3, item4, item5, item6, item7, item8;      

   //定义1个系统默认检查套餐“入学体检”        

HealthCheckSet seta;        

//保存套餐的提交项目        

public List<HealthCheckItem> kitems = new List<HealthCheckItem>();       

  //保存所有的体检项目       

  public List<HealthCheckItem> list = new List<HealthCheckItem>();

      //使用字典保存套餐集合       

  public Dictionary<string, HealthCheckSet> HealthSet = new Dictionary<string, HealthCheckSet>

     //添加项目名名称的方法

    public void InitItems()         {

      item = new HealthCheckItem("身高", "用于检查身高", 5);            

item1 = new HealthCheckItem("体重", "用于检查体重", 5);            

item2 = new HealthCheckItem("视力", "用于检查视力", 15);           

  item3 = new HealthCheckItem("听力", "用于检查听力.", 10);        

     item4 = new HealthCheckItem("肝功能", "用于检查肝功能",10);            

item5 = new HealthCheckItem("B超", "用于检查B超", 10);         

    item6 = new HealthCheckItem("心电图", "用于检查心电图.",10);           

  item7 = new HealthCheckItem("血压", "用于检查血压.", 20);           

  item8 = new HealthCheckItem("血常规", "用于检查血常规.", 20);          

   list.Add(item);            

list.Add(item1);           

  list.Add(item2);         

    list.Add(item3);          

   list.Add(item4);            

list.Add(item5);           

  list.Add(item6);           

list.Add(item7);            

list.Add(item8);

  }      

   //添加套餐列表的方法        

public void InitSets()         {            

//创建一种默认套餐对象           

  kitems = new List<HealthCheckItem>();            

kitems.Add(item);          

   kitems.Add(item1);          

   kitems.Add(item);          

   seta = new HealthCheckSet("入学体检", kitems);          

   //计算套餐价格            

seta.CalcpRice();            

this.HealthSet.Add("入学体检", seta);   }       

  /// <summary>       

  /// 加载体检套餐       

/// </summary>       

  public void InitHealthSetList()    {           

  this.cmb_taocan.Items.Clear();            

this.cmb_taocan.Items.Add("请选择");

  foreach (string key in this.HealthSet.Keys)      { 

this.cmb_taocan.Items.Add(key);             }            

this.cmb_taocan.SelectedIndex = 0;         }        

/// <summary>        

/// 加载事件        

/// </summary>        

/// <param name="sender"></param>        

/// <param name="e"></param>      

   private void Form1_Load(object sender, EventArgs e)         {         

    //禁止添加列            

dataGridView1.AutoGenerateColumns = false;          

   this.lbl_name.Text = "";          

   this.lbl_price.Text = "";           

  this.btn_insert1.Enabled = false;            

this.btn_delete.Enabled = false;            

//初始化所有检查项目           

  InitItems();            

//初始化默认套餐            

InitSets() ;            

//加载套餐列表           

  InitHealthSetList();       

        }        

/// <summary>        

/// 填充套餐的DataGridView        

/// </summary>        

/// <param name="set"></param>      

   private void UpdateSet(HealthCheckSet set)         {

    this.dataGridView1.DataSource = new BindingList<HealthCheckItem>(set.Items);         }

    //选择“套餐”下拉列表事件        

private void cmb_taocan_SelectedIndexChanged_1(object sender, EventArgs e)         {             string setName = this.cmb_taocan.Text;          

   if (setName == "请选择")             {             

    this.dataGridView1.DataSource = new BindingList<HealthCheckItem>();                 lbl_name.Text = "";      

           lbl_price.Text = "";             

    return;            

}          

   //设置套餐名称           

  lbl_name.Text = this.HealthSet[setName].Name;          

   //设置套餐总价           

  lbl_price.Text = this.HealthSet[setName].Price.ToString();     

        //更新套餐检查项目        

     UpdateSet(HealthSet[setName]);      

       //设置删除按钮为“可用状态”      

       this.btn_delete.Enabled = true;

        }

        //删除检查项目      

   private void btn_delete_Click(object sender, EventArgs e)         {

          

  string setName = this.cmb_taocan.Text;

           

if (this.dataGridView1.SelectedRows.Count == 0)          

   {               

  MessageBox.Show("没有选择删除项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);                 return;            

}          

   //获取选中检查项目的索引      

       int index = this.dataGridView1.SelectedRows[0].Index;   

          //删除检察项            

this.HealthSet[setName].Items.RemoveAt(index);         

    //重新计算价格           

  this.HealthSet[setName].CalcpRice();       

      //更新DataGridView显示      

       UpdateSet(HealthSet[setName]);     

        //重设标签显示    

         this.lbl_name.Text = seta.Name;          

   this.lbl_price.Text = seta.Price.ToString();       

      MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

      

  }

        //根据是否选择体检项目,设置“添加”显示状态    

     private void cmb_jiacha_SelectedIndexChanged(object sender, EventArgs e)         {          

   if (this.cmb_jiacha.Text != "请选择")             {           

      this.btn_insert1.Enabled = true;             }       

      else             {            

     this.btn_insert1.Enabled = false;             }

        }        

//“添加”检查项目        

private void btn_insert1_Click(object sender, EventArgs e)         {

            if (this.cmb_jiacha.SelectedIndex == 0)     

        {             

    MessageBox.Show("请选择一个项目。");        

         return;             }

        

    string cboSetText = this.cmb_taocan.Text;    

         if (cboSetText == "请选择")             {     

            MessageBox.Show("请选择套餐!");         

        return;             }            

//添加操作        

     int index = this.cmb_jiacha.SelectedIndex - 1;        

     if (!this.HealthSet[cboSetText].Items.Contains(list[index]))     

        {                 //添加检查项        

         this.HealthSet[cboSetText].Items.Add(list[index]);      

           //重新计算总价格               

  this.HealthSet[cboSetText].CalcpRice();               

  //更新                 UpdateSet(this.HealthSet[cboSetText]);       

          //刷新窗体集合A名称             

    this.lbl_name.Text = this.HealthSet[cboSetText].Name;      

           //刷新集合A价格               

  this.lbl_price.Text = this.HealthSet[cboSetText].Price.ToString();    

            

   MessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);             }             else      

       {               

  MessageBox.Show("该项目已存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

           }

        }

        //添加套餐       

  private void btn_insert_Click(object sender, EventArgs e)         {

            if(string.IsNullOrEmpty(this.txt_name.Text.Trim()))             {

   MessageBox.Show("请输入套餐名称","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);                 return;        

     }            

HealthCheckSet set = new HealthCheckSet();          

   this.HealthSet.Add(this.txt_name.Text.Trim(),set);

            this.InitHealthSetList();      

       this.cmb_taocan.SelectedIndex = this.HealthSet.Count;       

      MessageBox.Show("添加成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

        }

    }       

    }

原文地址:https://www.cnblogs.com/1-9-9-5/p/6686102.html