Write a code in C++ that uses a finite state_ machine to stimulate a soft drink
ID: 3805674 • Letter: W
Question
Write a code 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 Enema 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!Explanation / Answer
#include <iostream>
using namespace std;
class vending {
pair<string, int> type1;
pair<string, int> type2;
int cost;
public:
vending(int c1, int c2) {
type1.first = "Pepsi";
type1.second = c1;
type2.first = "Coke";
type2.second = c2;
cost = 75;
}
int get_cost() {return cost;};
int get_count(int type) {
if (type == 1) return type1.second;
return type2.second;
}
void update_count(int type, int count) {
if (type == 1) type1.second -= count;
else type2.second -= count;
}
};
int main() {
enum states{ Enter, Select, Check, Return};
cout << "Welcome to soft drink vending machine ";
class vending vend(2,2); // initialize machine with no of Pepsi and Coke with this constructor
int state = Enter;
int value = 0, change = 0, temp, choice, ch1 = 0, ch2 = 0;
bool done = true;
while (done) {
switch (state) {
case Enter: {
cout << "Please Enter Amount Enter 0 to Exit ";
cin >> value;
if (value == 0) {
state = Return;
break;
}
while (value < vend.get_cost()) {
cout << "Please Enter more Amount Enter 0 to Exit ";
cin >> temp;
if (temp == 0) {
state = Return;
break;
}
value += temp;
}
state = Select;
break;
}
case Select: {
cout <<"Please select your Drinks. 0) Exit 1) 1 Pepsi 2) 1 Coke 3) 2 Pepsi 4) 2 Coke 5) 1 Pepsi 1 Coke ";
cin >> choice;
state = Check;
switch (choice) {
case 1: ch1 = 1; break;
case 2: ch2 = 1; break;
case 3: ch1 = 2; break;
case 4: ch2 = 2; break;
case 5: ch1 = 1; ch2 = 1; break;
default : "Wrong Choice";
state = Return;
}
}
case Check: {
cout << value <<"type1" <<vend.get_count(1) << "type2" <<vend.get_count(2)<< ch1 * vend.get_cost() + ch2 * vend.get_cost()<< endl;
if (value < (ch1 * vend.get_cost() + ch2 * vend.get_cost())) { // If Amount not sufficient Return
cout << "Insufficient balance ";
ch1 = 0;
ch2 = 0;
state = Return;
} else {
if (vend.get_count(1) == 0 && vend.get_count(2) == 0) {// All sold out case
cout << "All Sold Out ";
ch1 = 0;
ch2 = 0;
state = Return;
}
else if (ch1 > vend.get_count(1) || ch2 > vend.get_count(2)) {// Items unavailable case
cout << "Items Not available ";
ch1 = 0;
ch2 = 0;
state = Select;
} else {
vend.update_count(1, ch1);
vend.update_count(2, ch2);
change = value - (ch1 + ch2) * vend.get_cost();
value = value - change;
state = Return;
}
}
break;
}
case Return: {
if (ch1 || ch2) {
cout << "Your Order : ";
if (ch1) cout << ch1 << "Pepsi" <<endl;
if (ch2) cout << ch2 << "Coke" << endl;
cout << "Total Cost:" << value << endl;
ch1 = 0;
ch2 = 0;
cout << "Change Returned:" << change<<" ******************************** "<< endl;
} else {
cout << "Amount Returned "<< value << " ******************************** "<< 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.