Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void initiateMenu()
- {
- std::string selection;
- /**
- * Entry data content
- */
- struct entry {
- std::string state;
- std::string description;
- int minAuth;
- int maxAuth;
- void (*fptr)();
- bool active = false;
- };
- /**
- * Menu list
- */
- std::map<std::string, entry> entries;
- /**
- * add menu entries!
- */
- entries["KR"] = {"/", "Customer registration", GUEST, ADMIN, registerCustomer};
- entries["KL"] = {"/", "Customer login", GUEST, GUEST, loginCustomer};
- entries["KU"] = {"/", "Customer logout", CUSTOMER, ADMIN, logoutCustomer};
- entries["KS"] = {"/", "Display my items", CUSTOMER, ADMIN, displayCustomerItems};
- entries["KE"] = {"/", "Update customers authorization access", ADMIN, ADMIN, setAuthorityLevel};
- /**
- * Start the program loop.
- */
- do {
- bool authenticated = customers->authenticated();
- int authorization = authenticated ? customers->session()->returnAuth() : GUEST;
- /**
- * Display that a customer is logged in
- * if his / her session is successfully set.
- */
- if (customers->authenticated()) {
- std::cout
- << "Logged in as: "
- << customers->session()->username()
- << std::endl;
- }
- /**
- * Print the menu, with command options.
- *
- * ROOT_STATE == /
- */
- std::cout << "------------------ MENU ------------------" << std::endl;
- for (auto & i : entries) {
- i.second.active = false;
- std::string state = i.second.state;
- int minAuth = i.second.minAuth;
- int maxAuth = i.second.maxAuth;
- /**
- * If the current entry does not follow the rules for being displayed,
- * we ignore the rest of the round and go to the next entry.
- */
- if (state != "*") {
- if (authenticated && state != customers->state()) {
- continue;
- }
- if (!authenticated && minAuth > GUEST) {
- continue;
- }
- if (authenticated && minAuth > authorization) {
- continue;
- }
- if (authenticated && maxAuth < authorization) {
- continue;
- }
- if (!authenticated && state != ROOT_STATE) {
- continue;
- }
- }
- /**
- * prints out the menu entry: COMMAND - description
- */
- std::cout
- << i.first
- << " - "
- << i.second.description
- << std::endl;
- /**
- * let the program know which commands were displayed.
- */
- i.second.active = true;
- }
- /**
- * Retrieves the selected option as a string by the customer.
- * The string is then converted to an enum, and
- */
- selection = getInput("Menu choice", 1, 2, true); // Get command from user. Lengths; min: 1, max: 2. Uppercase.
- /**
- * When the returned position isn't set to the end()[index value]
- * there was a match.
- */
- if (entries.find(selection) != entries.end()) {
- //set user state
- //TODO: add this after function pointer, and add a way for categories to use their ID in stead.
- customers->updateState(selection);
- // call function stored in entry
- (*entries.at(selection).fptr)(); //TODO: do i need to add a * in front of entries. ?
- }
- } while ( !(selection == "Q" && customers->state() == "/") );
- }
- /* OUTPUT
- ------------------ MENU ------------------
- Q - Quit current task / program
- KL - Customer login
- KR - Customer registration
- Menu choice:
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement