Hello everybody,

I'm back, started my 2nd year at Uni now and started programming in the wonder that is C++ and enjoying it tremendously.

but I've hit a slight problem, not with code but with design.

I'm not sure where to put the input/output, sounds weird but I'll elaborate.

i have all my base classes sorted i have a Bank class which contains arrays of Accounts and Customers and contains all the functions required to add/delete/display accounts and customers.

its only a simple console application using a simple menu and the user simply enters a number 1 - 9 (as a char) to navigate the menu.

what i'm confused about is where to put the menu output and where to read the menu input, do i put it all in my bank class or to i put it in my main class.

my original idea was to have all of the menu output in functions within the bank class; for example.

void Bank::mainMenu()
{
	do
	{
		char menu;
		system("cls");
		draw();
		cout << "Main Menu" << endl;
		draw();
		cout << "1 - Accounts Menu" << endl;
		cout << "2 - Cards Menu" << endl;
		cout << "3 - Lending Menu" << endl;
		cout << "4 - Investements Menu" << endl;
		cout << "5 - Exit" << endl;
		draw();
		cout << "Please Make Your Choice: ";
		cin >> menu;
		switch (menu)
		{
		case '1':
			accMenu();
			break;
		case '2':
			cardMenu();
			break;
			// only showing a couple you get the point :P
		}
	}while(menu != '5'); 
}

with this method i would call the mainMenu function on a bank object in my main.cpp file and that mainMenu function would do all the output input and validation and then display the next menu. therefore my main function would probably look something like this

int main ()
{
	Bank b;
	b.mainMenu();

	//End Sequence
	cin.ignore(1000, '\n'); //Ignores all input up till next line
	cout << "Press Enter to Continue" << endl;
	cin.get(); //makes the program wait before continuing 
}

but i'm worried that this would be going against oop concepts and i'm just wondering what other members of the community think.

Thanks in advance for any help

-Midi

Recommended Answers

All 3 Replies

I suppose there are as many ways to design your program as there are people to program it. But if I were going to do it I would not put the menu in the Bank class at all but put it in the main() function.

int DisplayMenu();
const int DONE = 5 // menu item 5
int main()
{
    Bank bank;
    int option;
    while( (option = DisplayMenu()) != DONE )
    {
          switch(option)
          {
               case 1:
                   // do something here
                   break;
               <etc, etc for the other menu items
          }
      }
}
int DisplayMenu()
{
int choice = 0;
system("cls");
draw();
cout << "Main Menu" << endl;
draw();
cout << "1 - Accounts Menu" << endl;
cout << "2 - Cards Menu" << endl;
cout << "3 - Lending Menu" << endl;
cout << "4 - Investements Menu" << endl;
cout << "5 - Exit" << endl;
draw();
cout << "Please Make Your Choice: ";
cin >> choice;
return choice;
}

You could also make a Menu class to encapsulate the banking operation. However, it might be better to specialize your Menu and give it a name (and make an 'abstract' base class Menu for general Menu's ).

-Alex

In C++ if designer properly you could easily avoid switch cases ,etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.