-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.cpp
executable file
·149 lines (135 loc) · 3.85 KB
/
menu.cpp
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
/******************************************************
Class Name: Menu
Author: Howard Chen
Date: 3/19/2017
Description: A console menu that can be edited before
being displayed.
************************************************8******/
#include "menu.hpp"
#include <iostream>
//Description: Default constructor for a Menu Object.
//Arguments: none
//Returns Menu object
//Precondition: none.
//Postcondition: Menu object is created completely empty.
Menu::Menu()
{
}
//Description: Get one of the menu choices.
//Arguments: number of the choice.
//Returns a const char* representing the message.
//Precondition: Menu must have at least one choice.
//Postcondition: Either the menu choice is returned,
// or an error message is returned.
std::string Menu::getChoice(unsigned int num)
{
if(num < choice.size())
return choice[num];
else
return "ERROR: CHOICE NUMBER IS OUT OF BOUNDS";
}
//Description: Get the number of choices in the menu.
//Arguments: none
//Returns: number of menu choices.
//Precondition: Menu must exist.
//Postcondition: Menu's choice number is returned.
int Menu::getNumChoices()
{
return choice.size();
}
//Description: Validates a menu choice.
//Arguments: a number representing the menu choice.
//Returns boolean: whether or not the menu choice is valid.
//Precondition: Menu should have at least one choice.
//Postcondition: Returns true or false.
bool Menu::validate(unsigned int num)
{
if(num < choice.size())
return true;
else
return false;
}
//Description: Adds options to the menu.
//Arguments: c-string representing the menu choice.
//Returns nothing.
//Precondition: None.
//Postcondition: Menu will have one extra option.
void Menu::addChoice(std::string option)
{
choice.push_back(option);
}
//Description: Swaps menu option positions.
//Arguments: option positions that will be swapped.
//Returns nothing.
//Precondition: Menu must have at least two options.
//Postcondition: Menu options are swapped.
void Menu::swapChoice(unsigned int first, unsigned int second)
{
//Check if swap positions are valid choices.
unsigned int bound = choice.size();
//If they are, swap them. Otherwise, print an
//error message.
if(first < bound && second < bound)
{
std::string temp = choice[first];
choice[first] = choice[second];
choice[second] = temp;
}
else
std::cout << "ERROR: OUT OF BOUNDS" << std::endl;
}
//Description: Display the menu options to a user.
//Arguments: none.
//Returns nothing.
//Precondition: Menu object must have at least one option.
//Postcondition: Menu will be printed to console.
void Menu::display()
{
std::cout << "Menu Options" << std::endl;
for(unsigned int i = 0; i < choice.size(); ++i)
{
std::cout << i << '\t' << choice[i] << std::endl;
}
}
//Description: removes a specified choice from the menu.
//Arguments: position of the menu choice.
//Returns nothing.
//Precondition: Menu must have at least one choice.
//Postcondition: Menu option is removed.
void Menu::removeChoice(unsigned int num)
{
//Print an error message if menu is empty.
if(choice.empty())
std::cout << "ERROR: Empty Menu!" << std::endl;
//Remove option if it is valid, otherwise print an
//error message.
else if (num < choice.size())
{
unsigned int last = choice.size() - 1;
std::string tmp = choice[num];
choice[num] = choice[last];
choice[last] = tmp;
choice.pop_back();
}
else
std::cout << "ERROR: OUT OF BOUNDS" << std::endl;
}
/*
Description: Returns length of menu.
Arguments: None.
Precondition: None.
Postcondition: Returns length of menu.
*/
int Menu::length()
{
return this->choice.size();
}
/*
Description: Clears menu of all choices.
Arguments: None.
Precondition: None.
Postcondition: Menu is cleared of all choices.
*/
void Menu::clear(){
choice.clear();
}