C#简易商城收银系统v1.0(2-1)
-
当初:
- 面向编程对象的好处及应用简单工厂模式(继承,多态)
-
现在:
- 制作一个简易的收银窗体应用程序
可以参考之前的 计算器 随笔
创建窗体程序
客户端代码
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 商城收银软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d;//声明一个double变量,用total来计算总计 private void button1_Click(object sender, EventArgs e) { double totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text); total = total + totalPrices;//将每个商品合计计算出来 lbxList.Text = "单价:" + txtPrice.Text + "数量:" + txtNum.Text + "合计:" + totalPrices.ToString(); //显示信息 lblResult.Text = total.ToString();//显示总计数 }
}
}
实现效果
现在增加一个打折功能
客户端代码
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 商城收银软件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double total = 0.0d;//声明一个double变量,用total来计算总计 private void button1_Click(object sender, EventArgs e) { double totalPrices = 0d; switch (cbxType.SelectedIndex) { case 0: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text); break; case 1: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)*0.8; break; case 2: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)*0.7; break; case 3: totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)*0.5; break; } total = total + totalPrices; lbxList.Text = "单价:" + txtPrice.Text + "数量:" + txtNum.Text + " " + cbxType.SelectedItem + "合计:" + totalPrices.ToString(); lblResult.Text = total.ToString(); } private void Form1_Load(object sender, EventArgs e) { cbxType.Items.AddRange(new object[] { "正常收费", "打八折", "打七折", "打五折" }); cbxType.SelectedIndex = 0; }
}
总结
像Convert.ToDouble()很多这样重复的
打折的分支可以考虑重构
客户端实例化出来 只需要更改运算
可以尝试使用简单工厂实现
下一篇随笔就用简单工厂来实现商城收银系统v1.0