#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
class CFruits
{
private:
double t_count;
double t_price;
public:
void SetCount(double count)
{
t_count =count;
}
void SetPrice(double price)
{
t_price =price;
cout<<"set price "<<t_price<<endl;
}
double GetPrice()
{
return this->t_price;
}
double GetCount()
{
return this->t_count;
}
};
class CFruitBasket
{
private:
vector <CFruits> m_vfruits;
double m_total;
public:
virtual ~CFruitBasket()
{
}
void SetFruit(vector <CFruits> fruits)
{
m_vfruits = fruits;
}
virtual double GetTotal()
{
m_total = 0;
for(vector<CFruits>::iterator iter=m_vfruits.begin();iter!=m_vfruits.end();iter++)
{
cout<<(*iter).GetCount()<<endl;
m_total += (*iter).GetPrice()*(*iter).GetCount();
}
return m_total;
}
};
class CDiscount:public CFruitBasket
{
private:
double m_rate;
public:
void SetRate(double rate)
{
m_rate = rate;
}
double GetRate()
{
cout << "Rate is :"<<m_rate<<endl;
return this->m_rate;
}
virtual double GetTotal()
{
double ret = CFruitBasket::GetTotal()*m_rate;
cout << "ret is:"<<ret<<endl;
return ret;
}
};
class CMeetMinus:public CFruitBasket
{
private:
double m_least;
double m_minus;
public:
void SetLeast(double least)
{
m_least=least;
}
void SetMinus(double minus)
{
m_minus = minus;
}
double GetMinus()
{
return m_minus;
}
double GetLeast()
{
return m_least;
}
double GetTotal()
{
double ret = CFruitBasket::GetTotal();
if(ret>=m_least)
{
return (ret-m_minus);
}
else
return ret;
}
};
int main()
{
CFruits Apple;
Apple.SetPrice(4);
Apple.SetCount(5);
cout << "Apple Price is :"<<Apple.GetPrice()<<endl;
CFruits Orange;
Orange.SetPrice(3);
Orange.SetCount(10);
cout << "Orange Price is :"<<Orange.GetPrice()<<endl;
CFruits Durian;
Durian.SetPrice(18.8);
Durian.SetCount(5);
cout << "Durian Price is :"<<Durian.GetPrice()<<endl;
vector <CFruits> vfruits;
vfruits.push_back(Apple);
vfruits.push_back(Orange);
vfruits.push_back(Durian);
CDiscount discountA;
discountA.SetRate(0.8);
discountA.GetRate();
CMeetMinus minusA;
minusA.SetLeast(100);
minusA.SetMinus(20);
CFruitBasket *basket = NULL;
cout<<"Please enter your choice:"<<endl;
int in;
cin>>in;
switch(in)
{
case 0:
cout<<"NO DISCOUNT!"<<endl;
basket = new CFruitBasket;
break;
case 1:
cout<<"STRATEGY 1!"<<endl;
basket =&discountA ;
break;
case 2:
cout<<"STRATEGY 2!"<<endl;
basket =&minusA ;
break;
default:
cout<<"WRONG NUMBER!"<<endl;
break;
}
basket->SetFruit(vfruits);
cout<<"Total is :"<<basket->GetTotal()<<endl;
return 0;
}