DEPARTMENT OF MECHANICAL
ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
EC-237 Computer Systems & Programming
Course Instructor: Dr. Sagheer Khan
Lab Instructor: Engr. Eman Fatima
Group Members:
Ammarah Ishaq (504144)
Aleeha Amanat (507017)
Hamid Naveed (533267)
Malik Faraz Anjum (533062)
Lab project
Coding:
#include <iostream>
#include <iomanip>
using namespace std;
class Item {
public:
string name;
double price;
int quantity;
Item() : name(""), price(0.0), quantity(0) {}
Item(string n, double p, int q = 5) {
name = n;
price = p;
quantity = q;
}
void display(int index) {
cout << " " << setw(2) << index + 1 << ". " << setw(15) << left << name
<< " | $" << fixed << setprecision(2) << setw(6) << price
<< "| " << quantity << " in stock";
if (quantity <= 2) {
cout << " [Low Stock!]";
}
cout << endl;
}
};
class VendingMachine {
private:
static const int ITEM_COUNT = 12;
Item items[ITEM_COUNT];
public:
VendingMachine() {
items[0] = Item("Water", 9.00);
items[1] = Item("Juice", 4.50);
items[2] = Item("Chocolate", 2.00);
items[3] = Item("Waffer", 6.00);
items[4] = Item("Ice Cream", 3.50);
items[5] = Item("Parco", 1.00);
items[6] = Item("Prince", 8.00);
items[7] = Item("Passion Fruit", 6.50);
items[8] = Item("Oreo", 5.00);
items[9] = Item("Lollipop", 0.00);
items[10] = Item("Sting", 7.50);
items[11] = Item("Bunties", 6.00);
}
void showMenu() {
cout << "\n===== VENDING MACHINE MENU =====\n\n";
for (int i = 0; i < ITEM_COUNT; i++) {
items[i].display(i);
}
cout << "\n================================\n";
}
void buyItem() {
int choice;
cout << "Enter the number of the item you want: ";
cin >> choice;
if (cin.fail() || choice < 1 || choice > ITEM_COUNT) {
cin.clear();
cin.ignore((std::streamsize)10000, '\n');
cout << "Invalid choice. Please select a valid item." << endl;
return;
}
Item& selected = items[choice - 1];
if (selected.quantity == 0) {
cout << "Sorry, " << selected.name << " is out of stock." << endl;
return;
}
int requestedQuantity;
cout << "Enter quantity you want to buy: ";
cin >> requestedQuantity;
if (cin.fail() || requestedQuantity <= 0) {
cin.clear();
cin.ignore((std::streamsize)10000, '\n');
cout << "Invalid quantity entered." << endl;
return;
}
if (requestedQuantity > selected.quantity) {
cout << "Sorry, only " << selected.quantity << " " << selected.name << "(s) are
available." << endl;
return;
}
double totalCost = selected.price * requestedQuantity;
cout << "Total cost: $" << fixed << setprecision(2) << totalCost << endl;
double money;
cout << "Please insert money: $";
cin >> money;
if (cin.fail() || money < totalCost) {
cout << "Not enough money or invalid input. Please try again." << endl;
return;
}
double change = money - totalCost;
selected.quantity -= requestedQuantity;
cout << "\n✅ Transaction Successful!\n";
cout << "Dispensing " << requestedQuantity << " " << selected.name << "(s)..." << endl;
if (change > 0) {
cout << "Your change: $" << fixed << setprecision(2) << change << endl;
}
cout << "Enjoy your item(s)!" << endl;
// Purchase summary
cout << "\n===== PURCHASE SUMMARY =====\n";
cout << " Item : " << selected.name << endl;
cout << " Quantity : " << requestedQuantity << endl;
cout << " Total Paid : $" << money << endl;
cout << " Change : $" << change << endl;
cout << "============================\n";
}
void adminMenu() {
const string adminPassword = "admin123";
string input;
cout << "\nEnter admin password to access admin panel: ";
cin >> input;
if (input != adminPassword) {
cout << "Access denied. Incorrect password." << endl;
return;
}
int option;
do {
cout << "\n--- Admin Menu ---\n";
cout << "1. Restock Items\n";
cout << "2. Update Prices\n";
cout << "3. View Inventory\n";
cout << "0. Exit Admin Menu\n";
cout << "Choose an option: ";
cin >> option;
switch (option) {
case 1:
restockItems();
break;
case 2:
updatePrices();
break;
case 3:
showMenu();
break;
case 0:
cout << "Exiting Admin Menu...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (option != 0);
}
void restockItems() {
int index, qty;
cout << "Enter item number to restock (1-" << ITEM_COUNT << "): ";
cin >> index;
if (index < 1 || index > ITEM_COUNT) {
cout << "Invalid item number.\n";
return;
}
cout << "Enter quantity to add: ";
cin >> qty;
if (qty <= 0) {
cout << "Invalid quantity.\n";
return;
}
items[index - 1].quantity += qty;
cout << "Item restocked successfully!\n";
}
void updatePrices() {
int index;
double newPrice;
cout << "Enter item number to update price (1-" << ITEM_COUNT << "): ";
cin >> index;
if (index < 1 || index > ITEM_COUNT) {
cout << "Invalid item number.\n";
return;
}
cout << "Enter new price: $";
cin >> newPrice;
if (newPrice < 0) {
cout << "Invalid price.\n";
return;
}
items[index - 1].price = newPrice;
cout << "Price updated successfully!\n";
}
};
int main() {
VendingMachine machine;
char again;
cout << "===== Welcome to Snack Time Vending Machine! =====\n";
do {
cout << "\nAre you a:\n1. Customer\n2. Admin\nChoose (1/2): ";
int role;
cin >> role;
if (role == 2) {
machine.adminMenu();
}
else {
machine.showMenu();
machine.buyItem();
}
cout << "\nWould you like to continue? (y/n): ";
cin >> again;
cout << "\n------------------------------------\n";
} while (again == 'y' || again == 'Y');
cout << "Thank you for using the vending machine. See you next time!" << endl;
return 0;
}
Output: