Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
//dispenserType Header File

class dispenserType
{
public:
    int getNoOfItems() const; 
      //Function to show the number of items in the machine.
      //Postcondition: The value of numberOfItems is returned.

    int getCost() const; 
      //Function to show the cost of the item.
      //Postcondition: The value of cost is returned.

    void makeSale();  
      //Function to reduce the number of items by 1.
      //Postcondition: numberOfItems--;

    dispenserType(int setNoOfItems = 50, int setCost = 50); 
      //Constructor 
      //Sets the cost and number of items in the dispenser
      //to the values specified by the user.
      //Postcondition: numberOfItems = setNoOfItems;
      //               cost = setCost;
      //               If no value is specified for a 
      //               parameter, then its default value is 
      //               assigned to the corresponding member 
      //               variable.

private:
    int numberOfItems;     //variable to store the number of 
                           //items in the dispenser
    int cost;  //variable to store the cost of an item
};
//


//cashRegister Header File

class cashRegister
{
public:
    int getCurrentBalance() const;
       //Function to show the current amount in the cash 
       //register.
       //Postcondition: The value of cashOnHand is returned.

    void acceptAmount(int amountIn);
       //Function to receive the amount deposited by 
       //the customer and update the amount in the register.
       //Postcondition: cashOnHand = cashOnHand + amountIn;

    cashRegister(int cashIn = 500); 
       //Constructor
       //Sets the cash in the register to a specific amount.
       //Postcondition: cashOnHand = cashIn;
       //               If no value is specified when the 
       //               object is declared, the default value 
       //               assigned to cashOnHand is 500.

private:
     int cashOnHand;     //variable to store the cash 
                         //in the register
};


// Main Program
#include <iostream>
#include "cashRegister.h"
#include "dispenserType.h"

using namespace std;

void showSelection();
void sellProduct(dispenserType& product, 
                 cashRegister& pCounter);

int main()
{
    cashRegister counter;
    dispenserType candy(100, 50); 
    dispenserType chips(100, 65);
    dispenserType gum(75, 45);
    dispenserType cookies(100, 85);

    int choice;  //variable to hold the selection

    showSelection();
    cin >> choice;

    while (choice != 9)
    {
        switch (choice)
        {
        case 1: 
            sellProduct(candy, counter);
            break;
        case 2: 
            sellProduct(chips, counter);
            break;
        case 3: 
            sellProduct(gum, counter);
            break;
        case 4: 
            sellProduct(cookies, counter);
            break;
        default: 
            cout << "Invalid selection." << endl;
        }//end switch

        showSelection();
        cin >> choice;
    }//end while

    return 0;
}//end main

void showSelection()
{
    cout << "*** Welcome to Shelly's Candy Shop ***" << endl;
    cout << "To select an item, enter " << endl;
    cout << "1 for Candy" << endl;
    cout << "2 for Chips" << endl;
    cout << "3 for Gum" << endl;
    cout << "4 for Cookies" << endl;
    cout << "9 to exit" << endl;
}//end showSelection

void sellProduct(dispenserType& product, 
                 cashRegister& pCounter)
{
    int amount;  //variable to hold the amount entered
    int amount2; //variable to hold the extra amount needed

    if (product.getNoOfItems() > 0) //if the dispenser is not 
                                    //empty
    {
        cout << "Please deposit " << product.getCost()
             << " cents" << endl;
        cin >> amount;

        if (amount < product.getCost())
        {
            cout << "Please deposit another "
                 << product.getCost()- amount
                 << " cents" << endl;
            cin >> amount2;
            amount = amount + amount2;
       }

        if (amount >= product.getCost())
        {
            pCounter.acceptAmount(amount);
            product.makeSale();
            cout << "Collect your item at the bottom and "
                 << "enjoy." << endl;
        }
        else
            cout << "The amount is not enough. " 
                 << "Collect what you deposited." << endl;

        cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
             << endl << endl;
    }
    else
        cout << "Sorry, this item is sold out." << endl;
}//end sellProduct

//Implementation file dispenserType.cpp 
//This file contains the definitions of the functions to 
//implement the operations of the class dispenserType 

#include <iostream>
#include "dispenserType.h"

using namespace std;

int dispenserType::getNoOfItems() const
{
    return numberOfItems;
}

int dispenserType::getCost() const
{
    return cost;
}

void dispenserType::makeSale()
{
    numberOfItems--;
}

dispenserType::dispenserType(int setNoOfItems, int setCost)
{
    if (setNoOfItems >= 0)
         numberOfItems = setNoOfItems;
    else    
        numberOfItems = 50;

    if (setCost >= 0)
        cost = setCost;
    else
        cost = 50;
}


//Implementation file cashRegisterImp.cpp 
//This file contains the definitions of the functions to 
//implement the operations of the class cashRegister

#include <iostream>
#include "cashRegister.h"

using namespace std;

int cashRegister::getCurrentBalance() const
{
    return cashOnHand;
}

void cashRegister::acceptAmount(int amountIn)
{
    cashOnHand = cashOnHand + amountIn;
}

cashRegister::cashRegister(int cashIn)
{
    if (cashIn >= 0)
        cashOnHand = cashIn;
    else
        cashOnHand = 500;
}
Posted
Updated 13-Jun-11 14:41pm
v3
Comments
JOAT-MON 13-Jun-11 20:40pm    
Edit: wrapped in code block

Use a loop:

while ( /* the amount isn't enough */)  {

      /* collect more money and add it to the amount */

 }

 /* now make change if it is too much */


I leave the actual code to you to figure out.
 
Share this answer
 
Comments
babyniblet 13-Jun-11 22:10pm    
can someone help me with the code? I am stuck and can't think anymore. The program so far makes sure that there are two chances to deposit and it does NOT give out change if someone paid more than the cost. can someone give me the line of code that finishes the rest? I appreciate your help and thanks TRK3
Albert Holguin 13-Jun-11 23:34pm    
don't you have a debugger? you should be able to see why your program is not working as expected by setting a breakpoint and stepping through the code
TRK3 14-Jun-11 13:15pm    
I don't think anybody is going to bother giving you the code -- what you are asking for is simple enough that you ought to able to work it out yourself with the hints we are willing to give you. psycho00range almost gave you the entire answer and you are still having trouble with it.

Maybe you should explain the point of the exercise. Is this a homework assignment?
<pre>/*
GivenAmount before this loop is $5
AmountNeed before this loop is $10
*/
while(GivenAmount&lt;AmountNeeded) //after first pass because you have(12) more than amount needed(10) it will skip this loop again and break out going to give you change
{
cout &lt;&lt; "Still need $" &lt;&lt; AmountNeeded-GivenAmount &lt;&lt; ":" &lt;&lt; endl; //should out put 'Still need $5:'
cout &lt;&lt; "How much would you like to give?";
cin &gt;&gt; Give; //lets give $7 more dollars here
cout &lt;&lt; endl;
GivenAmount += Give; //GivenAmount = (before second ask)5 + (after ask)7 = 12
}
//change will always be given so no if statement should be used here
Change = GivenAmount - AmountNeeded //Change = 12 - 10
cout &lt;&lt; "Your Change is $" &lt;&lt; Change &lt;&lt; endl; // Your Change is $2</pre>



All you have to do now is implement this into your coding....i give you a base now you change everything around so you make it work for yourself
 
Share this answer
 
v4
Comments
babyniblet 14-Jun-11 0:20am    
thank you so much but it still tells me that GivenAmount and AmountNeeded are undeclared and I am stuck thanks for the help guys.
psych00range 14-Jun-11 0:25am    
what i wrote is not meant to just plug into what you have...you have to make it your own and able to work with your classes. GivenAmount and AmountNeeded are just names i came up with so you could read it easier

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900