
using System;
using System.Collections.Generic;
using System.Text;
namespace ExaminationList
{
public class HealthCheckItem
{
public HealthCheckItem(string name, int price, string description)
{
this.Name = name;
this.Price = price;
this.Description = description;
}
public HealthCheckItem()
{
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string description;
public string Description
{
get { return description; }
set { description = value; }
}
private int price;
public int Price
{
get { return price; }
set { price = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ExaminationList
{
public class HealthCheckSet
{
public HealthCheckSet()
{
items = new List<HealthCheckItem>();
}
public HealthCheckSet(string name, List<HealthCheckItem> items)
{
this.Name = name;
this.items = items;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private List<HealthCheckItem> items;
public List<HealthCheckItem> Items
{
get { return items; }
set { items = value; }
}
private int price;
public int Price
{
get { return price; }
}
public void CalcPrice()
{
int totalPrice = 0;
foreach (HealthCheckItem item in items)
{
totalPrice += item.Price;
}
this.price = totalPrice;
}
}
}
using ExaminationList;
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 js
{
public partial class Form1 : Form
{
HealthCheckItem height, weight, sight, hearing, liverFun, ekg;
HealthCheckSet setA;
public Form1()
{
InitializeComponent();
}
List<HealthCheckItem> AllItem = new List<HealthCheckItem>();
List<HealthCheckItem> items = new List<HealthCheckItem>();
public Dictionary<string, HealthCheckSet> healthset = new Dictionary<string, HealthCheckSet>();
private void Form1_Load(object sender, EventArgs e)
{
lblSetName.Text = "";
lblSetPrice.Text = "";
this.btnAdd.Enabled = false;
this.btnDel.Enabled = false;
InitItems();
InitSets();
InitHealthSetList();
}
public void InitItems()
{
height = new HealthCheckItem("身高",5,"用於檢查身高");
weight = new HealthCheckItem("體重",8,"用於檢查體重");
sight = new HealthCheckItem("視力",10,"用於檢查視力");
hearing = new HealthCheckItem("聽力",10,"用於檢查聽力");
liverFun = new HealthCheckItem("肝功能",50,"用於檢查肝功能");
ekg = new HealthCheckItem("心電圖",100,"用於檢查心電圖");
AllItem.Add(height);
AllItem.Add(weight);
AllItem.Add(sight);
AllItem.Add(hearing);
AllItem.Add(liverFun);
AllItem.Add(ekg);
}
public void InitSets()
{
items = new List<HealthCheckItem>();
items.Add(height);
items.Add(weight);
items.Add(sight);
setA = new HealthCheckSet("入學體檢",items);
setA.CalcPrice();
this.healthset.Add("入學體檢",setA);
}
public void InitHealthSetList()
{
this.cboSets.Items.Clear();
this.cboSets.Items.Add("請選擇");
foreach(string key in this.healthset.Keys){
this.cboSets.Items.Add(key);
}
this.cboSets.SelectedIndex = 0;
}
private void UpdateSet(HealthCheckSet set)
{
this.dgvHealthList.DataSource = new BindingList<HealthCheckItem>(this.items);
}
private void cboSets_SelectedIndexChanged(object sender, EventArgs e)
{
string setName = this.cboSets.Text;
if(setName=="請選擇"){
this.dgvHealthList.DataSource = new BindingList<HealthCheckItem>();
lblSetName.Text = "";
lblSetPrice.Text = "";
return;
}
lblSetName.Text = this.healthset[setName].Name;
lblSetPrice.Text = this.healthset[setName].Price.ToString();
UpdateSet(healthset[setName]);
this.btnDel.Enabled = true;
}
private void btnDel_Click(object sender, EventArgs e)
{
string setName = this.cboSets.Text;
if(this.dgvHealthList.SelectedRows.Count==0){
MessageBox.Show("您沒有選中刪除項","友情提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
int index = this.dgvHealthList.SelectedRows[0].Index;
this.healthset[setName].Items.RemoveAt(index);
this.healthset[setName].CalcPrice();
UpdateSet(healthset[setName]);
this.lblSetName.Text = setA.Name;
this.lblSetPrice.Text = setA.Price.ToString();
MessageBox.Show("刪除成功!","友情提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.cboItems.Text != "請選擇")
{
this.btnAdd.Enabled = true;
}
else
{
this.btnAdd.Enabled = false;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
if(this.cboItems.SelectedIndex==0){
MessageBox.Show("請選擇一個項目");
return;
}
string cboSetText = this.cboSets.Text;
if(cboSetText=="請選擇"){
MessageBox.Show("請選擇套餐");
return;
}
int index = this.cboItems.SelectedIndex - 1;
if (!this.healthset[cboSetText].Items.Contains(AllItem[index]))
{
this.healthset[cboSetText].Items.Add(AllItem[index]);
this.healthset[cboSetText].CalcPrice();
UpdateSet(this.healthset[cboSetText]);
this.lblSetName.Text = this.healthset[cboSetText].Name;
this.lblSetPrice.Text = this.healthset[cboSetText].Price.ToString();
MessageBox.Show("添加成功!", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("該項目已存在!","友情提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void btnOK_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(this.txtHealthName.Text.Trim())){
MessageBox.Show("請輸入套餐名稱","友情提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
HealthCheckSet Hch = new HealthCheckSet();
this.healthset.Add(this.txtHealthName.Text.Trim(),Hch);
this.InitHealthSetList();
this.cboSets.SelectedIndex = this.healthset.Count;
MessageBox.Show("添加成功!","友情提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
}
