Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please use c++ a vending machine that dispenses at least 4 different drinks. For

ID: 3760195 • Letter: P

Question

please use c++

a vending machine that dispenses at least 4 different drinks. For each drink, it keeps track of inventory (how much left) as well as price of that item. to dispensing the drink, there must be options to refill when item runs out and change prices. Create a class definition with aforementioned properties. Next, use this class to define an object in in the main() function. Present a menu using while(1) and switch statements to let a user pick a drink of choice, and perform a financial transaction to purchase that drink.

Explanation / Answer

Hi,

Below is the solution to your question:

class cashRegister
{
   int getBalance() const;//Function to get the balance
   void acceptAmount(int);//Function to accept the amount
   cashRegister(int cashIn=500);//Constructor
  
private:
int cash;
}
------------------------------------------------------------------------------------------

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

using namespace std;
//Implementation of cashRegister class function,returns balance amount
int cashRegister::getBalance() const
{
return cash;
}
//Function that sets the cash in the register
void cashRegister::acceptAmount(int in)
{
cash=cash+in;
}
//Contructor where amount must be greater than or equal to 0,otherwise default amount=500
cashRegister::cashRegister(int cashIn)
{
if (cashIn>=0)
cash=cashIn;
else
cash=500;
}
------------------------------------------------------------------------------------------
class dispenserType
{
public:
int getNoOfItems() const;//Function to get number of items
int getCost() const;//function to get cost of the item
void makeSale();//Function to makesale of an item
dispenserType(int setItems=50,int setCost=50);//Constructor set with some default values
private:
int numItems;
int cost;
}

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

using namespace std;


//Function to get number of items in the dispensory
int dispenserType::getNoOfItems() const
{
return numItems;
}

//Function to get the cost
int dispenserType::getCost() const
{
return cost;
}
//Function to decrement the items as they get sold out
void dispenserType::makeSale()
{
numItems--;
}
//Constructor to make sure both numbers are greater than 0
dispenserType::dispenserType(int setItems,int setCost)
{
   if(setItems >=0)
   numItems=setItems;
   else
   numItems=50;
   if(setCost>=0)
   cost=setCost;
   else
   cost=50;
}

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

using namespace std;

void menu();
//Function that takes reference parameter of dispenserType and cashRegister
void sell(dispenserType &,cashRegister &);

int main()
{
   cashRegister counter;
   dispenserType pulpyorange(100,50);
   dispenserType cocacola(100,75);
   dispenserType pepsi(50,50);
   dispenserType sprite(100,80);
   int choice;

   menu();
   cin>> choice;
   while(choice!=9)
   {
   switch(choice);
   {
  
   case 1:
   sell(pulpyorange,counter);
       break;
   case 2:
   sell(cococola,counter);
       break;
   case 3:
   sell(pepsi,counter);
       break;
   case 4:
   sell(sprite,counter);
       break;     
   default:
   cout<<"Invalid Selection!!!!!"<<endl;
      
   menu();
   cin>>choice;
   }
return 0;
}

void menu()
{
cout<<"Welcome to vending machine.......<<endl";
cout<<"Enter your choice<<endl";
cout<<"Enter 1 for pulpyorange<<endl";
cout<<"Enter 2 for cococola<<endl";
cout<<"Enter 3 for Pepsi<<endl";
cout<<"Enter 4 for sprite<<endl";
cout<<"Enter 9 to exit<<endl";
}

void sell(dispenserType& product,cashRegister& count)
{
int amount1,amount2;
   if(product.getNoOfItems()>0)
   {
   cout<<"Please deposit"<<product.getCost()<<"cents."<<endl;
   cin>>amount1;
   if(amount1 <product.getCost())
   {
   cout<<"Please deposit another"<<product.getCost()=amount1<<"cents."<<endl;
   cin>>amount2;
   amount1=amount1+amount2;
     
   }
   if(amount1 >=product.getCost())
   count.acceptAmount(amount1);
   product.makeSale();
   cout<<"Please collect your item,HAVE A NICE DAY!!!!<<endl";
   if(amount 1>product.getcost())
       cout<<"change amount ="<<amount1 - product.getCost()<<"Cents."<<endl;
      
   }
   else
       cout<<"Not enough amount,please take back your money<<endl";
       cout<<"***************************************************<<endl<<endl";
      
   }
   else{
       cout<<"Sorry item is currently sold out<<endl";
   }
      
  
}
      
       -------------------------------------------------------------------------------

Hope that helps...HAPPY ANSWERING!!!!!!