static void Main(string[] args) { while (true) { Console.WriteLine("请输入商品价值"); double money = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请选择折扣类型 1.无优惠 2.满500减100 3.全场8折"); string operate = ""; switch (Console.ReadLine()) { case "1": operate = "无优惠"; break; case "2": operate = "满500减100"; break; case "3": operate = "全场8折"; break; } CashContext cs = new CashContext(operate); Console.WriteLine("优惠后价格为:" + cs.GetResult(money)); Console.ReadKey(); } }
/// <summary> /// 策略模式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOk_Click(object sender, EventArgs e) { CashContext cc = null; switch (cbxType.SelectedItem.ToString()) { case "正常收费": cc = new CashContext(new CashNormal()); break; case "满300减100": cc = new CashContext(new CashReturn("300", "100")); break; case "打8折": cc = new CashContext(new CashRebate("0.8")); break; default: break; } // CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString()); double totalPrices = 0; totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total = total + totalPrices; lblList.Items.Add("模式:" + cbxType.SelectedItem.ToString() + "单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 合计:" + totalPrices.ToString()); lblResult.Text = total.ToString(); }
static void Main(string[] args) { CashContext csuper = new CashContext("满300返100"); var res = csuper.GetResult(300); Console.WriteLine(res); Console.ReadLine(); }
private void button1_Click(object sender, EventArgs e) { CashContext cash = new CashContext(comboBox1.SelectedItem.ToString()); double totalCash = 0d; totalCash = cash.GetResult(Convert.ToDouble(Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox2.Text))); total = total + totalCash; label4.Text = total.ToString(); listBox1.Items.Add("价格: " + textBox1.Text + " 数量: " + textBox2.Text + comboBox1.SelectedItem + " 小计: " + totalCash); }
/// <summary> /// 策略模式和工场模式结合 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOk_Click1(object sender, EventArgs e) { // CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString()); CashContext csuper = new CashContext(cbxType.SelectedItem.ToString()); double totalPrices = 0; totalPrices = csuper.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total = total + totalPrices; lblList.Items.Add("模式:" + cbxType.SelectedItem.ToString() + "单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 合计:" + totalPrices.ToString()); lblResult.Text = total.ToString(); }
//private void btnOK_Click(object sender, RoutedEventArgs e) //{ // double totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text); // total += totalPrices; // lbxList.Items.Add("单价: " + txtPrice.Text + "数量: " + txtNum.Text + ",合计: " + totalPrices.ToString()); // lblResult.Text = total.ToString(); //} #endregion #region 增加打折和返利 //private void btnOK_Click(object sender, RoutedEventArgs e) //{ // double totalPrices = 0; // switch (cbxType.SelectedIndex) // { // case 0: // totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text); // break; // case 1: // totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.8; // break; // case 2: // totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.7; // break; // case 3: // totalPrices = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text) * 0.6; // break; // default: // break; // } // total += totalPrices; // lbxList.Items.Add("单价: " + txtPrice.Text + "数量: " + txtNum.Text + ",合计: " + totalPrices.ToString()); // lblResult.Text = total.ToString(); //} #endregion #region 简单工厂模式 //private void btnOK_Click(object sender, RoutedEventArgs e) //{ // CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString()); // double totalPrices = 0d; // totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); // total += totalPrices; // lbxList.Items.Add("单价: " + txtPrice.Text + "数量: " + txtNum.Text + ",合计: " + totalPrices.ToString()); // lblResult.Text = total.ToString(); //} #endregion #region 策略模式 //private void btnOK_Click(object sender, RoutedEventArgs e) //{ // CashContext cashContext = null; // switch (cbxType.SelectedItem.ToString()) // { // case "正常收费": // cashContext = new CashContext(new CashNormal()); // break; // case "满300返100": // cashContext = new CashContext(new CashReturn("300", "100")); // break; // case "打8折": // cashContext = new CashContext(new CashRebate("0.8")); // break; // default: // break; // } // double totalPrices = 0d; // totalPrices = cashContext.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); // total += totalPrices; // lbxList.Items.Add("单价: " + txtPrice.Text + "数量: " + txtNum.Text + ",合计: " + totalPrices.ToString()); // lblResult.Text = total.ToString(); //} #endregion #region 策略结合简单工厂模式 private void btnOK_Click(object sender, RoutedEventArgs e) { CashContext cashContext = new CashContext(cbxType.SelectedItem.ToString()); double totalPrices = 0d; totalPrices = cashContext.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total += totalPrices; lbxList.Items.Add("单价: " + txtPrice.Text + "数量: " + txtNum.Text + ",合计: " + totalPrices.ToString()); lblResult.Text = total.ToString(); }
private void btnOK_Click(object sender, EventArgs e) { double totalPrices = 0d; #region 简单工厂的客户端代码,须知道(抽象类和工厂类) //CashSuper super = CashFactory.CreateCashAccept(cbxType.SelectedItem.ToString()); //totalPrices = super.acceptCash(Convert.ToDouble(txtPrice.Text)) // * Convert.ToDouble(txtNum.Text); //total = total + totalPrices; //lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text // + " " + cbxType.SelectedItem + " 合计: " + totalPrices.ToString()); //lbResult.Text = total.ToString(); #endregion #region 只有策略模式的客户端代码,须知道(策略类和各子类) //CashContext cc = null; //switch (cbxType.SelectedItem.ToString()) //{ // case "正常收费": // cc = new CashContext(new CashNormal()); // break; // case "满300返100": // cc = new CashContext(new CashReturn("300", "100")); // break; // case "打8折": // cc = new CashContext(new CashRebate("0.8")); // break; // default: break; //} //totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text)) // * Convert.ToDouble(txtNum.Text); //total = total + totalPrices; //lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text // + " " + cbxType.SelectedItem + " 合计: " + totalPrices.ToString()); //lbResult.Text = total.ToString(); #endregion #region 策略模式和简单工厂模式结合 只需知道策略类 CashContext csuper = new CashContext(cbxType.SelectedItem.ToString()); 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()); lbResult.Text = total.ToString(); #endregion }
static void Main(string[] args) { double all = 693.8; CashContext con = new CashContext("打折"); Console.WriteLine(con.GetCalResult(all)); }
static void Main(string[] args) { var cashContext = new CashContext("normal"); }