标签:对象 data his floor object 多个 简易 for price
可以参考之前的 C#简易商城收银系统v1.0 随笔
添加CashSuper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 商城收银软件 { abstract class CashSuper //现金收费抽象类 { public abstract double acceptCash(double money); //现金收取的抽象方法.参数为原价,返回当前价格 } }
添加CasHNormal类,并引用CashSuper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 商城收银软件 { class CasHNormal : CashSuper //正常收费类 { public override double acceptCash(double money) { return money; //正常收费,原价返回 } } }
添加CashRebate类,并引用CashSuper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 商城收银软件 { class CashRebate : CashSuper//打折收费类 { private double moneyRebate = 1d; public CashRebate(string moneyRebate) { //打折收费初始化 //必须输入折扣率,如8折 this.moneyRebate = double.Parse(moneyRebate); } public override double acceptCash(double money) { return money * moneyRebate; } } }
添加CashRrturn类,并引用CashSuper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 商城收银软件 { class CashReturn : CashSuper //返利收费子类 { private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; 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; } } }
添加CashFactory类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 商城收银软件 { class CashFactory //现金收费工厂类 { public static CashSuper createCashAccept(string type) { CashSuper cs = null; switch (type) { case "正常收费": cs = new CasHNormal(); break; case "满300返100": cs = new CashReturn("300","100"); break; case "打八折": cs = new CashRebate("0.8"); break; } return cs } } }
客户端主要类
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) { CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString()); double totalPrices = 0d; totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text)); 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[] { "正常收费", "打8折", "满300返100"}); cbxType.SelectedIndex = 0; } } }
总结 面向对象简单工厂实例
添加多个不同商品折扣类型
定义工厂类
客户端实例化出来
多看 多敲 慢慢的就可以领悟其中的设计模式
标签:对象 data his floor object 多个 简易 for price
原文地址:https://www.cnblogs.com/zaohuojian/p/11495150.html