using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 商场收银
{
abstract class CashSuper
{
/// <summary>
/// 现金收费抽象类
/// </summary>
/// <param name="money">原价</param>
/// <returns>折扣价格</returns>
public abstract double acceptCash(double money);
}
/// <summary>
/// 二合一的具体效果实现1/2
/// </summary>
class CashContext
{
private CashSuper cs;
/// <summary>
///构造函数,让传入具体的不同的子类实例化对象
/// </summary>
/// <param name="type"></param>
public CashContext(string type)
{
switch (type)
{
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
cs = new CashReturn("300", "100");
break;
case "打八折":
cs = new CashRebate("0.8");
break;
}
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
/// <summary>
/// 简单工厂模式的具体实现效果1/2
/// </summary>
class CashFactory
{
public static CashSuper createCashAccept(string type)
{
CashSuper cs = null;
switch (type)
{
case "正常收费":
cs = new CashNormal();
break;
case "满300返100":
cs = new CashReturn("300", "100");
break;
case "打8折":
cs = new CashRebate("0.8");
break;
}
return cs;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 商场收银
{
class CashNormal : CashSuper
{
/// <summary>
/// 正常收费,原价返回。
/// </summary>
/// <param name="money"></param>
/// <returns></returns>
public override double acceptCash(double money)
{
return money;
}
}
/// <summary>
/// 打折收费类
/// </summary>
class CashRebate : CashSuper
{
private double moneyRebate = 1d;
public CashRebate(string moneyRebate)
{
this.moneyRebate = double.Parse(moneyRebate);
}
/// <summary>
/// 打折收费
/// </summary>
/// <param name="money"></param>
/// <returns></returns>
public override double acceptCash(double money)
{
return money * moneyRebate;
}
}
/// <summary>
/// 返利收费
/// </summary>
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;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 商场收银
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
double total = 0.0d;
private void btnOk_Click(object sender, EventArgs e)
{
double totalPrices = 0d;
////简单工程的实现方式2/2
////策略模式的实现方式2/2
CashContext cc = new CashContext(cbxType.SelectedItem.ToString());
totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
total += totalPrices;
lbxList.Items.Add("单价:" + txtPrice.Text + " 数量:" + txtNum.Text + " 总计:" + totalPrices);
lbMess.Text = total.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
total = 0;
txtPrice.Text = "";
txtNum.Text = "";
lbMess.Text = null;
lbxList.Items.Clear();
cbxType.SelectedIndex = 0;
}
private void FormMain_Load(object sender, EventArgs e)
{
cbxType.Items.AddRange(new object[] { "正常收费", "满300返100", "打八折" });
cbxType.SelectedIndex = 0;
}
}
}