btnCalc_Click
private void btnCalc_Click(object sender, EventArgs e) { double total = 0.0d; string ctypeStr = icmbCtype.SelectedItem.ToString(); css = DXSLFactory.CashFactory.CreateCashSuper(ctypeStr); double d = css.AcceptCash(double.Parse(txtMoney.Text)) * Convert.ToDouble(txtNumber.Text); total = total + d; listBoxControl1.Items.Add(string.Format("单价:{0} 数量:{1} 合计:{2} ", txtMoney.Text, txtNumber.Text, total)); }
CashFactory
public class CashFactory { /// <summary> /// 根据条件返回相应对象 /// </summary> /// <param name="ctype"></param> /// <returns></returns> public static DXSLCashSuper.CashSuper CreateCashSuper(string ctype) { DXSLCashSuper.CashSuper css = null; switch (ctype) { case "正常收费": CashNormal csn = new CashNormal(); css = csn; break; case "满300返100": CashRetrun csrt = new CashRetrun("300", "100"); css = csrt; break; case "打8折": CashRebate csrb = new CashRebate("0.8"); css = csrb; break; } return css; } }
现金收取父类CashSuper
/// <summary> /// 现金收取父类 /// </summary> public abstract class CashSuper { /// <summary> /// 抽象方法,收取现金 /// </summary> /// <param name="money">原价,返回为当前价格</param> /// <returns></returns> public abstract double AcceptCash(double money); }
正常收费CashNormal ,继承CashSuper
/// <summary> /// 正常收费,继承CashSuper /// </summary> public class CashNormal : CashSuper { public override double AcceptCash(double money) { return money; } }
打折收费CashRebate ,继承CashSuper
/// <summary> /// 打折收费,继承CashSuper /// </summary> public class CashRebate : CashSuper { private double moneyRebate = 1d; public CashRebate(string moneyRebate) { this.moneyRebate = double.Parse(moneyRebate); } public override double AcceptCash(double money) { return money * moneyRebate; } }
返利收费CashRetrun ,继承CashSuper
/// <summary> /// 返利收费,继承CashSuper /// </summary> public class CashRetrun : CashSuper { private double moneyCondition = 0d; private double moneyReturn = 0d; public CashRetrun(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; } }