00001 #include <iostream> 00002 #include <vector> 00003 #include <string> 00004 using namespace std; 00005 00006 class Command{ 00007 public: 00008 Command(void){}; 00009 virtual void execute(void) =0; 00010 virtual ~Command(void){}; 00011 }; 00012 00013 class Ingredient : public Command { 00014 public: 00015 Ingredient(string amount, string ingredient){ 00016 _ingredient = ingredient; 00017 _amount = amount; 00018 } 00019 void execute(void){ 00020 cout << " *Add " << _amount << " of " << _ingredient << endl; 00021 } 00022 private: 00023 string _ingredient; 00024 string _amount; 00025 }; 00026 00027 class Step : public Command { 00028 public: 00029 Step(string action, string time){ 00030 _action= action; 00031 _time= time; 00032 } 00033 void execute(void){ 00034 cout << " *" << _action << " for " << _time << endl; 00035 } 00036 private: 00037 string _time; 00038 string _action; 00039 }; 00040 00041 class CmdStack{ 00042 public: 00043 void add(Command *c) { 00044 commands.push_back(c); 00045 } 00046 void createRecipe(void){ 00047 for(vector<Command*>::size_type x=0;x<commands.size();x++){ 00048 commands[x]->execute(); 00049 } 00050 } 00051 void undo(void){ 00052 if(commands.size() > 0) { 00053 commands.pop_back(); 00054 } 00055 else { 00056 cout << "Can't undo" << endl; 00057 } 00058 } 00059 private: 00060 vector<Command*> commands; 00061 }; 00062 00063 int main(void) { 00064 CmdStack list; 00065 00066 //Create ingredients 00067 Ingredient first("2 tablespoons", "vegetable oil"); 00068 Ingredient second("3 cups", "rice"); 00069 Ingredient third("1 bottle","Ketchup"); 00070 Ingredient fourth("4 ounces", "peas"); 00071 Ingredient fifth("1 teaspoon", "soy sauce"); 00072 00073 //Create Step 00074 Step step("Stir-fry","3-4 minutes"); 00075 00076 //Create Recipe 00077 cout << "Recipe for simple Fried Rice" << endl; 00078 list.add(&first); 00079 list.add(&second); 00080 list.add(&step); 00081 list.add(&third); 00082 list.undo(); 00083 list.add(&fourth); 00084 list.add(&fifth); 00085 list.createRecipe(); 00086 cout << "Enjoy!" << endl; 00087 return 0; 00088 } 00089 00090 /*Output 00091 Recipe for simple Fried Rice 00092 *Add 2 tablespoons of vegetable oil 00093 *Add 3 cups of rice 00094 *Stir-fry for 3-4 minutes 00095 *Add 4 ounces of peas 00096 *Add 1 teaspoon of soy sauce 00097 Enjoy! 00098 */