I'm creating a console application and have the need for a menu. This menu can link to a next menu or preform an action. What is a good OOP way of doing this?
I've seen many do it like this, but isn't really maintainable
TJ::clearScreen(); // Function I wrote to clear the console
TJ::breakSection('='); // Creates a line of "===...==="
std::cout << "Main menu" << std::endl;
TJ::breakSection();
std::cout << "[1] abcd" << std::endl;
std::cout << "[2] hrhdd" << std::endl;
std::cout << "[3] aeshsrh" << std::endl;
TJ::breakSection('=');
std::cin >> choice;
switch(choice) {
case 1:
doThing1();
break;
case 2:
doThing1();
break;
case 3:
doThing1();
break;
}
Is there a way to make it look like this?
menu.addChoice(functionA);
menu.addChoice(menuB);
menu.addChoice(functionC);
menu.addChoice(menuD);
menu.display();
So if the user chooses option A (by typing "A" into the console), it will execute functionA. If the user chooses option B, it will show menuB.
I've looked at doing something like you see below. But I can't figure out how to allow both functions and menus to be chosen.
class Menu {
private:
std::string title;
class Item {
private:
public:
Item();
~Item();
int run();
int display();
};
std::vector<Item> Choices;
public:
Menu();
~Menu();
Menu& addChoice();
Menu& display();
};