当我使用满300减100时,显示报错,找不到是哪里出了问题,求大佬指点
winform窗体代码
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;
using WindowsFormsApp4.MyClasses;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
//用于存放“用户添加的”商品信息的集合
Dictionary<string, Good> goods = new Dictionary<string, Good>();
//用于存放商品下拉框商品名称和对应的价格的集合
Dictionary<string,double> jh=new Dictionary<string, double>();
public Form1()
{
InitializeComponent();
//为jh集合添加商品名称和价格的键值对
jh.Add("湘西腊肉", 45.00);
jh.Add("东江鱼", 42.00);
jh.Add("长沙臭豆腐", 5.00);
jh.Add("临武鸭", 90.00);
//在下拉框中显示商品名称
cbb_name.Items.AddRange(jh.Keys.ToArray());//取出jh中所有键,并转换为数组
}
private void cbb_name_SelectedIndexChanged(object sender, EventArgs e)
{
//获取下拉框选中的商品名称
string goodName = cbb_name.Text;
//将商品名称作为键在names集合中找到对应的值
txt_price.Text = jh[goodName].ToString("0.00"); //取出的值为double类型,需要转化为字符串,0.00为显示为小数点后两位
}
private void cbb_jffs_SelectedValueChanged(object sender, EventArgs e)
{
if (this.cbb_jffs.Text == "打折")
{
this.txt_zkou.Enabled = true;
}
else
{
this.txt_zkou.Enabled = false;
this.txt_zkou.Text = "1.0";
}
}
private void button1_Click(object sender, EventArgs e)
{
if (goods.ContainsKey(cbb_name.Text))
{
MessageBox.Show("已添加相同名称的商品");
return;
}
Good good = new Good();
good.Name = cbb_name.Text;
good.Price = double.Parse(txt_price.Text);
good.Number = int.Parse(txt_number.Text);
good.Casname = cbb_jffs.Text;
Cash cash = null;
switch (cbb_jffs.Text)
{
case "正常收费":
cash = new NormalCash();
break;
case "打折":
cash = new RataCash();
break;
case "满300减100":
cash = new ReturnCash();
break;
}
good.TotalPrice = cash.CalPrice(good.Price * good.Number, double.Parse(txt_zkou.Text));
//将对象添加到集合
goods.Add(good.Name, good);
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = goods.Values.ToList();//将字典集合goods中所有值取出,并转换为列表集合
}
private void button2_Click(object sender, EventArgs e)
{
//删除,需要将dataGridView控件的selectedMode属性设置为fullrowSelect(整行选中)
//①获取用户选中的行的第一格数据(商品名称)
string goodName = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//②按获取的商品名称删除集合数据
goods.Remove(goodName);
//③刷新数据
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = goods.Values.ToList();//将字典集合goods中所有值取出,并转换为列表集合
}
}
}
Cash
abstract class Cash //优惠方式的抽象类
{
public abstract double CalPrice(double oldPrice, double rate);
}
Good
public class Good
{
public string Name { get; set; }
public double Price { get; set; }
public int Number { get; set; }
public string Casname { get; set; }//计费(优惠)方式总称
public double TotalPrice { get; set; }//单价*数量的结果优惠后的总价
}
NormalCash
class NormalCash:Cash
{
//重写抽象类的方法
public override double CalPrice(double oldPrice, double rate)
{
return oldPrice;
}
}
RataCash
class RataCash:Cash
{
public override double CalPrice(double oldPrice, double rate)
{
return oldPrice * rate;//原价*折扣率
}
}
ReturnCash
class ReturnCash:Cash
{
public override double CalPrice(double oldPrice, double rate)
{
if (oldPrice >= 300)
{
return oldPrice - 100;//返利100
}
return oldPrice;//返回原值
}
}