////// 现金收费抽象类 /// public abstract class CashSuper { public abstract double acceptCash(double money); }
////// 正常收费子类 /// public class CashNormal : CashSuper { public override double acceptCash(double money) { return money; } }
////// 打折收费子类 /// 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; } }
////// 返利收费子类 /// public 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; } }
private void Form1_Load(object sender, EventArgs e) { comboBox1.Items.AddRange(new object[] { "正常收费", "满300返100", "打8折"}); comboBox1.SelectedIndex = 0; } double total = 0.0d; private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); CashSuper csuper = CashFactory.CreateCashAccept(comboBox1.SelectedItem.ToString()); double totalPrices = 0d; totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice.Text))*Convert.ToDouble(txtCount.Text); total = total + totalPrices; listBox1.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtCount.Text + " " + comboBox1.SelectedItem + " 合计:" + totalPrices.ToString()); lblTotal.Text = total.ToString(); }