第2章デパートの販促(3)——策略と簡単な工場の結合
2758 ワード
/*shop.h */
#ifndef _SHOP_
#define _SHOP_
#include<iostream>
#include<string>
//
class Cashsuper
{
public:
virtual double acceptcash(double money);
};
//
class cashnormal:public Cashsuper
{
public:
virtual double acceptcash(double money);
};
//
class cashrebate:public Cashsuper
{
public:
cashrebate():moneyrebate(0.1){}
virtual double acceptcash(double money);
void calcute_rebate(std::string moneystring);
private:
double moneyrebate;//
};
//
class cashreturn:public Cashsuper
{
public:
void MoneyReturn(std::string moneycondition_str,std::string moneyreturn_str);
virtual double acceptcash(double money);
private:
double moneycondition;// ( 300)
double moneyreturn;// ( 100)
};
// ,
class cashcontxt
{
public:
cashcontxt(std::string type){
if(type==" ")
{
cashnormal* cx=new cashnormal;
cas=cx;
}
else if(type==" ")
{
cashrebate *cx=new cashrebate;
cx->calcute_rebate(type);
cas=cx;
}
else if(type==" 300 100")
{
cashreturn* cy=new cashreturn;
cy->MoneyReturn(" 300"," 100");
cas=cy;
}
else
exit(1);
}
double getresult(double money)
{
return cas->acceptcash(money);
}
private:
Cashsuper* cas;
};
#endif
/*shop,cpp */
#include<iostream>
#include"shop.h"
using namespace std;
// ( )
double Cashsuper::acceptcash(double money)
{
return money;
}
// ( )
double cashnormal::acceptcash(double money)
{
return money;
}
// ( )
void cashrebate::calcute_rebate(std::string moneystring)
{
if(moneystring==" ")
moneyrebate=0.8;
else if(moneystring==" ")
moneyrebate=0.9;
}
double cashrebate::acceptcash(double money)
{
return money*moneyrebate;
}
// ( )
void cashreturn::MoneyReturn(std::string moneycondition_str,std::string moneyreturn_str)
{
if(moneycondition_str==" 100")
moneycondition=100;
if(moneycondition_str==" 200")
moneycondition=200;
if(moneycondition_str==" 300")
moneycondition=300;
if(moneyreturn_str==" 10")
moneyreturn=10;
if(moneyreturn_str==" 50")
moneyreturn=50;
if(moneyreturn_str==" 100")
moneyreturn=100;
}
double cashreturn::acceptcash(double money)
{
return money-moneyreturn;
}
/*main.cpp */
#include "shop.h"
#include<iostream>
using namespace std;
int main()
{
string type=" 300 100";
cashcontxt* cas;
cas=new cashcontxt(type);
cout<<cas->getresult(1000)<<endl;
system("pause");
return 0;
}