Create a menu in your .cpp file to use your Coke Machine. 0. Walk away 1. Buy a
ID: 3753041 • Letter: C
Question
Create a menu in your .cpp file to use your Coke Machine. 0. Walk away 1. Buy a Coke 2. Restock Machine 3. Add change 4. Display Machine Info When you instantiate your CokeMachine object, your constructor will assign Your machine's name The price of a single Coke The amount of change in the machine The number of items (inventory level) in the machine The constructor I used (that you will see in the example output) is CokeMachine MyCokeMachine{"CSE 1325 Coke Machine", 50, 500, 100}; The max capacities of the machine (inventory and change) are set in the initalizers of the data members. These values are given in the UML. Any printing to the screen should take place in the .cpp file. Your CokeMachine class should not have any cins or couts in it. The UML is set up such that all functions return the information needed to print messages to the screen. Additional Information about the using of action in buyACoke() buyACoke() returns true/false to indicate whether or not the function worked. buyACoke() also sets action to different values depending on what happened while executing the code in buyACoke(). For example, if a Coke costs $0.50 and a payment of 30 cents is entered, then buyACoke() will fail but your .cpp program needs to know why so it can print a message to inform the user what went wrong – your .cpp program needs to know what action to take. buyACoke() would assign a specific value to action that would relay back to your .cpp program what happened so that it can print a message about it. For example, buyACoke() if payment is insufficient, then action would be set to 3 (for example) .cpp program get action back from buyACoke() and print a message about insufficient funds because action is 3 You will need to define multiple values for action to mean the various actions that your .cpp program needs to take based on what happened when buyACoke() was called. I used an enumerated type so that I could use words instead of numbers to make my code easier to read. enum ACTION {OK, NOINVENTORY, NOCHANGE, INSUFFICIENTFUNDS, EXACTCHANGE};
Explanation / Answer
//shows all possible action in coke machine
#include<iostream.h>
class MyCokeMachine
{
private:
char mname[30];
int stock,change,price,quatity,max;
enum ACTION {OK, NOINVENTORY, NOCHANGE, INSUFFICIENTFUNDS, EXACTCHANGE};
MyCokeMachine()
{
max=200; //setting maximum capacity
quantity=0;
}
myCokeMachine(char[] name,int p,int chge,int cp) // initialization of coke machine by setting initial values to data members.
{
strcpy(mname,name);
price=p;
change=chge;
quantity=cp;
}
void restock(int cp)
{
quantity+=cp; //adding stock to the exiting stock at reskocking time.
}
void addChange(int chge)
{
change+=chge; //adding change to the exiting change during add change time.
}
void dcr_change(int chge)
{
change-=chge; //subtracting the withdrawed change.
}
boolean buyacoke(int qt,int amt)
{
ACTION ac;
if(qt> quantity)
{
ac=NOINVENTORY; //checking available quantity
return false;
}
else
{
if(qt*price == amt)
{
quantity-=qt; //subtracting quantity saled from stock quantity
ac=OK; // on successfull transaction.
return true;
}
else if(qt*price > amt)
{
ac= INSUFFICIENTFUNDS; //on insufficient fund deposit
return false;
}
else if(qt*price < amt)
{
p=qt*price;
if(amt-p < change)
{
change-=p; //provide change;
if(p==change)
ac=EXACTCHANGE;
else
ac=OK;
return true;
}
else
{
ac=NOCHANGE; //change is not available
return false;
}
}
}
}
int check()
{
return max-quantity; //check quantity needed for restock
}
void display()
{
cout<<" Machine :"<<mname;
cout<<" coke unit price: "<<price;
cout<<" Quantity available: "<<quantity;
cout<<" Change available: "<<change;
}
};
int main()
{
char name[30];
int utpr,qt,stck,amt,chge,cpty;
boolean b;
do
{
MyCokeMacine obj;
cout<<" Enter machine name,unit price,change available,no of items: ";
cin>>name>>utpr>>chge>>qt;
obj.MyCokeMachine(name,utpr,chge,qt);
cout<<" 0:Walk away 1:Buy 2:Restock 3:Add change 4:Info display ";
cin>>ch;
switch(ch)
{
case 0:
break;
case 1:
cout<<" Enter the quatity needed and amount you inserted:"
cin>>qt>>amt;
b= obj.buyacoke(qt,amt);
if(b == TRUE)
cout<<" SUCCESS";
else
cout<<" TRANSACTION NOT POSSIBLE";
break;
case 2:
cpty=obj.check();
cout<<" Capacity needed is "<<cpty<<" "; //show capacity needed .assume only that amount of capaity will be added
obj.restock(cpty);
break;
case 3:
cout<<" Enter the change to be added: ";
cin<<chge;
obj.addChange(chge);
break;
case 4:
obj.display();
break;
default:
cout<<" Invalid choice";
break;
}
}while(1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.