Professional Documents
Culture Documents
Submitted by-
SAHIL(2K19/MC/112)
SAMEER KUMAR(2K19/MC/113)
Delhi Technological University
(Formerly Delhi College of Engineering)
Bawana road, Delhi-110042
Candidate Declaration
Date 18/04/2022
ACKNOWLEDGEMENT
Finite state machines (FSMs) are used in lots of different situations to model
complex entity state.
An entity may transition from one state to another, or it may remain in its current
state. The arrows denote transitions. The conditions under which a transition
should take place will need to be coded into the FSM itself.
Pure FSM form is composed of:
• A set of states
• A set of possible inputs (or events)
• A set of possible outputs (or actions)
• A transition function: – Given the current state and an input: defines the output
and the next state
States:
• Represent all possible “situations” that must be distinguished
• At any given time, the system is in exactly one of the states
• There is a finite number of these states
When the user puts in money, money counter tells the control unit, the amount of
money inserted in the Vending Machine. When the user presses the button to
purchase the item that he wants, the control unit turns on the motor and dispenses
the product if correct amount is inserted. If there is any change, machine will
return it to the user. The machine will demand for servicing when the products are
not available inside the machine.
Desc: Simple 4 state finite state machine for simulating a console application
vending machine that allows user to pay with points, update wallet, and keep track
of inventory.
#include <iostream>
#include <string>
enum VendingMachineState {
SELECT, QUANTITY, CALCULATE, UPDATE
};
enum Brand {
COKE = 100, PEPSI = 200, SEVENUP = 300
};
struct Student {
string name;
int points;
};
struct Soda {
Brand brandName;
int inventory;
int price;
};
int main() {
Soda selectedSoda;
Student eric;
eric.name = "Eric";
eric.points = 100000; //100,000 Points in Wallet
while (true) {
switch (currentState) {
case SELECT:
//1. Display the current status for the vending machine and the
student's wallet.
cout << "<<<<<---CURRENT STATUS--->>>>>" << endl;
cout << "[[[Vending Machine]]]" << endl;
for (int i = 0; i < 3; i++) {
cout << vendingMachine[i].brandName << " has " <<
vendingMachine[i].inventory << " drinks, and each costs " <<
vendingMachine[i].price << " points." << endl;
}
cout << "[[[Student]]]" << endl;
cout << eric.name << " has " << eric.points << " points total."
<< endl;
cout << "<<<<<---END STATUS--->>>>>" << endl << endl;
//2. Ask the user what drink they want.
cout << "Please select a drink from the following available
options: " << endl;
//3. Display the soda names and corresponding ID values.
cout << "COKE = 100\nPEPSI = 200\nSEVENUP = 300" <<
endl;
//4. User inputs the drink.
int selectedDrink;
cin >> selectedDrink;
//5. If valid drink selected, move to QUANTITY state; else, go
back to SELECT state.
switch (selectedDrink) {
case COKE:
cout << "You have selected COKE" << endl;
selectedSoda.brandName = COKE;
currentState = QUANTITY;
break;
case PEPSI:
cout << "You have selected PEPSI" << endl;
selectedSoda.brandName = PEPSI;
currentState = QUANTITY;
break;
case SEVENUP:
cout << "You have selected SEVENUP" << endl;
selectedSoda.brandName = SEVENUP;
currentState = QUANTITY;
break;
default:
cout << "Invalid Drink Selected!" << endl;
currentState = SELECT;
break;
}
break;
case QUANTITY:
cout << "Your order has been received. You placed an order "
<< orderQuantity << " soda(s)." << endl;
switch (selectedSoda.brandName) {
case COKE:
if (orderQuantity < 0 || orderQuantity >
vendingMachine[0].inventory) {
cout << "Not Enough " <<
selectedSoda.brandName << " In Stock" << endl;
currentState = SELECT;
cout << endl << endl;
}
else {
currentState = CALCULATE;
}
break;
case PEPSI:
if (orderQuantity < 0 || orderQuantity >
vendingMachine[1].inventory) {
cout << "Not Enough In Stock" << endl;
currentState = SELECT;
cout << endl << endl;
}
else {
currentState = CALCULATE;
}
break;
case SEVENUP:
if (orderQuantity < 0 || orderQuantity >
vendingMachine[2].inventory) {
cout << "Not Enough In Stock" << endl;
currentState = SELECT;
cout << endl << endl;
}
else {
currentState = CALCULATE;
}
break;
default:
cout << "Invalid Amount Received." << endl;
break;
}
break;
case CALCULATE:
cout << "The total cost for your soda is: ";
int cost;
switch (selectedSoda.brandName) {
case COKE:
cost = vendingMachine[0].price * orderQuantity;
cout << cost << endl;
break;
case PEPSI:
cost = vendingMachine[1].price * orderQuantity;
cout << cost << endl;
break;
case SEVENUP:
cost = vendingMachine[2].price * orderQuantity;
cout << cost << endl;
break;
default:
cout << "Error!" << endl;
break;
}
int payment;
cout << "Please type in your payment: ";
cin >> payment;
break;
case UPDATE:
switch (selectedSoda.brandName) {
case COKE:
vendingMachine[0].inventory =
vendingMachine[0].inventory - orderQuantity;
currentState = SELECT;
cout << endl << endl;
break;
case PEPSI:
vendingMachine[1].inventory =
vendingMachine[1].inventory - orderQuantity;
currentState = SELECT;
cout << endl << endl;
break;
case SEVENUP:
vendingMachine[2].inventory =
vendingMachine[2].inventory - orderQuantity;
currentState = SELECT;
cout << endl << endl;
break;
default:
cout << "Error" << endl;
currentState = SELECT;
cout << endl << endl;
break;
}
break;
default:
cout << "Error! Invalid State Detected." << endl;
break;
}
}
return 0;