博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
策略模式
阅读量:7060 次
发布时间:2019-06-28

本文共 2148 字,大约阅读时间需要 7 分钟。

///     /// 现金收费抽象类    ///     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();        }

 

 

 

转载于:https://www.cnblogs.com/xuweixia/p/3616883.html

你可能感兴趣的文章
sqlserver查询表索引
查看>>
JavaScript 基础知识系列:数据类型及slice(8,-1)
查看>>
String,StringBuffer,StringBuilder三者有什么异同?
查看>>
[LeetCode] Invert Binary Tree
查看>>
2018.3.31——(4)句子
查看>>
js call
查看>>
【原】Java学习笔记024 - 包装类
查看>>
如何写一手漂亮的 Vue
查看>>
2018.10.29-dtoj-3999-游戏(game)
查看>>
LNOI2019 游记
查看>>
php简单实现MVC
查看>>
json和jsonp的区别(转)
查看>>
轮播图
查看>>
我们为什么要当程序员?
查看>>
Bootstrap3系列:按钮组
查看>>
React笔记:组件(3)
查看>>
tomcat与jboss 01
查看>>
【记录一个问题】linux + opencv + gpu视频解码,好不容易编译通过,运行又coredump了...
查看>>
问题总结
查看>>
testNG框架,使用@BeforeClass标注的代码,执行失败不抛出异常,只提示test ignore的解决方法...
查看>>