商场促销模式
//现金收费抽象类
namespace 商城收银管理
{
abstract class CashSuper
{
//抽象方法:收取现金,参数为原价,返回为当前价
public abstract double acceptCash(double money);
}
}
//正常收费子类
namespace 商城收银管理
{
class cashNormal:CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
}
//打折收费子类
namespace 商城收银管理
{
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;
}
}
}
//返利收费子类
namespace 商城收银管理
{
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;
}
}
}
//现金收费工厂类
namespace 商城收银管理
{
class CashFactory
{
public static CashSuper createCashAccept(string type)
{
CashSuper cs = null;
switch (type)
{
case "正常收费":
cs = new cashNormal();
break;
case "满300返100":
CashReturn cashReturn = new CashReturn("300","100");
cs = cashReturn;
break;
case "打8折":
CashRebate cashRebate = new CashRebate("0.8");
cs = cashRebate;
break;
}
return cs;
}
}
}
//客户端代码
namespace 商城收银管理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double total = 0.0d;
private void btnOk_Click(object sender, EventArgs e)
{
CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
double totalPrices=0d;
totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
total = total + totalPrices;
lbxList.Items.Add("单价:" + txtPrice.Text + "数量:" + txtNum.Text + "合计:" + totalPrices.ToString());
lblResult.Text = total.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
total = 0;
txtPrice.Text = "1";
txtNum.Text = "0.00";
lbxList.Items.Clear();
lblResult.Text = "0.00";
}
}
}