Classes and functions

Hi, I know I can't ask for a program to be written for me (I wish), but I am lost and almost overwhelmed with this assignment i have to do. I just finished a simpler one over classes, but this one is much more in depth. I don't know where to begin, or where I would go from there. Any pointers would be appreciated greatly, I'll keep workin' on it.

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
 Create a c++ file to be used in a BlueBox Video Center (same idea as the RedBox)

The class must be named BlueBox.

It must have member functions named:

Inventory - track DVD inventory. Your BlueBox can hold no more than 2 copies

            of 3 different DVDs. If the user wants to rent more than 1 DVD,

            you must allow them. If they request a title that is out of

            inventory you must tell them.

Sales -     The rental fee is $1 per night. Assume everyone returns the DVDs

            within 24 hours.You must tell the customers how much they owe

            at the end of all sales.

Screen -    This member function should control all output to the screen,

            which includes the user interface.

Customers - This member function tracks which customer is renting. Give each

            customer a name and greet them each time they sign in to rent.

            There are three customers which gain access using a two digit code.

            The codes are 00 01 and 12.

Returned -  Controls the returned DVDs by a customer. These are restocked.

Admin -     Allows the owner of the machine to sign in using code 99. The screen

            should show the owner any DVDs currently in stock and not rented and

            those that have been returned. It should also show the owner how

            much money he has made on sales. Assume all revenues are profits.

 

The program shall remain running once it has started so all customers can rent and

return DVDs without rerunning the code. When all DVDs, are rented, the machine must

close down and cout a message to customers that the BlueBox is "Out of Order."

 

To get full credit, the member functions as described must be in the program. This

includes the function names given above. You are allowed to have additional member

functions if desired.
First of all, what I would do is create some kind of main menu (once the user has signed in) where the user can type in commands to say whether they want to either rent or return a DVD. You could do this by enclosing the output and input commands around a "do" loop if you know what I mean. So inside the loop you would output the options the user can choose and then get the input. Then depending on the input, send it to the corresponding member function of the class - if they wish to rent a DVD then you could call the inventory function which will then check whether the DVD is in stock or not.

To store how many of each DVD is available you could use a global array that contains the quantity of each DVD left and would contain 3 elements as there are 3 DVDs. You just simply need to check how many is left of the DVD they want to rent and then tell them whether they can rent it or not.

Hope this helps,
Tom
I wrote a first draft of what the program should look like. One thing to note: constexpr is for C++11. If your compiler is complaining, just change it to const. This code is a guide so to speak but feel free to rip it.

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;

enum UserType{USERS, ADMIN};
constexpr unsigned short MAX_TITLES = 3, MAX_COPIES = 2,
                         MAX_USERS = 4, ACCESS_CODE = 2;

class BlueBox
{
    string m_inventory[MAX_TITLES][MAX_COPIES];
    string m_users[MAX_USERS][ACCESS_CODE];
    unsigned int m_revenue;

    const unsigned short customer(const unsigned int) const;
    void rent(const unsigned short);
    void returns();
    void sales(const string&);
    void admin() const;
    void error(const string& message){system("CLS"); cout << message << endl; exit(0);}
public:
    BlueBox(const string inventory[][MAX_COPIES], const string users[][ACCESS_CODE]);
    void screen();
};

int main()
{
    const string INVENTORY[][MAX_COPIES] = {{"Brave Heart", "2"},
                                            {"Saw 4", "2"},
                                            {"Jaws", "2"}};
    const string USERS[][ACCESS_CODE] = {{"Brian", "00"},
                                         {"David", "01"},
                                         {"Brat", "12"},
                                         {"Admin", "99"}};

    BlueBox dvdStore(INVENTORY, USERS);

    dvdStore.screen();

    return 0;
}

BlueBox::BlueBox(const string inventory[][MAX_COPIES], const string users[][ACCESS_CODE]):m_revenue(0)
{
    for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
        for(unsigned short copies = 0; copies < MAX_COPIES; copies++)
            m_inventory[titles][copies] = inventory[titles][copies];

    for(unsigned short user = 0; user < MAX_USERS; user++)
        for(unsigned short codes = 0; codes < ACCESS_CODE; codes++)
            m_users[user][codes] = users[user][codes];
}
void BlueBox::screen()
{
    char answer;

    do
    {
        cout << "\t\t\tBlueBox Video Center\n\nMain Menu:\n---\n" <<
                "1. Rent\n2. Returns\n3. Admin\n4. EXIT\n---\nAnswer: ";
        cin >> answer;

        system("CLS");

        switch(answer)
        {
        case '1':
            {
                rent(customer(USERS));
                break;
            }
        case '2':
            {
                returns();
                break;
            }
        case '3':
                customer(ADMIN);
        }
    }while(answer != '4');
}
const unsigned short BlueBox::customer(const unsigned int userType) const
{
    string accessCode;

    if(userType == USERS)
        while(true)
        {
            cout << "\t\t\tBlueBox Video Center\n\nCustomer Login:\n---\nEnter Access Code: ";
            cin >> accessCode;

            system("CLS");

            for(unsigned short user = 0; user < MAX_USERS - 1; user++)
                if(m_users[user][ACCESS_CODE - 1] == accessCode)
                    return user;
        }
    else if(userType == ADMIN)
        while(true)
        {
            cout << "\t\t\tBlueBox Video Center\n\nADMIN Login:\n---\nEnter Access Code: ";
            cin >> accessCode;

            system("CLS");

            if(m_users[MAX_USERS - 1][ACCESS_CODE - 1] == accessCode)
            {
                admin();

                break;
            }
        }

    return true;
}
void BlueBox::rent(const unsigned short user)
{
    unsigned short titleCounter = 0;
    char answer;

    while(true)
    {
        cout << "\t\t\tBlueBox Video Center\nWelcome " <<
                m_users[user][0] << "!\n\nWhich Title to Rent:\n---\n";

        for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
            if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
                cout << ++titleCounter << ". " << setw(12) << m_inventory[titles][0] << " | " <<
                     m_inventory[titles][MAX_COPIES - 1] << " copies left\n";

        if(titleCounter == 0)
            error("NO DVDS TO RENT - OUT OF ORDER!\n");

        cout << ++titleCounter << ". Exit\n---\nAnswer: ";
        cin >> answer;

        system("CLS");

        if(answer - 48 == titleCounter)
            break;
        else if(answer - 48 > titleCounter || answer - 48 < 1)
        {
            titleCounter = 0;

            continue;
        }
        else
        {
            unsigned short titlePickerCounter = 0;

            for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
            {
                if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
                    titlePickerCounter++;

                if(answer - 48 == titlePickerCounter)
                {
                    m_inventory[titles][MAX_COPIES - 1].at(0)--;

                    break;
                }
            }
        }

        titleCounter = 0;
    }
}
void BlueBox::returns()
{
    unsigned short titleCounter = 0;
    char answer;

    while(true)
    {
        cout << "\t\t\tBlueBox Video Center\n\nWhich Title to Return:\n---\n";

        for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
            if(m_inventory[titles][MAX_COPIES - 1].at(0) < '2')
                cout << ++titleCounter << ". " << setw(12) << m_inventory[titles][0] << " | " <<
                     2 - atoi(m_inventory[titles][MAX_COPIES - 1].c_str()) << " copies missing\n";

        if(titleCounter == 0)
        {
            system("CLS");

            return;
        }

        cout << ++titleCounter << ". Exit\n---\nAnswer: ";
        cin >> answer;

        system("CLS");

        if(answer - 48 == titleCounter)
            break;
        else if(answer - 48 > titleCounter || answer - 48 < 1)
        {
            titleCounter = 0;

            continue;
        }
        else
        {
            unsigned short titlePickerCounter = 0;

            for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
            {
                if(m_inventory[titles][MAX_COPIES - 1].at(0) < '2')
                    titlePickerCounter++;

                if(answer - 48 == titlePickerCounter)
                {
                    m_inventory[titles][MAX_COPIES - 1].at(0)++;

                    sales(m_inventory[titles][0]);

                    break;
                }
            }
        }

        titleCounter = 0;
    }
}
void BlueBox::sales(const string& titleName)
{
    cout << "Thank you for returning " << titleName << " within 24 hours!\n---\n";
    cout << "That will cost you $1.00 - Thank you for your business!\n\n";

    system("PAUSE");
    system("CLS");

    m_revenue++;
}
void BlueBox::admin() const
{
    cout << "\t\t\tBlueBox Video Center\n\nADMIN Panel:\n---\n\nCurrent DVDs IN_STOCK:\n-\n";

    for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
        if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
            cout << setw(12) << m_inventory[titles][0] << " | " <<
                     m_inventory[titles][MAX_COPIES - 1] << " copies\n";

    cout << "\nRevenue:\n-\n$" << m_revenue << ".00\n\n";

    system("PAUSE");
    system("CLS");
}


EDIT 1: I read again the guideline to your assignment and it seems I wrote part of the code wrong. You will have to make a private/public member function named inventory(...) in which you either pass the inventory array as an argument or use a static inventory array within the function itself.

Also, it seems you need to input the DVD's name instead of selecting it. Seems redundant; its better to see what's available right? Well, I gave you a template. Just edit it. Or write your own while using mine as a guide.
Last edited on
Wow. I can see why you are confused. This is one of the worst problem descriptions I've ever seen. I have no idea what the arguments, return values, or functionality of any of those methods should be. They sound like data rather than methods to me.

So to do this assignment, I think I'd start by figuring out how it "should" work and then shoe-horn the functionality into the backward functions that have been specified.

I usually find it easiest to tackle a problem by designing the data first, then the methods. I find it easiest to use a combination of both top-down and bottom-up design. What I mean is that you can usually figure out some things that you'll need at the top level, and some things that you'll need at the bottom level. Once you specify these, you can work your way from both ends towards the center.

Hope this helps.
Ok, thanks alot for the replies. Popeye, what is the stdlib.h and what is unsigned short ? I know that is standardlibrary, but what is the .h? We haven't/learned used stdlib at all or unsigned short.
Hy jon, im doing this assignment right now for my c++ class tonight. Im about as lost as you are
The ".h" stands for "header." It's part of a class definition. The function definitions are somewhere else (you don't need to worry about those).
Unsigned short is a form of integer variable. "Unsigned" means it is always positive, and "short" means it holds a smaller value than a regular integer can.

Also, I agree with dehayden. The problem description is horrible. This reminds me of the programming projects I had to do back a couple of programming courses ago.

For an article about stdlib: http://www.cplusplus.com/reference/cstdlib/
Ok, thanks for the link I'll check that out. Also, do I need the unsigned short in my program? Why not just int? Also also, which functions are used (in the above program) by the stdlib.h ?
You don't need unsigned short. Using an unsigned integer just ensures the number will never be negative. Using "short" takes up less memory (but that isn't really important for a smaller program). Using an int would do the same job.

I don't have stdlib memorized by any means, but the atoi function call on line 181 comes from "stdlib".
Jon, I'm in the same class as you. How is this program going for you. I'm still having lots of trouble.
Please let us know what troubles you are having. We're here to help. We can't just give you the answers, but we can help you along the way to finding a solution.
In PopEye's original code, what do lines 140-168 (and similarly 195-225) do exactly? why do you use "48" specifically?
nevermind, I figured it out
But why 48?
@Jon15
---
To give you an example on why we need a constant 48 I will need to give you a scenario. Lets look at lines 117 through 168 (below will be that section):

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
void BlueBox::rent(const unsigned short user)
{
    unsigned short titleCounter = 0;
    char answer;

    while(true)
    {
        cout << "\t\t\tBlueBox Video Center\nWelcome " <<
                m_users[user][0] << "!\n\nWhich Title to Rent:\n---\n";

        for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
            if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
                cout << ++titleCounter << ". " << setw(12) << m_inventory[titles][0] << " | " <<
                     m_inventory[titles][MAX_COPIES - 1] << " copies left\n";

        if(titleCounter == 0)
            error("NO DVDS TO RENT - OUT OF ORDER!\n");

        cout << ++titleCounter << ". Exit\n---\nAnswer: ";
        cin >> answer;

        system("CLS");

        if(answer - 48 == titleCounter)
            break;
        else if(answer - 48 > titleCounter || answer - 48 < 1)
        {
            titleCounter = 0;

            continue;
        }
        else
        {
            unsigned short titlePickerCounter = 0;

            for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
            {
                if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
                    titlePickerCounter++;

                if(answer - 48 == titlePickerCounter)
                {
                    m_inventory[titles][MAX_COPIES - 1].at(0)--;

                    break;
                }
            }
        }

        titleCounter = 0;
    }
}


When the function is called, 3 Bytes are allocated for titleCounter and answer. titleCounter is used as a counter per say. In the for loop, if a title as 1 or more copies:

if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')

Then titleCounter gets incremented. So if we have 20 different titles and have 1 copy for each available title, then titleCounter will be 20. But, if we have 20 different titles and 4 of them has no copies available:

m_inventory[titles][MAX_COPIES - 1].at(0) == '0')

Then titleCounter will be 16. That being said, the titles displayed on the screen will be the ones with copies available for renting.

Lets say for example, 2 titles were displayed with the addition of exiting:
1. Jaws
2. The Grinch
3. Exit

You will be prompted to pick a title. Lets say you want to pick 'The Grinch' which is '2'. You type '2' and then you press enter. Your answer will be stored in answer which is of type char. The reason we have answer - 48, is because '2' is really 50. Whenever you use char, your storing characters that are part of the ASCII. Every character is represented by a number (internally). In this case, '2' is represented by 50. So the constant 48 is used to properly validate the conditions in most of the code.

Hope I explained clearly.
Topic archived. No new replies allowed.