Looking for feedback

Hello all,

We just started learning about classes in class (no pun intended), and while I have a fairly good grasp of how to do everything, I'm positive there are much more efficient and overall better ways of doing things. We've been given an extra credit project of rewriting our last program (where we made a vending machine with 10 items), using classes instead of structs. My program is all finished and working, however I'd like to get some feedback on how I can leverage classes better. Thanks in advance!!!

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
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>

using namespace std;

class coins        // Structure for vending machine coins
{
 public:
    void setCoins(int dollars, int quarters, int dimes, int nickels);
    void requestMoney();
    bool checkChange(coins buffer, coins cash, coins& returned, int cost);
    void dispenseChange(coins buffer, coins& cash, coins returned, int cost);
    int getDollar();
    int getQuarter();
    int getDime();
    int getNickel();
 private:
    int dollar, quarter, dime, nickel;
};

class stock        // Structure for vending machine stock
{
  public:
    void setInventory(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10);
    void setPrice(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10);
    void setStockNames(string slot1, string slot2, string slot3, string slot4, string slot5, string slot6, string slot7, string slot8, string slot9, string slot10);
    void displayMenu();
    void dispenseItem(int item);
    int getItem(int item);
    string getName(int item);
    bool checkStock(int item);
  private:
    int stockItem[10];
    string stockName[10];
};

int main()
{
    coins cash, buffer, returned;       // Declare variables for the coins struct.
    stock inventory, price;             // Declare variables for the stock struct.
    int choice, item;                   // Declare the integer "choice" for us in the switch statement
    char answer;

    cash.setCoins(300,75,30,15);                            // Call function to set the initial amount of cash in the machine.
    inventory.setInventory(3,3,3,3,3,3,3,3,3,3);            // Call function to set the amount of inventory for each item.
    inventory.setStockNames("a Box of Paper Clips. What were you thinking?","a cup of Coffee.","an Elephant. (Shipping not included)", "some flowers.", "a pint of Guiness. (Must be 21 or older, except in Germany)","a 20oz Pepsi. Goes great with a sandwhich!","a Boston Terrier puppy. Give it lots of hugs!","a ham sandwhich (goes great with a Pepsi!).","a Tickle Me Elmo (watch out for crazy moms...).","an umbrella.");
    price.setPrice(15,25,200,75,100,30,150,50,250,125);     // Call function to set each item's price

    cout.setf(ios::showpoint);
    cout.setf(ios::fixed);
    cout.precision(2);

    do
    {
        returned.setCoins(0,0,0,0);                                                             // Set the returned coins value to 0
        price.displayMenu();                                                                    // Call function to display the menu

        buffer.requestMoney();                                                                  // Call function to request user to input money after seeing menu.

        cout << "\n\nChoose an item from the menu above (1-10): ";
        cin >> choice;                                                                          // Ask for user's choice from the menu.
        item = choice-1;                                                                        // Set variable 'item' to user's choice(-1).

        switch (choice)                                                                         // Switch statement using the user's choice as argument.
        {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            {
                cout << "\nYou've chosen " << inventory.getName(item) << endl;

                if(inventory.checkStock(item))                                              // If the item is in stock...
                {
                    if(buffer.checkChange(buffer, cash, returned, price.getItem(item)))     // check to see if the machine can make change
                    {
                        cash.dispenseChange(buffer, cash, returned, price.getItem(item));   // If so, take the user's money and dispense the necessary change...
                        inventory.dispenseItem(item);                                       // then dispense the item.
                    }
                }

                break;
            }
            default:                                                                    // Default option if an incorrect choice is made
            {
                cout << "\nPlease choose a valid item...\n"
                     << "Your change has been returned.\n";
                cin.clear();                                                            // Clear the cache
                cin.ignore(1000,'\n');                                                  // Ignore up to 1000 characters or a new line, whichever comes first.
            }
        }

        cout << "\nDollars left in machine: " << setw(3) << cash.getDollar()/100 << endl     // Tell the user how many dollars are left in the machine.
             << "Quarters left in machine: " << setw(2) << cash.getQuarter()/25 << endl      // Tell the user how many quarters are left in the machine.
             << "Dimes left in machine: " << setw(5) << cash.getDime()/10 << endl            // Tell the user how many dimes are left in the machine.
             << "Nickels left in machine: " << setw(3) << cash.getNickel()/5;                // Tell the user how many nickels are left in the machine.

        cout << "\n\nWould you like another item? Type Y for yes or N for no: ";        // Ask if the user wants to choose another item.
        cin >> answer;
    }while(answer=='y' || answer=='Y');


    cout << endl;   // Formatting only

    return 0;
}


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
void coins::setCoins(int dollars, int quarters, int dimes, int nickels)
{
    dollar = dollars;
    quarter = quarters;
    dime = dimes;
    nickel = nickels;
}

void stock::setInventory(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10)     // Sets the initial number of each stocked item.
{
    stockItem[0] = slot1;
    stockItem[1] = slot2;
    stockItem[2] = slot3;
    stockItem[3] = slot4;
    stockItem[4] = slot5;
    stockItem[5] = slot6;
    stockItem[6] = slot7;
    stockItem[7] = slot8;
    stockItem[8] = slot9;
    stockItem[9] = slot10;
}

void stock::setStockNames(string slot1, string slot2, string slot3, string slot4, string slot5, string slot6, string slot7, string slot8, string slot9, string slot10)
{
    stockName[0] = slot1;
    stockName[1] = slot2;
    stockName[2] = slot3;
    stockName[3] = slot4;
    stockName[4] = slot5;
    stockName[5] = slot6;
    stockName[6] = slot7;
    stockName[7] = slot8;
    stockName[8] = slot9;
    stockName[9] = slot10;
}
void stock::setPrice(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10)             // Sets the price for each item in stock.
{
    stockItem[0] = slot1;
    stockItem[1] = slot2;
    stockItem[2] = slot3;
    stockItem[3] = slot4;
    stockItem[4] = slot5;
    stockItem[5] = slot6;
    stockItem[6] = slot7;
    stockItem[7] = slot8;
    stockItem[8] = slot9;
    stockItem[9] = slot10;
}

void stock::displayMenu()          // Displays the available items and their price.
{
    cout << "\n1: " << " Paper clips" << setw(15) << "$" << stockItem[0]/100.0 << endl;
    cout << "2: " << " Coffee" << setw(20) << "$" << stockItem[1]/100.0 << endl;
    cout << "3: " << " Elephant" << setw(18) << "$" << stockItem[2]/100.0 << endl;
    cout << "4: " << " Flowers" << setw(19) << "$" << stockItem[3]/100.0 << endl;
    cout << "5: " << " Guiness (pint)" << setw(12) << "$" << stockItem[4]/100.0 << endl;
    cout << "6: " << " Pepsi (20oz)" << setw(14) << "$" << stockItem[5]/100.0 << endl;
    cout << "7: " << " Puppy (Boston Terrier)" << setw(4) << "$" << stockItem[6]/100.0 << endl;
    cout << "8: " << " Sandwhich (ham)" << setw(11) << "$" << stockItem[7]/100.0 << endl;
    cout << "9: " << " Tickle Me Elmo" << setw(12) << "$" << stockItem[8]/100.0 << endl;
    cout << "10: " << "Umbrella (red & white)" << setw(4) << "$" <<  stockItem[9]/100.0 << endl;
}
void coins::requestMoney()        // Requests money from the user
{
    int input;
    double total=0;                 // Create double value 'total' and set to 0.

    cout << "\nPlease tell the machine how many of each bill or coin you've inserted:\n";
    while(1)                        // Using a while statement to continually ask for input until a valid integer is given.
    {
        cout << "\nDollars: ";
        cin >> input;               // Ask user to input the number of dollars deposited...
        if(cin.fail())              // If the input is invalid (non-integer)...
        {
            cout << "Please enter a valid number of bills.\n\n";
            cin.clear();            // clear the cache...
            cin.ignore(1000,'\n');  // and ignore the next 1000 characters, or the new line command...
            continue;               // and try again.
        }
        break;                      // Stops the while loop upon a successful 'insert' value.
    }
    dollar = input*100;             // Set the value of buffer.dollar to the inserted value times 100 (dollar value).

    while(1)
    {
        cout << "Quarters: ";
        cin >> input;               // Ask user to input the number of dollars deposited...
        if(cin.fail())              // If the input is invalid (non-integer)...
        {
            cout << "Please enter a valid number of coins.\n\n";
            cin.clear();            // clear the cache...
            cin.ignore(1000,'\n');  // and ignore the next 1000 characters, or the new line command...
            continue;               // and try again.
        }
        break;                      // Stops the while loop
    }
    quarter = input*25;             // Set the value of buffer.quarter to the inserted value times 25 (quarter value).

    while(1)
    {
        cout << "Dimes: ";
        cin >> input;               // Ask user to input the number of dollars deposited...
        if(cin.fail())              // If the input is invalid (non-integer)...
        {
            cout << "Please enter a valid number of coins.\n\n";
            cin.clear();            // clear the cache...
            cin.ignore(1000,'\n');  // and ignore the next 1000 characters, or the new line command...
            continue;               // and try again.
        }
        break;                      // Stops the while loop
    }
    dime = input*10;                // Sets the value of buffer.dime to the inserted value times 10 (dime value).

    while(1)
    {
        cout << "Nickels: ";
        cin >> input;               // Ask user to input the number of dollars deposited...
        if(cin.fail())              // If the input is invalid (non-integer)...
        {
            cout << "Please enter a valid number of coins.\n\n";
            cin.clear();            // clear the cache...
            cin.ignore(1000,'\n');  // and ignore the next 1000 characters, or the new line command...
            continue;               // and try again.
        }
        break;                      // Stops the while loop
    }
    nickel = input*5;               // Sets the value of buffer.nickel to the inserted value times 5 (nickel value).

    total = dollar + quarter + dime + nickel;                               // The total equals the sum of all buffer values

    cout << "You've deposited a total of $" << total/100 << endl;           // Display the total value of coins in currency format.
}

int stock::getItem(int item)
{
    return stockItem[item];
}
string stock::getName(int item)
{
    return stockName[item];
}
bool stock::checkStock(int item)
{
    if(stockItem[item]>0)                                                                 // If the item's stock is greater than 0...
        return true;                                                            // return a value of true...
    else
    {
        cout << "\nThis item is out of stock. Your money has been returned.\n"; // If there's not enough stock, tell the user.
        return false;                                                           // and return a value of false.
    }
}
int coins::getDollar()
{
    return dollar;
}
int coins::getQuarter()
{
    return quarter;
}
int coins::getDime()
{
    return dime;
}
int coins::getNickel()
{
    return nickel;
}
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
bool coins::checkChange(coins buffer, coins cash, coins& returned, int cost) // Checks to see if there's enough change for the transaction. Returns true or false.
{
    int change, tdollar, tquarter, tdime, tnickel, cdollar, cquarter, cdime, cnickel;   // Define integers necessary for the function.  't' and 'c' coin variables
                                                                                        // are used as temporary variables in place of a structure.
    tdollar = buffer.dollar;                                                            // Set tdollar to the value of buffer.dollar
    tquarter = buffer.quarter;                                                          // Set tquarter to the value of buffer.quarter
    tdime = buffer.dime;                                                                // Set tdime to the value of buffer.dime
    tnickel = buffer.nickel;                                                            // Set tnickel to the value of buffer.nickel

    cdollar = cash.dollar;                                                              // Set cdollar to the value of cash.dollar
    cquarter = cash.quarter;                                                            // Set cquarter to the value of cash.quarter
    cdime = cash.dime;                                                                  // Set cdime to the value of cash.dime
    cnickel = cash.nickel;                                                              // Set cnickel to the value of cash.nicke

    change = tdollar + tquarter + tdime + tnickel - cost;                               // Calculate change by subtracting the cost of the item from the sum of the temporary buffer.

    if(change==0)                                                                       // If change is not required...
        return true;                                                                    // return a true value.
    else if(change>0)                                                                   // Then, if change is in fact greater than 0 (acts to verify that cost > the total amount paid)
    {
        cdollar+= tdollar;                                                              // Set the temporary cash value of dollar to the old value plus what's in the buffer
        cquarter += tquarter;                                                           // Set the temporary cash value of quarter to the old value plus what's in the buffer
        cdime += tdime;                                                                 // Set the temporary cash value of dime to the old value plus what's in the buffer
        cnickel += tnickel;                                                             // Set the temporary cash value of nickel to the old value plus what's in the buffer

        while(returned.dollar < change/100 && (returned.dollar+1)*100 <= cdollar)       // While the returned dollar amount is less than the amount that can be returned for this amount of change, AND the number
        {                                                                               // of returned dollar bills is less than or equal to how many dollars are in the cash drawer...
            returned.dollar++;                                                          // essentially return 1 dollar for each time this is true.
        }
        change = change - returned.dollar*100;                                          // Once the number of dollars returned has been found, subtract that dollar amount from the total change left to give.

        while(returned.quarter < change/25 && (returned.quarter+1)*25 <= cquarter)      // While the returned quarter amount is less than the amount that can be returned for this amount of change, AND the number
        {                                                                               // of returned quarters is less than or equal to how many quarters are in the cash drawer...
            returned.quarter++;                                                         // essentially return 1 quarter for each time this is true.
        }
        change -= returned.quarter*25;                                                  // Once the number of quarters returned has been found, subtract that dollar amount from the total change left to give.

        while(returned.dime < change/10 && (returned.dime+1)*10 <= cdime)               // While the returned dime amount is less than the amount that can be returned for this amount of change, AND the number
        {                                                                               // of returned dimes is less than or equal to how many dimes are in the cash drawer...
            returned.dime++;                                                            // essentially return 1 dime for each time this is true.
        }
        change -= returned.dime*10;                                                     // Once the number of dimes returned has been found, subtract that dollar amount from the total change left to give.

        while(returned.nickel < change/5 && (returned.nickel+1)*5 <= cnickel)           // While the returned nickel amount is less than the amount that can be returned for this amount of change, AND the number
        {                                                                               // of returned nickels is less than or equal to how many nickels are in the cash drawer...
            returned.nickel++;                                                          // essentially return 1 nickel for each time this is true.
        }
        change -= returned.nickel*5;                                                    // Once the number of nickels returned has been found, subtract that dollar amount from the total change left to give.

        if(change==0)                                                                   // If at this point the amount of change left is 0, it means you had sufficient change in the cash drawer to make change...
        {
            return true;                                                                // so return true.
        }
        else                                                                            // Otherwise, you didn't have enough change...
        {
            cout << "\nThere is not enough change in the machine. Please use exact change.\n";
            return false;                                                               // so return false.
        }
    }
    else                                                                                // If change was not greater than 0 (a negative number essentially) then the user did not pay enough...
    {
        cout << "\nInsufficient funds. Please try again.\n";                            // so tell them so...
        return false;                                                                   // and return a false value.
    }

}
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
void coins::dispenseChange(coins buffer, coins& cash, coins returned, int cost)  // "Dispenses" the customer's change
{
    double change=0;                                                                // Define change and set to 0.

    change = buffer.dollar + buffer.quarter + buffer.dime + buffer.nickel - cost;   // Set change equal to the sum of coin buffer values, minus the cost of the item.

    cout << "\nYour change is: $" << change/100 << endl << endl;                    // Tell user how much change they will receive.

    cash.dollar += buffer.dollar;                                                   // Add the buffered dollars to the cash dollars.
    cash.quarter += buffer.quarter;                                                 // Add the buffered quarters to the cash quarters.
    cash.dime += buffer.dime;                                                       // Add the buffered dimes to the cash dimes.
    cash.nickel += buffer.nickel;                                                   // Add the buffered nickels to the cash nickels.

    cash.dollar -= returned.dollar*100;                                             // Since we've already calculated how many dollars to return, subtract that from the cash dollars
    cash.quarter -= returned.quarter*25;                                            // Since we've already calculated how many quarters to return, subtract that from the cash quarters
    cash.dime -= returned.dime*10;                                                  // Since we've already calculated how many dimes to return, subtract that from the cash dimes
    cash.nickel -= returned.nickel*5;                                               // Since we've already calculated how many nickels to return, subtract that from the cash nickels

    cout << "Dollars returned: " << setw(3) << returned.dollar << endl              // Displayed the returned dollars
         << "Quarters returned: " << setw(2) << returned.quarter << endl            // Displayed the returned quarters
         << "Dimes returned: " << setw(5) << returned.dime << endl                  // Displayed the returned dimes
         << "Nickels returned: " << setw(3) << returned.nickel << endl;             // Displayed the returned nickels
}
void stock::dispenseItem(int item) // "Dispenses" the item
{
    stockItem[item] -= 1;          // Removes 1 from the inventory item stock for whichever item 'item' currently points to.
    cout << "\nYour item has been dispensed. Stock left for this item: " << stockItem[item] << endl << endl;
}
Topic archived. No new replies allowed.