1: //test_menu1.cpp 2: //A test driver for the Scobey::Menu class. 3: //Illustrates the construction and use of several Menu objects. 5: #include <iostream> 6: #include <string> 7: using namespace std; 9: #include "utilities.h" 10: using Scobey::Menu; 11: using Scobey::Pause; 13: int main() 14: { 15: Pause(0, "\nTesting the Scobey::Menu class ..." 16: "\n\nFirst we declare a default Menu object, " 17: "and then display it."); 18: Menu menu1; 19: menu1.display(); 20: Pause(); 22: Pause(0, "Next we declare a second menu object, this time " 23: "initializing it with a title.\nThen we add several options, " 24: "displaying the menu after adding each one.\nAs we do this, " 25: "notice how the menu display remains \"centered\"."); 26: Menu menu2("Main Menu"); 27: menu2.display(); 28: Pause(); 29: menu2.addOption("Quit"); 30: menu2.display(); 31: Pause(); 32: menu2.addOption("Get information"); 33: menu2.display(); 34: Pause(); 35: menu2.addOption("Do something"); 36: menu2.display(); 37: Pause(); 38: menu2.addOption("Do something really, really very long and involved"); 39: menu2.display(); 40: Pause(); 42: Pause(0, "Now we loop while the user does not choose the Quit option, " 43: "i.e., while the\nuser does not choose option 1 from the menu, " 44: "and while the user does not enter\n3 invalid options in a row."); 45: int menuChoice; 46: bool finished; 47: do 48: { 49: menu2.display(); 50: menuChoice = menu2.getChoice(); 51: finished = (menuChoice == 1 || menuChoice == -1); 52: switch (menuChoice) 53: { 54: case -1: 55: case 1: 56: cout << "Now quitting from this menu.\n"; 57: break; 58: case 2: 59: cout << "Now getting information.\n"; 60: break; 61: case 3: 62: cout << "Now doing something.\n"; 63: break; 64: case 4: 65: cout << "Now doing something really, really " 66: "very long and involved.\n"; 67: break; 68: } 69: Pause(); 70: } 71: while (!finished); 74: Pause(0, "Now we create a third menu with 20 options (the " 75: "maximum number). Note how the\nperiods following the option " 76: "numbers line up, and continue to line up when the\noption " 77: "number goes from one digit to two digits."); 78: Menu menu3("Test Menu"); 79: menu3.addOption("Option 1 permits you to quit"); 80: menu3.addOption("Option 2 allows you to do this"); 81: menu3.addOption("Option 3 lets you do something just a bit longer"); 82: menu3.addOption("Option 4 just lets you do it"); 83: menu3.addOption("Option 5 lets you do that"); 84: menu3.addOption("Option 6 lets you do it now, not later"); 85: menu3.addOption("Option 7 lets you do whatever"); 86: menu3.addOption("Option 8 requires you to do it quickly if you can"); 87: menu3.addOption("Option 9 lets you do it and take your time"); 88: menu3.addOption("Option 10 lets you do it over and over again"); 89: menu3.addOption("Option 11 lets you do it with style"); 90: menu3.addOption("Option 12 requires it to be done by Monday"); 91: menu3.addOption("Option 13 asks that you do it next week"); 92: menu3.addOption("Option 14 demands that it be done yesterday"); 93: menu3.addOption("Option 15 doesn't care if it's done at all"); 94: menu3.addOption("Option 16 lets you do it whenever you like"); 95: menu3.addOption("Option 17 requires it to happen every Friday"); 96: menu3.addOption("Option 18 suggests you do it Sundays and Mondays"); 97: menu3.addOption("Option 19 asks for a full report in the morning"); 98: menu3.addOption("Option 20 asks you to undo it"); 99: menu3.display(); 100: Pause(); 102: Pause(0, "Now we loop again, and once again we continue while " 103: "the user does not choose\nthe Quit option, and while the user " 104: "does not enter 2 invalid options in a row\n(last time it was 3, " 105: "remember). Note that the user prompt is different this\ntime as " 106: "well. So, observe that the client can change both the maximum " 107: "number\nof tries the user is allowed when trying to make a valid " 108: "menu choice, as well\nas the user prompt."); 109: do 110: { 111: menu3.display(); 112: menuChoice = menu3.getChoice(2, "Enter choice here: "); 113: finished = (menuChoice == 1 || menuChoice == -1); 114: cout << "\nThe menu choice returned was " << menuChoice << ".\n"; 115: Pause(); 116: } 117: while (!finished); 120: Pause(0, "Now create a fourth menu, again with 20 options and, " 121: "on this occasion, a very\nlong title. Note the position of " 122: "that title on the line. Study the source\ncode to see how the " 123: "options are created and added, even though the technique\nis " 124: "not particularly useful, except in situations like this."); 125: Menu menu4("Another Test Menu with a Very, Very, Very, " 126: "Very Long Title"); 127: string commonText("Option "), newOption; 128: for (int i=1; i<=20; i++) 129: { 130: newOption = (i>9) ? commonText + char('0' + i/10) : commonText; 131: newOption = newOption + char('0' + i%10); 132: menu4.addOption(newOption); 133: } 134: menu4.display(); 135: Pause(); 138: Pause(0, "Finally, we create a fifth, rather bizarre menu, " 139: "as one more example to show\nthat its display is at least " 140: "reasonable. We show the menu before, and after,\nadding " 141: "the last option, which is again very long."); 142: Menu menu5("A Final Test Menu with a Very, Very, Very, Very Long Title"); 143: for (int i=1; i<=19; i++) 144: { 145: newOption = (i>9) ? commonText + char('0'+i/10) : commonText; 146: newOption = newOption + char('0' + i%10); 147: menu5.addOption(newOption); 148: } 149: menu5.display(); 150: Pause(); 151: menu5.addOption("And this of course is a very, very, very long option"); 152: menu5.display(); 153: Pause(); 155: Pause(0, "No more tests to perform." 156: "\nProgram will now terminate."); 157: }