//////////////////////////////////////////////////////////////////////////
//CashCalculate.h
#include <string>
using std::string;
/************************************************************************/
/* 请实现一个商场收银软件,包含正常收费,打折收费和返利收费三种具体策略 */
/************************************************************************/
/*
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成都是相同的工作,只是实现不同,
它可以以相同的方式调用所有算法,减少各种算法类与使用算法类之间的耦合。
策略模式的straterty类觌为Context 定义了一系列的算法和行为,继承有助于析取这些算法中的公共功能。
用CashContext对象管理在堆中分配的算法对象(CashSuper),可以免去调用者手动delete对象
*/
class CashSuper
{
public:
virtual float acceptCash(float money){return 0.0;}
};
class CashNormal : public CashSuper
{
public:
virtual float acceptCash(float money)
{
return money;
}
};
class CashRebate : public CashSuper
{
private:
float moneyRebate;
public:
CashRebate(float r):moneyRebate(r){}
CashRebate():moneyRebate(1){}
float acceptCash(float money)
{
return moneyRebate*money;
}
};
class CashReturn : public CashSuper
{
private:
float moneyCondition;
float moneyReturn;
public:
CashReturn():moneyReturn(0),moneyCondition(0){}
CashReturn(float condition,float ret):moneyCondition(condition),moneyReturn(ret){}
float acceptCash(float money)
{
if(money>=moneyCondition)
money=(int)(money/moneyCondition)*moneyReturn;
return money;
}
};
class CashFactory
{
public:
static CashSuper* createCashAccept(string flag)
{
CashSuper *super=NULL;
if(flag=="正常收费")
super=new CashNormal;
else if (flag=="打8折")
super=new CashRebate(0.8f);
else if(flag=="满300 返100")
super=new CashReturn(300,100);
return super;
}
};
class CashContext
{
protected:
CashSuper *cs;
public:
CashContext():cs(0){}
~CashContext()
{
if(cs)
delete cs;
}
CashContext &operator=(CashSuper*csuper)
{
if(cs) delete cs;
cs=csuper;
return *this;
}
float GetRedult(float money)
{
return cs->acceptCash(money);
}
};
// strategy.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "CashCalculate.h"
#include <iostream>
using namespace std;
//请实现一个商场收银软件,包含正常收费,打折收费和返利收费三种具体策略
int _tmain(int argc, _TCHAR* argv[])
{
//////////////////////////////////////////////////////////////////////////
//策略模式
CashContext cs;
int choice=-1;
cout<<"请输入收费方式(0 1 2 )"<<endl;
cin>>choice;
switch(choice)
{
case 0:
cs=new CashNormal;
break;
case 1:
cs=new CashRebate(0.8f);
break;
case 2:
cs=new CashReturn(100,10);
break;
default:
return 0;
}
float price=0.0;
int number=0;
cout<<"输入单价"<<endl;
cin>>price;
cout<<"请输入数量"<<endl;
cin>>number;
float total=cs.GetRedult(price*number);
cout<<"结果为"<<total<<endl;
system("pause");
return 0;
//////////////////////////////////////////////////////////////////////////
//工厂模式
/*float price=0.0;
cout<<"请输入单价"<<endl;
cin>>price;
int number;
cout<<"请输入数量"<<endl;
cin>>number;
cout<<"单价:"<<price<<" "<<"数量:"<<number<<endl;
cout<<"----------------------------------------------"<<endl;
float currentMoney=price*number;
string choice;
cout<<"请输入收费方式(正常收费 打8折 满300 返100 )"<<endl;
cin>>choice;
CashSuper *cashsuper;
cashsuper=CashFactory::createCashAccept(choice);
cout<<"应交钱为"<<cashsuper->acceptCash(currentMoney);
delete cashsuper;
return 0;
*/
//////////////////////////////////////////////////////////////////////////
//一般模式
/*
float price=0.0;
cout<<"请输入单价"<<endl;
cin>>price;
int number;
cout<<"请输入数量"<<endl;
cin>>number;
cout<<"单价:"<<price<<" "<<"数量:"<<number<<endl;
cout<<"----------------------------------------------"<<endl;
float total=0.0;
float discount=1.0;
float freeLimit=0.0;
float freeMoney=0.0;
int choice=-1;
cout<<"请输入收费方式(0 正常收费 1 打折收费 2 返利收费 )"<<endl;
cin>>choice;
switch(choice)
{
case 0:
total=number*price;
break;
case 1:
cout<<"请输入折扣 如 0.8 0.6"<<endl;
cin>>discount;
total=number*price*discount;
break;
case 2:
cout<<"请输入返利单元和返利金额 比如 满100元 送 10元等"<<endl;
cin>>freeLimit>>freeMoney;
total=number*price;//
total=total-((int)(total/100) *10);
break;
default:
cout<<"输入非法"<<endl;
return 0;
}
cout<<"最终价格为"<<total<<endl;
*/
system("pause");
return 0;
}