How to merge two functions with same conditions?

  • A+

I quickly wrote the below class for this question.

I'm looking for a way to merge addFruit() with removeFruit() to reduce the code.

They both use identical conditions but just different function call at the end.

My Code :

#include <iostream> #include <string> #include <vector>  class MyClass { public:     void addFruit(const std::string &str, int count)     {         if (str == "apples")             addToVec(apples, count);         else if (str == "oranges")             addToVec(oranges, count);         else if (str == "lemons")             addToVec(lemons, count);         else if (str == "melons")             addToVec(melons, count);         else if (str == "bananas")             addToVec(bananas, count);         else             std::cout << "Unknown Fruit : " << str << '/n';     }     void removeFruit(const std::string &str)     {         if (str == "apples")             removeFromVec(apples);         else if (str == "oranges")             removeFromVec(oranges);         else if (str == "lemons")             removeFromVec(lemons);         else if (str == "melons")             removeFromVec(melons);         else if (str == "bananas")             removeFromVec(bananas);         else             std::cout << "Unknown Fruit : " << str << '/n';     } private:     void addToVec(std::vector<int> &vec, int count)     {         vec.push_back(count);     }     void removeFromVec(std::vector<int> &vec)     {         vec.pop_back();     }     std::vector<int> apples;     std::vector<int> oranges;     std::vector<int> lemons;     std::vector<int> melons;     std::vector<int> bananas; }; 

Any clever way to nicely merge the two functions so I can reduce the code?


Make an additional function e.g. determineTargetVector(const std::string &str) which returns the corresponding vector, where you want to insert/remove an element, so you have no redundant conditions. Also its nice to have only a single reponsibility for each function.

For example:

std::vector<int> *determineTargetVector(const std::string &str) {     if (str == "apples")         return &apples;     else if (str == "oranges")         return &oranges;     else if (str == "lemons")         .         .         .     else         //something invalid, to check for in superior function         return nullptr; } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: