要求:
1.根据顾客要求定义hotdog,计算总价
2.按uml类图编写
测试样例:
int main() {
auto h = new Hotdog(new WheatBread());
h->addFood(new Beef());
h->addFood(new Cheese());
h->addFood(new Mustard());
h->addFood(new Onion());
h->addFood(new Onion());
h->addFood(new Tomato());
h->printDetails();
}
#include<iostream>
#include<vector>
using namespace std;
class FoodIngredients{
int m_price;
public:
FoodIngredients(int price){m_price=price;}
int getPrice()const{return m_price;}
virtual void printDetail()=0;
};
class Bread:public FoodIngredients{
public:
Bread(int price):FoodIngredients(price){}
void printDetail(){cout<<"Bread"<<" "<<getPrice()<<endl;}
};
class Wheat_bread:virtual public Bread{
public:
Wheat_bread(int price=7):Bread(price){}
void printDetail(){cout<<"Wheat_bread"<<" "<<getPrice()<<endl;}
};
class Honey_break:virtual public Bread{
public:
Honey_break(int price=8):Bread(price){}
void printDetail(){cout<<"Honey_break"<<" "<<getPrice()<<endl;}
};
class Bacon:virtual public FoodIngredients{
public:
Bacon(int price=8):FoodIngredients(price){}
void printDetail(){cout<<"Bacon"<<" "<<getPrice()<<endl;}
};
class Beef:virtual public FoodIngredients{
public:
Beef(int price=12):FoodIngredients(price){}
void printDetail(){cout<<"Beef"<<" "<<getPrice()<<endl;}
};
class Onion:virtual public FoodIngredients{
public:
Onion(int price=1):FoodIngredients(price){}
void printDetail(){cout<<"Onion"<<" "<<getPrice()<<endl;}
};
class Tomoto:virtual public FoodIngredients{
public:
Tomoto(int price=2):FoodIngredients(price){}
void printDetail(){cout<<"Tomoto"<<" "<<getPrice()<<endl;}
};
class Lettuce:virtual public FoodIngredients{
public:
Lettuce(int price=1):FoodIngredients(price){}
void printDetail(){cout<<"Lettuce"<<" "<<getPrice()<<endl;}
};
class Cheese:virtual public FoodIngredients{
public:
Cheese(int price=2):FoodIngredients(price){}
void printDetail(){cout<<"Chess"<<" "<<getPrice()<<endl;}
};
class Mustard:virtual public FoodIngredients{
public:
Mustard(int price=1):FoodIngredients(price){}
void printDetail(){cout<<"Mustard"<<" "<<getPrice()<<endl;}
};
class Hotdog{
Bread *bread;
vector<FoodIngredients *> v;//虚基类无法有对象
public:
Hotdog(Bread *b){bread=b;}
bool addFood(FoodIngredients *f){v.push_back(f);}
bool setbreak(Bread *b){}
void printDetails(){
cout<<"消费表单:"<<endl;
bread->printDetail();
for(vector<FoodIngredients *>::iterator it=v.begin();it!=v.end();it++){
FoodIngredients *f=*it;
f->printDetail();
}
}
int getTotalPrice(){
int total=0;
total+=bread->getPrice();
for(vector<FoodIngredients *>::iterator it=v.begin();it!=v.end();it++){
FoodIngredients *f=*it;
total+=f->getPrice();
}
return total;
}
};
int main(){
auto h = new Hotdog(new Wheat_bread());
h->addFood(new Beef());
h->addFood(new Cheese());
h->addFood(new Mustard());
h->addFood(new Onion());
h->addFood(new Onion());
h->addFood(new Tomoto());
h->printDetails();
cout <<"总计:"<< h->getTotalPrice() << endl;
return 0;
}
结果:
消费表单:
Wheat_bread 7
Beef 12
Chess 2
Mustard 1
Onion 1
Onion 1
Tomoto 2
总计:26