static void Main(string[] args) { #region normal strategy StrategyContext context; context = new StrategyContext(new ConcreateStrategyA()); context.StrategyInterface(); context = new StrategyContext(new ConcreateStrategyB()); context.StrategyInterface(); context = new StrategyContext(new ConcreateStrategyC()); context.StrategyInterface(); #endregion #region cash strategy var money = 300; CashContext cashContext; cashContext = new CashContext(new CashFactorySimple.CashNormal()); LogInfo("原价销售", money, cashContext.GetResult(money)); cashContext = new CashContext(new CashFactorySimple.CashRate(0.8)); LogInfo("打八折销售", money, cashContext.GetResult(money)); cashContext = new CashContext(new CashFactorySimple.CashReturn(300, 100)); LogInfo("满300减100销售", money, cashContext.GetResult(money)); #endregion Console.ReadLine(); }
static void Main(string[] args) { CashContext context = new CashContext("正常收费"); Console.WriteLine(context.GetResult(600)); context = new CashContext("打8折"); Console.WriteLine(context.GetResult(600)); context = new CashContext("满300减100"); Console.WriteLine(context.GetResult(600)); }
private void BtnEnter_Click(object sender, EventArgs e) { cashContext = new CashContext(ComboBoxMethod.SelectedItem.ToString()); double totalPrices = cashContext.GetResult(Convert.ToDouble(TxtPrice.Text) * Convert.ToDouble(TxtCount.Text)); Total = Total + totalPrices; ListBox.Items.Add($"單價: {TxtPrice.Text} ,數量: {TxtCount.Text}, {ComboBoxMethod.SelectedItem} 合計: {totalPrices.ToString()}"); LabelTotalPrice.Text = Total.ToString(); }
void btnOK_Click(object sender, RoutedEventArgs e) { CashContext cc = new CashContext(cb.SelectedValue.ToString()); double totalPrice = 0d; totalPrice = cc.GetResult(double.Parse(price.Text) * double.Parse(qty.Text)); total += totalPrice; state.Items.Add("单价:" + price.Text + "数量:" + qty.Text + "合计:" + totalPrice); result.Content = total; }
private void buttonAdd_Click(object sender, EventArgs e) { CashContext myCashContext = new CashContext(comboBox_discount.SelectedIndex); decimal sum = myCashContext.GetResult(Int32.Parse(textBox_price.Text) * Int32.Parse(textBox_number.Text)); string detail = "price=" + textBox_price.Text + ",number=" + textBox_number.Text + ",discount=" + comboBox_discount.Text + ",item=" + sum.ToString(); listBox_detail.Items.Add(detail); total = total + sum; label_total.Text = total.ToString(); }
private void btnOk_Click(object sender, EventArgs e) { CashContext csuper = new CashContext(cbxType.SelectedItem.ToString()); double totalPrices = csuper.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total = total + totalPrices; lbxList.Items.Add("单价:" + txtPrice.Text + "数量:" + txtNum.Text + " " + cbxType.SelectedItem + " 合计:" + totalPrices.ToString()); lblResult.Text = total.ToString(); }
private void btnOk_Click(object sender, EventArgs e) { //BaseCashAccept cash = new CashAcceptFactory().Create(cbType.Text); //decimal money = cash.GetResult(decimal.Parse(textPrice.Text) * decimal.Parse(textNum.Text)); //lboxResult.Items.Add("数量:" + textNum.Text + " 单价:" + textPrice.Text + " 总价:" + money); //TotalMoney += money; //lbTotal.Text = TotalMoney.ToString(); CashContext context = new CashContext(cbType.Text); decimal money = context.AcceptCash(decimal.Parse(textPrice.Text) * decimal.Parse(textNum.Text)); lboxResult.Items.Add("数量:" + textNum.Text + " 单价:" + textPrice.Text + " 总价:" + money); TotalMoney += money; lbTotal.Text = TotalMoney.ToString(); }
static void Main(string[] args) { double num = 800; CashContext context = new CashContext("正常收费"); Console.WriteLine(context.GetResult(num)); CashContext context1 = new CashContext("满300返100"); Console.WriteLine(context1.GetResult(num)); CashContext context2 = new CashContext("打8折"); Console.WriteLine(context2.GetResult(num)); Console.ReadKey(); }
public static void Main(string[] args) { Console.WriteLine("請選擇收費方式:1.正常收費 2.滿三百送一百 3.打8折"); var cashType = (CashEnum)Enum.Parse(typeof(CashEnum), Console.ReadLine()); var cash = new CashContext(cashType); Console.WriteLine("請輸入數量:"); var count = int.Parse(Console.ReadLine()); Console.WriteLine("請輸入金額:"); var money = double.Parse(Console.ReadLine()); var result = cash.GetResult(count * money); Console.WriteLine("金額為:{0}", result); Console.Read(); }
static void Main(string[] args) { //Console.WriteLine("Hello World!"); Console.WriteLine("请输入单价:"); double price = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入数量:"); int count = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请选择优惠活动:\n Normal\tThreeDiscount\tFullReduce"); string preferential = Console.ReadLine(); CashContext context = new CashContext(preferential); double money = context.GetResult(price * count); Console.WriteLine("最后价格为:" + money); }
private static void Test2() { double money = 33333.1; CashContext cc; // 可以改造一下上下文类 加入简单工厂 让客户端脱离具体算法类 // 构造实例化算法A策略 正常收费 cc = new CashContext(new CashNormal()); Console.WriteLine(cc.GetLasResult(money)); // 构造实例化算法B策略 打折 cc = new CashContext(new CashRebate(0.8)); Console.WriteLine(cc.GetLasResult(money)); // 构造实例化算法C策略 返利 cc = new CashContext(new CashReturn(0.8, 500)); Console.WriteLine(cc.GetLasResult(money)); }
private static void NewMethod2() { const double txtPrice = 600; const int txtNum = 1; var cc = new CashContext("Normal charge"); var totalPrice = cc.GetResult(txtPrice) * txtNum; Console.WriteLine("TotalPrice1 " + totalPrice); cc = new CashContext("300 minus 100"); totalPrice = cc.GetResult(txtPrice) * txtNum; Console.WriteLine("TotalPrice2 " + totalPrice); cc = new CashContext("20% off"); totalPrice = cc.GetResult(txtPrice) * txtNum; Console.WriteLine("TotalPrice3 " + totalPrice); Console.Read(); }
static void Main(string[] args) {//還是工廠 CashFactory CashSuper會看到2個類別 double total = 0d; CashSuper cs = CashFactory.createCashAccept("打8折"); double totalprice = 0d; totalprice = cs.acceptCash(5000d) * 5;//price*num total += totalprice; Console.WriteLine(total); //策略模式 只需要一個CashContext類別 收費演算法跟用戶端分離 CashContext cc = null; cc = new CashContext("打8折"); totalprice = 0d; totalprice = cc.GetResult(5000) * 5; //單價X數量 total += totalprice; Console.WriteLine(total); Console.ReadKey(); }