Skip to content

Instantly share code, notes, and snippets.

@addamh
Created January 12, 2011 03:53
Show Gist options
  • Save addamh/775668 to your computer and use it in GitHub Desktop.
Save addamh/775668 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <string>
#include <ncurses.h>
#define WIDTH 30
#define HEIGHT 10
int startx = 0;
int starty = 0;
char *drinkChoices[] = {
"Coke",
"DrPepper",
"Sprite",
"MrPibb",
"Pepsi",
"Exit",
"Maintenence Mode"
};
char *serviceChoices[] = {
"Check drink inventory",
"Check cash bin",
"Quit Maintenance Menu"
};
char *moneyChoices[] = {
"Dollar Bills",
"Quarters",
"Dimes",
"Nickles",
"Finish"
};
int n_dChoices = sizeof(drinkChoices) / sizeof(char *);
int n_sChoices = sizeof(serviceChoices) / sizeof(char *);
int n_mChoices = sizeof(moneyChoices) / sizeof(char *);
using namespace std;
//////
// Vending Class
/////
class Vending
{
public:
static int cash;
//////
// Vending::Drink Class
/////
class Drink
{
public:
double drinkPrice;
char drinkName[10];
int drinkQuantity;
void setInfo(char *i_drinkName, double i_drinkPrice, int i_drinkQuantity)
{
strncpy(drinkName, i_drinkName, 10);
drinkPrice = i_drinkPrice;
drinkQuantity = i_drinkQuantity;
}
void Print() {
cout << "Name: " << drinkName << " Cost: " <<
drinkPrice << " Quantity Remaining: " << drinkQuantity << "\n";
}
};
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_dChoices; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", drinkChoices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", drinkChoices[i]);
++y;
}
wrefresh(menu_win);
};
void print_maintenance_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_sChoices; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", serviceChoices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", serviceChoices[i]);
++y;
}
wrefresh(menu_win);
};
void print_currency_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 1;
y = 1;
box(menu_win, 0, 0);
for(i = 0; i < n_mChoices; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", moneyChoices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", moneyChoices[i]);
++y;
}
wrefresh(menu_win);
};
//////
// End Vending::Drink Class
/////
//////
// Function to calulate change based off given cash
// Input: double tenderedChange, double DrinkCost
// Output: returnedChange in pennies
/////
int calculateChange (double tenderedChange, double drinkCost)
{
cout << "\n-------------------------------\n";
cout << "Making change --> \n";
printf("\nTendered: $%.2f", tenderedChange);
printf("\nCost: $%.2f\n\n", drinkCost);
int price_pennies = drinkCost * 100;
int cash_pennies = tenderedChange * 100;
int dollars = ((cash_pennies - price_pennies) / 100);
int remainder = (cash_pennies - price_pennies - (dollars * 100));
int quarters = (remainder / 25);
int dimes = (remainder - quarters * 25) / 10;
int nickels = (remainder - quarters * 25 - dimes * 10) / 5;
int pennies = (remainder - quarters * 25 - dimes * 10 - nickels * 5);
cout << "Change: dollars ("
<< dollars << "), quarters ("
<< quarters << "), dimes ("
<< dimes << "), nickels ("
<< nickels << "), pennies (" << pennies << ")" << endl;
cout << "\n-------------------------------\n";
return (cash_pennies - price_pennies);
}
bool mainScreen(Vending::Drink drinks[]){
for(int i=0; i<=4; i++){
if(drinks[i].drinkQuantity < 1){
cout << "\n\n<--- NEEDS SERVICE --->\n\n";
}
}
WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
double tenderedChange;
int returnedChange;
bool stop = false;
initscr();
clear();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (29 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(1, 22, "-------------------------------------");
mvprintw(2, 22, "| Welcome to Vending Machine v1.0 |");
mvprintw(3, 22, "-------------------------------------");
for(int i=0; i<=4; i++){
if(drinks[i].drinkQuantity < 1){
mvprintw(5, 28, "<--- NEEDS SERVICE --->");
}
}
mvprintw(7, 10, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_menu(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_dChoices;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_dChoices)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
default:
refresh();
break;
}
print_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, drinkChoices[choice - 1]);
clrtoeol();
refresh();
clear();
endwin();
if((choice!=6)&&(choice!=7)){
if((drinks[choice-1].drinkQuantity < 1)){
cout << "Please choose another drink. " << drinks[choice-1].drinkName << " is out.\n\n";
return false;
}
}
if(choice >= 1 && choice <= 5 ){
currencyMenu(drinks);
cout << "Insert money by typing the full dollar and cent amount tendered. Example (1.50 or 0.75)\n";
cout << "\nInsert Money: ";
cin >> tenderedChange;
cout << "You chose " << drinks[choice-1].drinkName << "\n";
printf("The cost of %s is $%.2f\n", drinks[choice-1].drinkName, drinks[choice-1].drinkPrice);
cout << " \n";
cout << "Vending " << drinks[choice-1].drinkName << "...\n";
cout << "\n-------------------------------\n";
cout << "Drink deployed to basket below";
cout << "\n-------------------------------\n";
drinks[choice-1].drinkQuantity--;
Vending::cash = Vending::cash + (tenderedChange * 100);
returnedChange = calculateChange(tenderedChange, drinks[choice-1].drinkPrice);
Vending::cash = Vending::cash - (returnedChange);
return false;
} else if (choice == 6) {
cout << "Canceling transaction and exiting.\n";
return true;
} else if (choice == 7) {
do {
stop = maintenanceModeMenu(drinks);
} while (stop!=true);
return false;
} else {
cout << "Not a valid selection";
}
}
bool currencyMenu(Vending::Drink drinks[])
{
WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
int tenderedChange;
int dollars;
int quarters;
int dimes;
int nickles;
bool stop = false;
initscr();
clear();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (29 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(1, 22, "-------------------------------------");
mvprintw(2, 22, "| Insert your change |");
mvprintw(3, 22, "-------------------------------------");
mvprintw(7, 10, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_currency_menu(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_dChoices;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_dChoices)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
default:
refresh();
break;
}
print_currency_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
clrtoeol();
refresh();
switch(choice)
{
case 1:
mvprintw(20,0,"How many dollar bills: ");
scanw("%d", &dollars);
mvprintw(19,0, "Entered dollars: %d", dollars);
mvprintw(25,0,"<-Press Any Key->"); /* tell the user to press a key */
getch();
clear();
return false;
break;
case 2:
mvprintw(18,0,"How many quarters: ");
break;
case 3:
mvprintw(18,0,"How many dimes: ");
break;
case 4:
mvprintw(18,0,"How many nickles: ");
break;
case 5:
return true;
break;
default:
break;
}
clear();
endwin();
}
//////
// End Vending Class Member Functions
/////
//////
// Vending Class Maintenance Function Block
/////
bool maintenanceModeMenu(Vending::Drink drinks[])
{
WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
initscr();
cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx);
keypad(menu_win, TRUE);
mvprintw(1, 22, "-------------------------------------");
mvprintw(2, 22, "| Vending Machive Service Menu |");
mvprintw(3, 22, "-------------------------------------");
mvprintw(6, 10, "Use arrow keys to go up and down, Press enter to select a choice");
refresh();
print_maintenance_menu(menu_win, highlight);
while(1)
{ c = wgetch(menu_win);
switch(c)
{ case KEY_UP:
if(highlight == 1)
highlight = n_sChoices;
else
--highlight;
break;
case KEY_DOWN:
if(highlight == n_sChoices)
highlight = 1;
else
++highlight;
break;
case 10:
choice = highlight;
break;
default:
mvprintw(24, 0, "Charcter pressed is = %3d Hopefully it can be printed as '%c'", c, c);
refresh();
break;
}
print_maintenance_menu(menu_win, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
clrtoeol();
refresh();
switch(choice)
{
case 1:
getDrinkInventory(drinks);
mvprintw(25,0,"<-Press Any Key->"); /* tell the user to press a key */
getch();
clear();
return false;
break;
case 2:
mvprintw(18,0,"There is $%.2f remaining in the machine", (Vending::cash / 100.0));
mvprintw(25,0,"<-Press Any Key->"); /* tell the user to press a key */
getch();
clear();
return false;
case 3:
clear();
endwin();
return true;
break;
default:
break;
}
}
void getDrinkInventory(Vending::Drink drinks[])
{
for(int i=0; i<=4; i++){
mvprintw(18+i,0,"Name: %s Cost: %.2f Quantity: %d",drinks[i].drinkName, drinks[i].drinkPrice, drinks[i].drinkQuantity);
}
}
//////
// End Vending Class Maintenance Function Block
/////
};
//////
// End Vending Class
/////
//Initialize static class member fortotal cash in the machine
int Vending::cash = 2000;
//////
// Main System Loop
/////
int main (int argc, char const *argv[])
{
Vending::Drink drinks[5];
drinks[0].setInfo("Coke", 1.00, 1);
drinks[1].setInfo("DrPepper", 1.25, 5);
drinks[2].setInfo("Sprite", 1.50, 5);
drinks[3].setInfo("MrPibb", 1.50, 5);
drinks[4].setInfo("Pepsi", 1.50, 5);
bool done = false;
char continueChoice;
do {
Vending vendingTransaction;
done = vendingTransaction.mainScreen(drinks);
if (done != false) {
return 0;
}
cout << "Would you like to make another purchase (y/n): ";
cin >> continueChoice;
if (continueChoice == 'n'){
done = true;
}
} while (!done);
return 0;
}
//////
// End Main System Loop
/////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment