The vending machine can be used anywhere. It can be used in a hostel, hospital , airport , college , school etc.
This vending machine will particularly keep drinks and snacks.
The drinks can be divided into juices , energy drinks , soda (diet + regular), yogurt , chocolates and chips. This would be used by user.
The system will also manage an inventory which will keep track of how many items do the system have, if system runs out of a particular item the system automatically sends the message to refill to administrator.
The system will also take care of transactions, be it coins , cash or cards if something goes wrong the message is sent to an administrator to come fix the issue.
Any other maintenance issue like money being processed and item not received , item stuck , card stuck etc can be delay with sending an error message to administrator.
The first core object would be User.
User{
public:
double moneySpent;
Item selected_Item;
};
The second one would be Administrator
Administrator{
public:
int id;
string contactInfo
};
Third one would be Item
Item{
public:
string name;
string type;
double price;
int quantity;
};
Fourth one is Transaction
Transaction{
public:
Item t;
TransactionType type;
string status;
};
Fifth one would be Vending Machine
VendingMachine{
public:
Inventory inventory;
Administrator admin;
vector<Transaction> transactions;
bool processTransaction(User u, TransactionType type);
bool sendErrorMessage(string error);
bool sendRefillMessage(string itemName);
};
Sixth one would be Inventory
class Inventory{
public:
Item t;
int total;
};
Also we create an enum fro transaction
enum TransactionType { CASH, COIN, CARD };
Almost every object is related to each other.
#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
using namespace std;
class User {
public:
double moneySpent;
Item selectedItem; // Tracks the item the user selects
};
// Core Object 2: Administrator
class Administrator {
public:
int id;
string contactInfo; // Email or phone number for notifications
};
// Core Object 3: Item
class Item {
public:
string name;
string type; // e.g., "drink", "snack"
double price;
int quantity; // Number of items in stock
};
// Core Object 4: Transaction
enum TransactionType { CASH, COIN, CARD };
class Transaction {
public:
Item purchasedItem;
TransactionType type;
string status; // "success", "error", "pending"
};
// Core Object 6: Inventory
class Inventory {
public:
map<string, Item> items; // Maps item names to their details
// Method to check if an item needs to be refilled
bool checkStock(string itemName) {
return items[itemName].quantity == 0;
}
};
// Core Object 5: VendingMachine
class VendingMachine {
public:
Inventory inventory; // The vending machine should reference an inventory
Administrator admin; // Reference the admin to notify for issues
vector<Transaction> transactions; // Track all transactions
bool processTransaction(User u, TransactionType type) {
Item selectedItem = u.selectedItem;
double price = selectedItem.price;
double money_to_be_paid = price;
// Check stock before processing the transaction
if (inventory.checkStock(selectedItem.name)) {
sendRefillMessage(selectedItem.name); // Call to send refill message
sendErrorMessage("The item doesn't exist in the vending machine. Contacting admin.");
cout << "Error: there is no item left" << endl;
return false;
}
Transaction t;
if (type == CASH) {
cout << "Payment in progress" << endl;
t.status = "success";
}
if (type == COIN) {
cout << "Payment in progress" << endl;
t.status = "success";
}
if (type == CARD) {
// Handle card payment
if (u.moneySpent >= money_to_be_paid) {
cout << "Payment success!" << endl;
t.status = "success";
} else {
sendErrorMessage("Insufficient funds. Use another payment method.");
cout << "Insufficient funds!" << endl;
t.status = "fail";
}
}
t.purchasedItem = selectedItem;
t.type = type;
// If the transaction was successful, reduce the quantity in inventory
if (t.status == "success") {
inventory.items[selectedItem.name].quantity--;
}
transactions.push_back(t);
return t.status == "success";
}
// Notify the administrator about an error
bool sendErrorMessage(string error) {
cout << "Error: " << error << endl;
// Send error message to admin (implementation can be expanded)
return true;
}
// Notify admin to refill
bool sendRefillMessage(string itemName) {
// Check if the stock is empty
if (inventory.checkStock(itemName)) {
cout << "Sending refill message for " << itemName << " to admin." << endl;
// Implement the notification logic to the administrator (e.g., via email or SMS)
return true;
} else {
cout << "No refill needed for " << itemName << endl;
return false;
}
}
};
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Critically examine your design for any flaws or areas for future improvement...