View difference between Paste ID: 1uj40Fw8 and 5KgjsUK7
SHOW: | | - or go back to the newest paste.
1
void initiateMenu()
2
{
3
    std::string selection;
4
5
    /**
6
     * Entry data content
7
     */
8
    struct entry {
9
        std::string state;
10
        std::string description;
11
        int minAuth;
12
        int maxAuth;
13
        void (*fptr)();
14
        bool active = false;
15
    };
16
17
    /**
18
     * Menu list
19
     */
20
    std::map<std::string, entry> entries;
21
22
    /**
23
     * add menu entries!
24
     */
25
    entries["KR"] = {"/",       "Customer registration",                    GUEST,      ADMIN, registerCustomer};
26
    entries["KL"] = {"/",       "Customer login",                           GUEST,      GUEST, loginCustomer};
27
    entries["KU"] = {"/",       "Customer logout",                          CUSTOMER,   ADMIN, logoutCustomer};
28
    entries["KS"] = {"/",       "Display my items",                         CUSTOMER,   ADMIN, displayCustomerItems};
29
    entries["KE"] = {"/",       "Update customers authorization access",    ADMIN,      ADMIN, setAuthorityLevel};
30
31
    /**
32
     * Start the program loop.
33
     */
34
    do {
35
        bool authenticated = customers->authenticated();
36
        int authorization  = authenticated ? customers->session()->returnAuth() : GUEST;
37
38
        /**
39
         * Display that a customer is logged in
40
         *  if his / her session is successfully set.
41
         */
42
        if (customers->authenticated()) {
43
            std::cout
44
                << "Logged in as: "
45
                << customers->session()->username()
46
                << std::endl;
47
        }
48
49
        /**
50
         * Print the menu, with command options.
51
         *
52
         * ROOT_STATE == /
53
         */
54
        std::cout << "------------------ MENU ------------------" << std::endl;
55
        for (auto & i : entries) {
56
            i.second.active = false;
57
58
            std::string state   = i.second.state;
59
            int minAuth         = i.second.minAuth;
60
            int maxAuth         = i.second.maxAuth;
61
62
            /**
63
             * If the current entry does not follow the rules for being displayed,
64
             *  we ignore the rest of the round and go to the next entry.
65
             */
66
            if (state != "*") {
67
                if (authenticated && state != customers->state()) {
68
                    continue;
69
                }
70
71
                if (!authenticated && minAuth > GUEST) {
72
                    continue;
73
                }
74
75
                if (authenticated && minAuth > authorization) {
76
                    continue;
77
                }
78
79
                if (authenticated && maxAuth < authorization) {
80
                    continue;
81
                }
82
83
                if (!authenticated && state != ROOT_STATE) {
84
                    continue;
85
                }
86
            }
87
88
            /**
89
             * prints out the menu entry: COMMAND - description
90
             */
91
            std::cout
92
                << i.first
93
                << " - "
94
                << i.second.description
95
                << std::endl;
96
97
            /**
98
             * let the program know which commands were displayed.
99
             */
100
            i.second.active = true;
101
        }
102
103
        /**
104
         * Retrieves the selected option as a string by the customer.
105
         *  The string is then converted to an enum, and
106
         */
107
        selection = getInput("Menu choice", 1, 2, true);   // Get command from user. Lengths; min: 1, max: 2. Uppercase.
108
109
        /**
110
         * When the returned position isn't set to the end()[index value]
111
         *  there was a match.
112
         */
113
        if (entries.find(selection) != entries.end()) {
114
            //set user state
115
            //TODO: add this after function pointer, and add a way for categories to use their ID in stead.
116
            customers->updateState(selection);
117
118
            // call function stored in entry
119
            (*entries.at(selection).fptr)(); //TODO: do i need to add a * in front of entries. ?
120
        }
121
122
    } while ( !(selection == "Q" && customers->state() == "/") );
123
}
124-
}
124+
125
/* OUTPUT
126
------------------ MENU ------------------
127
Q  - Quit current task / program
128
KL - Customer login
129
KR - Customer registration
130
Menu choice:
131
*/