Ielum button Sta Return Total Enter S lotal sodacos (issue change) all sold out
ID: 3807585 • Letter: I
Question
Ielum button Sta Return Total Enter S lotal sodacos (issue change) all sold out return button Wait For selection Check selectie) Inventory sold out of selection Write acode in C++that uses a finite state machine to stimulate a soft drink vending machine. You need to use the state diagram in picture and also you should use a switch statement. You should provide 2 drink choice and 2 drinks from each one(4 total) and keep track of inventory. Use an Enum for the state and the price of each drink is only 75 cent but you can use an integer in order to be easier(int 75). Also we should have 4 states here for this code, which are enter selection,inventory,return" Thank you in advance! Please dont use vector pairExplanation / Answer
#include <iostream>
using namespace std;
class VendingMachine {
public:
int coke;
int sprite;
VendingMachine(int m, int n){
this.coke = coke;
this.sprite = sprite;
}
void setCoke(int m){
coke = m;
}
void setSprite(int n){
sprite = n;
}
int getCoke(){
return coke;
}
int getSprite(){
return sprite;
}
};
int main() {
float total;
enum states{ Enter, Selection, Check, Return};
int state = Enter;
int c, i, j;
int d1,d2;
int price = 75;
VendingMachine vm = new VendingMachine(100,50);//Current Inventory Status
switch(state){
case Enter:
cout << "Your choices of drinks are: " << endl;
cout << "1 - Coke" << endl;
cout << "2 - Sprite" << endl;
cout << "What drink would you like to have: "<< endl;
cin >> c;
state = Selection;
break;
case Selection:
switch(c)
{
case '1' :
cout << "You chose Coke!" << endl;
cout << "How many cokes do you want?" << endl;
cin >> d1;
break;
case '2' :
cout << "You chose Sprite!" << endl;
cout << "How many cokes do you want?" << endl;
cin >> d2;
break;
default:
cout << "Invalid Choice, Please Try Again!";
break;
}
state = Check;
break;
case Check:
i = vm.getCoke();
j = vm.getSprite();
if((i >= d1) && (i >= d2)){
cout << "Order Successful!" << endl;
i=i-d1;
j=j-d2;
}
else if((i >= d1) && (j < d2)){
cout << "Only Coke available" << endl;
i=i-d1;
}
else if((i < d1) && (j >= d2)){
cout << "Only Sprite available" << endl;
j=j-d2;
}
else
cout << "Sorry, we're sold out" << endl;
state = Return;
break;
case Return:
total = (d1 + d2) * price;
cout << "Please pay: " << total << "Thank You!" << endl;
state = Enter;
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.