要做的是一个商场收银软件,营业员根据客户购买商品单价和数量,向客户收费。两个文本框,输入单价和数量,再用个列表框来记录商品的合计,最终用一个按钮来算出总额就可以了,还需要一个重置按钮来重新开始。

  

  核心代码(v1.0)

 //声明一个double变量total来计算总计
double total = 0.0d;
private void btnConfirm_Click(object sender, EventArgs e)
{
//声明一个double变量totalPrices来计算每个商品的单价(txtPrice) * 数量(txtNum)后的合计
double totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text);
//将每个商品合计计入总计
total = total + totalPrices;
//在列表框中显示信息
lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 合计:" + totalPrices.ToString());
//在lblTotalShow标签上显示总计数
lblTotalShow.Text = total.ToString();
}

  需求又来了,现在要求商场对商品搞活动,所有的商品打 8 折。扩展功能加了一个下拉选择框……v1.1版本来了

  核心代码(v1.1)

namespace ExtendDiscount
{
public partial class frmMain :Skin_Metro
{
public frmMain()
{
InitializeComponent();
}
//声明一个double变量total来计算总计
double total = 0.0d;
private void btnConfirm_Click(object sender, EventArgs e)
{
//声明一个double变量totalPrices
double totalPrices = 0d;
//加入打折情况
switch (cbxType.SelectedIndex)
{
case :
totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text);
break;
case :
totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.8;
break;
case :
totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.7;
break;
case :
totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.5;
break;
}
//将每个商品合计计入总计
total = total + totalPrices;
//在列表框中显示信息
lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 合计:" + totalPrices.ToString());
//在lblTotalShow标签上显示总计数
lblTotalShow.Text = total.ToString();
} private void frmMain_Load(object sender, EventArgs e)
{
cbxType.SelectedIndex = ;
} private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{
//数字0~9所对应的keychar为48~57
e.Handled = true;
//输入0-9
if ((e.KeyChar >= && e.KeyChar <= ) || e.KeyChar == )
{
e.Handled = false;
}
} private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
{
//数字0~9所对应的keychar为48~57
e.Handled = true;
//输入0-9
if ((e.KeyChar >= && e.KeyChar <= ) || e.KeyChar == )
{
e.Handled = false;
}
} private void btnReset_Click(object sender, EventArgs e)
{
total = 0.0;
txtPrice.Text = "";
txtNum.Text = "";
lblTotalShow.Text = "";
lbxList.Items.Clear();
cbxType.SelectedIndex = ;
}
}
}

  简单的工厂模式在WinForm中的应用

  现在又根据市场情况,添加此类需求——“满300送100、满200送50等等“

  面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类,打一折和打九折只是形式的不同,抽象分析出来,所有的打折算法都是一样的,所以打折算法应该是一个类;而”满300送100“等等,返利算法应该是另一个类。

  

  代码(v1.2)

  

namespace ExtendDiscountOfSimpleFactoryPattern
{
//现金收取父类
abstract class CashSuper
{
//抽象方法:收取现金,参数为原价,返回为当前价
public abstract double acceptCash(double money);
}
}

namespace ExtendDiscountOfSimpleFactoryPattern
{
//正常消费,继承CashSuper
class CashNormal:CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
}

namespace ExtendDiscountOfSimpleFactoryPattern
{
//打折收费消费,继承CashSuper
class CashRebate:CashSuper
{
private double moneyRebate = 1d;
//初始化时,必需要输入折扣率,如八折,就是0,8
public CashRebate(string moneyRebate)
{
//界面向类传值
this.moneyRebate = double.Parse(moneyRebate);
}
public override double acceptCash(double money)
{
return money * moneyRebate;
}
}
}

namespace ExtendDiscountOfSimpleFactoryPattern
{
//返利收费
class CashReturn:CashSuper
{
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
//初始化时必须要输入返利条件和返利值,比如满300返100
//则moneyCondition为300,moneyReturn为100
public CashReturn(string moneyCondition, string moneyReturn)
{
this.moneyCondition =double.Parse(moneyCondition);
this.moneyReturn = double.Parse(moneyReturn);
} public override double acceptCash(double money)
{
double result = money;
//若大于返利条件,则需要减去返利值
if (money >= moneyCondition)
{
result = money - Math.Floor(money / moneyCondition) * moneyReturn;
}
return result;
}
}
}

  //简单工厂模式类

 namespace ExtendDiscountOfSimpleFactoryPattern
{
//收费对象生成工厂
class CashFactory
{
//根据条件返回相应的对象
public static CashSuper createCashAccept(string type)
{
CashSuper cs = null;
switch (type)
{
case "正常消费":
cs = new CashNormal();
break;
case "满300返100":
cs = new CashReturn("", "");
break;
case "打8折":
cs = new CashRebate("0.8");
break;
case "打7折":
cs = new CashRebate("0.7");
break;
case "打5折":
cs = new CashRebate("0.5");
break;
}
return cs;
}
}
}

  //在WinForm中调用

  

 namespace ExtendDiscountOfSimpleFactoryPattern
{
public partial class frmMain :Skin_Metro
{
public frmMain()
{
InitializeComponent();
}
//客户端窗体程序
CashSuper cSuper;//声明一个父类对象 //声明一个double变量total来计算总计
double total = 0.0d;
private void btnConfirm_Click(object sender, EventArgs e)
{
//声明一个double变量totalPrices
double totalPrices = 0d;
//利用简单工厂模式根据下拉选择框,生成相应的对象
cSuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
totalPrices = cSuper.acceptCash(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
//将每个商品合计计入总计
total = total + totalPrices;
//在列表框中显示信息
lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 合计:" + totalPrices.ToString());
//在lblTotalShow标签上显示总计数
lblTotalShow.Text = total.ToString();
} private void btnReset_Click(object sender, EventArgs e)
{
total = 0.0;
txtPrice.Text = "";
txtNum.Text = "";
lblTotalShow.Text = "";
lbxList.Items.Clear();
cbxType.SelectedIndex = ;
} private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
{
//数字0~9所对应的keychar为48~57
e.Handled = true;
//输入0-9
if ((e.KeyChar >= && e.KeyChar <= ) || e.KeyChar == )
{
e.Handled = false;
}
} private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{
//数字0~9所对应的keychar为48~57
e.Handled = true;
//输入0-9
if ((e.KeyChar >= && e.KeyChar <= ) || e.KeyChar == )
{
e.Handled = false;
}
} private void frmMain_Load(object sender, EventArgs e)
{
//在窗体加载的时候,下拉选择框,就选择索引为0的元素——"正常消费"
cbxType.SelectedIndex = ;
}
}
}

最新文章

  1. 我的css笔记
  2. c#:Reflector+Reflexil 修改编译后的dll/exe文件
  3. iOS,html使用交互相关
  4. 【BZOJ-3293&amp;1465&amp;1045】分金币&amp;糖果传递&#215;2 中位数 + 乱搞
  5. 解决在windows的eclipse上面运行WordCount程序出现的一系列问题详解
  6. Android(java)学习笔记87:File类使用
  7. Java [leetcode 34]Search for a Range
  8. android-UI组件实例大全(六)------ImageView图像视图
  9. 网易云课堂_程序设计入门-C语言_第六章:数组_1多项式加法
  10. noip 2016 提高组题解
  11. WebSocket 连接关闭(代码:1006)
  12. 《java入门第一季》之类(Object类)
  13. tensorboard基础使用
  14. Android的BroadcastReceiver组件
  15. centos6.6安装hadoop-2.5.0(二、伪分布式部署)
  16. UINavigationController出现nested push animation can result in corrupted navigation bar的错误提示
  17. matlab reshape()、full()
  18. jQuery基础笔记(3)
  19. centos7 vnc 无法systemctl启动
  20. 第六章 consul UI

热门文章

  1. vue2.0 + element ui 实现表格穿梭框
  2. 小师妹学JavaIO之:MappedByteBuffer多大的文件我都装得下
  3. Node.js环境安装
  4. android中的逐帧动画
  5. 小师妹学JVM之:深入理解JIT和编译优化-你看不懂系列
  6. rust 生命周期2
  7. Centos7.X 搭建Grafana+Jmeter+Influxdb 性能实时监控平台(不使用docker)
  8. 黎活明8天快速掌握android视频教程--25_网络通信之资讯客户端
  9. Scala创建SparkStreaming获取Kafka数据代码过程
  10. 02 [掌握] redis详情命令