-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuList.cpp
More file actions
169 lines (149 loc) · 3.78 KB
/
Copy pathmenuList.cpp
File metadata and controls
169 lines (149 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* menuList.cpp
*
* The menuList class implements a very simple menu system.
*
* The menu is made up of a collection of menuItem values.
* Each menuItem contains a character for the menu (such as
* a for add), descriptive text (such as "Add an item"), and
* a command object. The command object is executed when
* the corresponding menu item is selected.
*
* Note that the menuList class does not know what is being
* done by the command objects, so this allows us to separate
* the menu from what is actually being executed as a result
* of the menu item being selected.
*
* Note that a menu item of "q exit the menu" will always be
* the last item in the menu displayed by menuList.
*
* The add member function of the menuList class adds an
* object of type menuItem to the menu. The start() member
* function starts the display / processing loop for the
* application. The start member function stops when the
* application user types in the quit command - q
*
* Author: dgv130030
*/
#include <iostream>
#include <string>
#include "menuList.h"
// create menuList with default menu heading
menuList::menuList()
: itemList(0), menuText(TITLE_TEXT)
{
}
// create menuList with the specified menu heading
menuList::menuList(const std::string &menuText)
: itemList(0), menuText(menuText)
{
}
// add an item to the list
void menuList::add(menuItem &item)
{
itemList.push_back(item);
}
// start the menu process
void menuList::start() const
{
char selectChar;
do
{
// display the menu
displayMenu();
// get the menu item selected
selectChar = getSelection();
// if it isn't quit process it
if (selectChar != QUIT)
{
bool valid = false;
// first find it
for (auto item : itemList)
{
if (selectChar == item.select())
{
// we found the menu item for the
// (valid) selectChar value
valid = true;
// execute the command
item.execute();
// no need to process the rest of
// the menu items
break;
}
}
if (valid)
{
// it was valid, just output a blank line
std::cout << std::endl;
}
else
{
// the item was not valid, display a message and try again
std::cout << "The menu item '" << selectChar << "' was invalid" << std::endl;
std::cout << std::endl;
}
}
}
while (selectChar != QUIT);
}
// display all of the menu item values
void menuList::displayMenu() const
{
std::cout << menuText << std::endl;
for (auto &item : itemList)
{
std::cout << item.select();
std::cout << " ";
std::cout << item.description();
std::cout << std::endl;
}
// display our quit option
std::cout << QUIT << " " << QUIT_TEXT << std::endl;
}
// get the menu item selection char from the user
char menuList::getSelection() const
{
char returnValue;
std::string text;
// read in the input buffer
getline(std::cin, text);
if (text.length() == 0)
{
// if it is empty return the null character
returnValue = '\0';
}
else
{
// return the character entered
returnValue = text[0];
}
return returnValue;
}
// create the dummyCommand
menuItem::quitCommand menuItem::dummyCommand;
// dummy command
menuItem::menuItem()
: theSelectChar('q'), theDescription("Quit"), theCommand(dummyCommand)
{
}
// actual, useful, command
menuItem::menuItem(char selectChar, const std::string &descriptionText, command &cmd)
: theSelectChar(selectChar), theDescription(descriptionText), theCommand(cmd)
{
}
//return selection char
char menuItem::select() const
{
return theSelectChar;
}
// return description
std::string menuItem::description() const
{
return theDescription;
}
// execute the command
void menuItem::execute()
{
theCommand.execute();
}