码迷,mamicode.com
首页 > 其他好文 > 详细

第2章商场促销(1)——工厂模式

时间:2015-05-31 16:39:06      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

/*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 CashFactory
{
public:
	static Cashsuper* createcashaccept(std::string type);
};

#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)
{
	double result=money;
	if(result>=moneycondition)
		result=money-moneyreturn;
	return result;
}

//现金收费工厂类(函数定义)
 Cashsuper* CashFactory::createcashaccept(std::string type)
{
	Cashsuper* cs=NULL;
	if(type=="正常收费")
		cs=new cashnormal;
	else if(type=="满300返100")
	{
		cashreturn* csre=new cashreturn;
		csre->MoneyReturn("满300","返100");
		cs=csre;
	}
	else if(type=="打八折")
	{
		cashrebate* csreb=new cashrebate;
		csreb->calcute_rebate("打八折");
		cs=csreb;
	}
	else
		exit(1);
	return cs;
 }

  

/*main.cpp文件*/
#include "shop.h"
#include<iostream>

using namespace std;

int main()
{
	string type;
	cin>>type;
	Cashsuper* csuper=CashFactory::createcashaccept(type);
	double totleprices=1000;
	double totle=0.0;
	totle=csuper->acceptcash(totleprices);
	cout<<totle<<endl;
	system("pause");
	return 0;
}

  

第2章商场促销(1)——工厂模式

标签:

原文地址:http://www.cnblogs.com/yanliang12138/p/4542163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!