c#问题求解
窗体代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Demo
{
public partial class Form1 : Form
{
HealthCheckItem height;
HealthCheckItem weight;
HealthCheckItem sight;
HealthCheckItem hearing;
HealthCheckItem liverFun;
HealthCheckItem bWaves;
HealthCheckItem ekg;
HealthCheckItem bloodPressure;
HealthCheckItem bloodTest;
Dictionary<string, HealthCheckItem> items;
Dictionary<string, HealthCheckItem> allItems;
HealthChecKSet setA;
HealthChecKSet setB;
public Form1()
{
InitializeComponent();
}
public void InitItems()
{
height = new HealthCheckItem("身高", 5, "用于检查身高");
weight = new HealthCheckItem("体重", 5, "用于检查体重");
sight = new HealthCheckItem("视力", 10, "用于检查视力");
hearing = new HealthCheckItem("听力", 10, "用于检查听力");
liverFun = new HealthCheckItem("肝功能", 10, "用于检查肝功能");
bWaves = new HealthCheckItem("B超", 30, "用于检查B超");
ekg = new HealthCheckItem("心电图", 50, "用于检查心电图");
bloodPressure = new HealthCheckItem("血压", 20, "用于检查血压");
bloodTest = new HealthCheckItem("血常规", 20, "用于检查血常规");
allItems = new Dictionary<string, HealthCheckItem>();
allItems.Add(height.Name, height);
allItems.Add(weight.Name, weight);
allItems.Add(sight.Name, sight);
allItems.Add(hearing.Name, hearing);
allItems.Add(liverFun.Name, liverFun);
allItems.Add(bWaves.Name, bWaves);
allItems.Add(ekg.Name, ekg);
allItems.Add(bloodPressure.Name, bloodPressure);
allItems.Add(bloodTest.Name, bloodTest);
}
public void InitSets()
{
items = new Dictionary<string, HealthCheckItem>();
items.Add(height.Name, height);
items.Add(weight.Name, weight);
items.Add(liverFun.Name, liverFun);
setA = new HealthChecKSet("入学体检", items);
setA.CalcPrice();
items = new Dictionary<string, HealthCheckItem>();
items.Add(height.Name, height);
items.Add(weight.Name, weight);
items.Add(sight.Name, sight);
items.Add(hearing.Name, hearing);
items.Add(liverFun.Name, liverFun);
items.Add(bWaves.Name, bWaves);
items.Add(ekg.Name, ekg);
items.Add(bloodPressure.Name, bloodPressure);
items.Add(bloodTest.Name, bloodTest);
setB = new HealthChecKSet("贵宾", items);
setB.CalcPrice();
}
private void Form1_Load(object sender, EventArgs e)
{
InitItems();
InitSets();
foreach (HealthCheckItem item in items.Values)
{
comboBox2.Items.Add(item.Name);
}
comboBox1.Items.Add(setA.Name);
comboBox1.Items.Add(setB.Name);
}
private void UpdateSet(HealthChecKSet set)
{
listView1.Items.Clear();
foreach (HealthCheckItem checkupItem in set.item.Values)
{
ListViewItem item = new ListViewItem(checkupItem.Name);
item.SubItems.AddRange(new string[] { checkupItem.Description, checkupItem.Price.ToString() });
listView1.Items.Add(item);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string setName = this.comboBox1.Text;
switch (setName)
{
case "入学体检":
UpdateSet(setA);
label4.Text = setA.Name;
label6.Text = setA.Price.ToString();
break;
case "贵宾":
UpdateSet(setB);
label4.Text = setB.Name;
label6.Text = setB.Price.ToString();
break;
}
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
string setName = comboBox1.Text;
string itemName = null;
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("没有删除的项", MessageBoxButtons.OKCancel.ToString());
}
else
{
itemName = listView1.SelectedItems[0].Text;
switch (setName)
{
case "入学体检":
setA.item.Remove(itemName);
UpdateSet(setA);
setA.CalcPrice();
label6.Text = setA.Price.ToString();
break;
case "贵宾":
setB.item.Remove(itemName);
UpdateSet(setB);
setB.CalcPrice();
label6.Text = setB.Price.ToString();
break;
}
MessageBox.Show("删除成功", MessageBoxButtons.OKCancel.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
string setName = comboBox1.Text;
string itemsName = null;
if (comboBox2.SelectedItem == null)
{
MessageBox.Show("没有选择要添加的项", MessageBoxButtons.OKCancel.ToString());
}
else
{
itemsName = comboBox2.Text;
switch (setName)
{
case "入学套餐":
foreach (string name in setA.item.Keys)
{
if (name == itemsName)
{
MessageBox.Show("该项已经添加", MessageBoxButtons.OKCancel.ToString());
}
}
setA.item.Add(itemsName,items[itemsName]);
UpdateSet(setA);
setA.CalcPrice();
label6.Text = setA.Price.ToString();
break;
case "贵宾":
foreach (string name in setB.item.Keys)
{
if (name == itemsName)
{
MessageBox.Show("该项已经添加", MessageBoxButtons.OKCancel.ToString());
}
}
setB.item.Add(itemsName, allItems[itemsName]);
UpdateSet(setB);
setB.CalcPrice();
label6.Text = setB.Price.ToString();
break;
}
}
MessageBox.Show("添加成功", MessageBoxButtons.OKCancel.ToString());
}
}
}
-------------------------------------------------
HealthCheckItem类代码
using System;
using System.Collections.Generic;
using System.Text;
namespace Demo
{
class 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 string result;
public string Result
{
get { return result;}
set { result = value;}
}
private int price;
public int Price
{
get { return price; }
set { price = value; }
}
public HealthCheckItem(string name,int price,string description)
{
this.Name = name;
this.Price = price;
this.Description = description;
}
}
}
---------------------------------------------------------------------------------
HealthChecKSet类代码
using System;
using System.Collections.Generic;
using System.Text;
namespace Demo
{
class HealthChecKSet
{
public HealthChecKSet()
{
items = new Dictionary<string, HealthCheckItem>();
}
public HealthChecKSet(string name,Dictionary<string,HealthCheckItem>items)
{
this.Name = name;
this.items = items;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int price;
public int Price
{
get { return price; }
set { price = value; }
}
private Dictionary<string, HealthCheckItem> items;
public Dictionary<string, HealthCheckItem> item
{
get { return items; }
set { items = value; }
}
public void CalcPrice()
{
int totalPrice=0;
foreach(HealthCheckItem item in items.Values)
{
totalPrice+=item.Price;
}
this.price=totalPrice;
}
}
}
--------------------------------------------------
这个段代码只能添加贵宾不能添加学生
求解 --------------------编程问答-------------------- 看的人头晕,能不能简单点
--------------------编程问答-------------------- 可以debug,看添加学生的时候发生了什么 --------------------编程问答-------------------- 提出问题的时候,要指出你的具体问题的地方,或者加颜色,或者文字说明代码块。
这样提出的问题,回答率很很很很低的。 --------------------编程问答-------------------- case "入学套餐":
改为 case "入学体检":
--------------------编程问答-------------------- 建议:1。像这种在逻辑处理中需要用到的字符串最好永变量只保留一份拷贝,然后通过变量来引用。
2。写代码最好注意命名规范,不要懒,vs给你生成button1你就用button1. setA, setB也一点意义都没有。 --------------------编程问答-------------------- 只贴代码,我们除了要看代码之外还要猜猜你的问题是什么…… --------------------编程问答-------------------- 看的眼花缭乱的 测试一下
补充:.NET技术 , .NET Framework