Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //David Masson
- //11-22-12
- //Vending Machine
- #include<iostream>
- #include<sstream>
- #include<iomanip>
- #include<fstream>
- #include<string>
- #include<cctype>
- #include<cstdlib>
- #include<algorithm>
- #include<unistd.h>
- using namespace std;
- class Stuff
- {
- private:
- string item;
- int quantity;
- float cost;
- float money;
- float usermoney;
- int bills;
- int coins;
- string password;
- ifstream inventory;
- public:
- Stuff(string = "Item", int = 0, float = 0.0);
- string stuff_in();
- void setItem(string, int, float);
- void changeItem(string, int, float);
- void dispense();
- void money_in();
- void display();
- bool pass();
- void replenish(int, int);
- float collectMoney();
- float collectUserMoney();
- float collectCost();
- void setMoney(float);
- void setUserMoney(float);
- int getCoins();
- void setCoins(int);
- void initMachine();
- int getItemQuantity();
- };
- Stuff::Stuff(string itempub, int quantitypub, float costpub)
- {
- item = itempub;
- quantity = quantitypub;
- cost = costpub;
- }
- string Stuff::stuff_in()
- {
- string inv;
- ifstream inventory("initial_inventory.txt");
- if (inventory.is_open())
- {
- if(inventory.good() )
- {
- getline(inventory,inv);
- }
- inventory.close();
- }
- else
- cout << "Unable to open file";
- return inv;
- }
- void Stuff::setItem(string it, int qu, float co)
- {
- item = it;
- quantity = qu;
- cost = co;
- return;
- }
- void Stuff::display()
- {
- cout << fixed << setprecision(2);
- cout << left << setw(14) << item;
- if(quantity <= 0)
- cout << right << setw(15) << "Sold Out";
- else
- cout << right << setw(15) << quantity;
- cout << right << setw(15) << cost << endl;
- return;
- }
- void Stuff::dispense()
- {
- quantity -= 1;
- cout << "Item dispensed" << endl;
- return;
- }
- void Stuff::money_in()
- {
- int temp;
- bool mongo = true;
- if(coins <= 3)
- {
- cout << "Use exact change." << endl;
- cout << "Enter money in (quarters): ";
- cin >> temp;
- money += 0.25 * float(temp);
- usermoney += 0.25 * float(temp);
- coins += temp;
- cin.ignore();
- cout << "Money entered: $" << usermoney << endl;
- }
- else
- {
- while(mongo)
- {
- cout << "Enter money in ($1 bills): ";
- cin >> temp;
- if(temp > 2)
- {
- cout << "Woah there big spender, enter $2 or less." << endl;
- temp = 0;
- }
- else
- mongo = false;
- }
- money += float(temp);
- usermoney += float(temp);
- bills += temp;
- cout << "Enter money in (quarters): ";
- cin >> temp;
- money += 0.25 * float(temp);
- usermoney += 0.25 * float(temp);
- coins += temp;
- cin.ignore();
- cout << "Money entered: $" << usermoney << endl;
- }
- return;
- }
- bool Stuff::pass()
- {
- bool yesno = false, running = true;
- while(running)
- {
- string tempstr;
- cout << "Enter password (blank is return to menu): ";
- getline(cin, tempstr);
- transform(tempstr.begin(), tempstr.end(), tempstr.begin(),::tolower);
- if(tempstr == password)
- {
- yesno = true;
- break;
- }
- else if(tempstr == "")
- {
- break;
- }
- else
- {
- continue;
- }
- }
- return yesno;
- }
- void Stuff::replenish(int i, int amount)
- {
- if(i == 0)
- coins += amount;
- else
- quantity += amount;
- }
- float Stuff::collectMoney()
- {
- return money;
- }
- float Stuff::collectUserMoney()
- {
- return usermoney;
- }
- void Stuff::setMoney(float tmoney)
- {
- money = tmoney;
- return;
- }
- void Stuff::setUserMoney(float tmoney)
- {
- usermoney = tmoney;
- return;
- }
- float Stuff::collectCost()
- {
- return cost;
- }
- int Stuff::getCoins()
- {
- return coins;
- }
- void Stuff::setCoins(int i)
- {
- coins = i;
- return;
- }
- void Stuff::initMachine()
- {
- money = 2.50;
- usermoney = 0.0;
- bills = 0;
- coins = 10;
- password = "2770";
- return;
- }
- int Stuff::getItemQuantity()
- {
- return quantity;
- }
- Stuff Machine[8];
- void showMachineContent();
- void initialize();
- void run();
- void admin();
- void indexStuff();
- string findString(string);
- string findInt(string);
- string findFloat(string);
- string removeAt(string);
- void refreshMoney();
- void vend(int);
- void endSession();
- void adminHelp();
- void userHelp();
- void menuRefresh();
- int main()
- {
- initialize();
- run();
- return 0;
- }
- void initialize()
- {
- system("clear");
- cout << "Loading..." << endl;
- sleep(1);
- cout << "64 KB RAM...";
- sleep(1);
- cout << " OK" << endl;
- cout << "4 MHz Z80 CPU...";
- sleep(1);
- cout << " OK" << endl;
- sleep(2);
- Machine[0].setItem("Machine", 1, 0.0);
- Machine[0].initMachine();
- indexStuff();
- menuRefresh();
- return;
- }
- void userHelp()
- {
- cout << "To insert money into the machine, enter 0." << endl;
- cout << "To select item, enter item number (1-7)." << endl;
- cout << "To redisplay machine contents and show this prompt again, enter 00." << endl;
- cout << "To end your session, enter 9.\n" << endl;
- return;
- }
- void menuRefresh()
- {
- system("clear");
- userHelp();
- cout << "$" << setprecision(2) << Machine[0].collectUserMoney() << " inserted\n" << endl;
- showMachineContent();
- return;
- }
- void run()
- {
- bool running = true;
- while(running) //main loop
- {
- string tempstr;
- cout << "Enter command: ";
- getline(cin, tempstr);
- transform(tempstr.begin(), tempstr.end(), tempstr.begin(),::tolower);
- if(tempstr == "00")
- {
- menuRefresh();
- }
- else if(tempstr == "0")
- {
- Machine[0].money_in();
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "1")
- {
- vend(1);
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "2")
- {
- vend(2);
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "3")
- {
- vend(3);
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "4")
- {
- vend(4);
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "5")
- {
- vend(5);
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "6")
- {
- vend(6);
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "7")
- {
- vend(7);
- sleep(1);
- menuRefresh();
- }
- else if(tempstr == "9")
- {
- endSession();
- }
- else if(tempstr == "1337")
- {
- system("clear");
- if(Machine[0].pass())
- {
- system("clear");
- admin();
- }
- }
- else if(tempstr == "")
- continue;
- else
- {
- system("clear");
- cout << "\tInvalid command" << endl;
- sleep(1);
- menuRefresh();
- continue;
- }
- }
- return;
- }
- void adminHelp()
- {
- cout << left << setw(15) << "Command";
- cout << left << setw(15) << "Description" << endl;
- cout << left << setw(15) << "00";
- cout << right << "Show this help menu." << endl;
- cout << left << setw(15) << "0";
- cout << right << "Go back to main menu." << endl;
- cout << left << setw(15) << "1";
- cout << right << "Reindexes inventory from file." << endl;
- cout << left << setw(15) << "2";
- cout << right << "Displays machine contents." << endl;
- cout << left << setw(15) << "3";
- cout << right << "Replenish coins." << endl;
- cout << left << setw(15) << "4";
- cout << right << "Replenish items." << endl;
- cout << left << setw(15) << "555";
- cout << right << "Shutdown vending machine." << endl;
- return;
- }
- void admin()
- {
- bool running = true;
- adminHelp();
- while(running) //admin loop
- {
- string tempstr;
- cout << "@Enter command: ";
- getline(cin, tempstr);
- transform(tempstr.begin(), tempstr.end(), tempstr.begin(),::tolower);
- if(tempstr == "00")
- {
- system("clear");
- adminHelp();
- }
- else if(tempstr == "1")
- {
- indexStuff();
- system("clear");
- }
- else if(tempstr == "2")
- {
- system("clear");
- cout << left << setw(15) << "Machine Money";
- cout << left << setw(15) << "User Money";
- cout << left << setw(15) << "Machine Quarters" << endl;
- cout << left << "$" << left << setw(14) << Machine[0].collectMoney();
- cout << left << "$" << left << setw(14) << Machine[0].collectUserMoney();
- cout << left << setprecision(0) << Machine[0].getCoins() << " quarters" << endl;
- }
- else if(tempstr == "3")
- {
- int tnum;
- cout << "Enter amount of coins to replenish: ";
- cin >> tnum;
- cin.ignore();
- Machine[0].replenish(0, tnum);
- }
- else if(tempstr == "4")
- {
- int tnum, tnum2 = 0;
- while(tnum2 < 1 || tnum2 > 7)
- {
- cout << "Enter item position to replenish: ";
- cin >> tnum2;
- }
- cout << "Enter amount of item to replenish: ";
- cin >> tnum;
- cin.ignore();
- Machine[tnum2].replenish(1, tnum);
- }
- else if(tempstr == "555")
- {
- system("clear");
- exit(0);
- }
- else if(tempstr == "420")
- cout << "Go home vending machine, you're high." << endl;
- else if(tempstr == "0")
- {
- system("clear");
- userHelp();
- showMachineContent();
- break;
- }
- else if(tempstr == "")
- continue;
- else
- {
- cout << "\tInvalid command" << endl;
- continue;
- }
- }
- return;
- }
- void showMachineContent()
- {
- cout << left << setw(15) << "Item number/item";
- cout << right << setw(15) << "Quantity";
- cout << right << setw(15) << "Price" << endl;
- for(int i = 1; i < 8; i++)
- {
- cout << i << " ";
- Machine[i].display();
- }
- return;
- }
- string findString(string str)
- {
- string temp, temp2;
- char nextChar = str.at(0);
- for(int i = 0; i < int(str.length()); i++)
- {
- nextChar = str.at(i);
- if(nextChar != ',')
- {
- temp = str.at(i);
- temp2 += temp;
- }
- else if (nextChar == '@')
- continue;
- else
- break;
- }
- return temp2;
- }
- string findInt(string str)
- {
- string temp, temp2, temp3;
- char nextChar;
- for(int i = 0; i < int(str.length()); i++)
- {
- nextChar = str.at(i);
- temp3 = nextChar;
- if(nextChar != ',' && isdigit(nextChar))
- {
- temp = str.at(i);
- temp2 += temp;
- }
- else if(nextChar == '@')
- continue;
- else
- break;
- }
- return temp2;
- }
- string findFloat(string str)
- {
- string temp, temp2, temp3;
- char nextChar;
- for(int i = 0; i < int(str.length()); i++)
- {
- nextChar = str.at(i);
- temp3 = nextChar;
- if(nextChar != ',' && nextChar != '@')
- {
- temp = str.at(i);
- temp2 += temp;
- }
- else if(nextChar == '@')
- continue;
- else
- break;
- }
- return temp2;
- }
- string removeNums(string str)
- {
- string temp, temp2;
- char nextChar;
- for(int i = 0; i < int(str.length()); i++)
- {
- nextChar = str.at(i);
- if(!isdigit(nextChar))
- {
- temp = str.at(i);
- temp2 += temp;
- }
- }
- return temp2;
- }
- string removeAt(string str)
- {
- string temp, temp2;
- char nextChar;
- for(int i = 0; i < int(str.length()); i++)
- {
- nextChar = str.at(i);
- if(nextChar != '@')
- {
- temp = str.at(i);
- temp2 += temp;
- }
- }
- return temp2;
- }
- void indexStuff()
- {
- string input, tempstr1;
- int j = 1;
- input = Machine[0].stuff_in();
- tempstr1 = input;
- for(int i = 1; i < 8; i++)
- {
- string tempstr2 = findString(tempstr1);
- tempstr1.replace(0, tempstr2.length() + j, "@");
- j = 2;
- string t2 = findInt(tempstr1);
- tempstr1.replace(0, t2.length() + j, "@");
- j = 2;
- string t3 = findFloat(tempstr1);
- tempstr1.replace(0, t3.length() + j, "@");
- j = 1;
- Machine[i].setItem(removeAt(tempstr2), atoi(t2.c_str()), atof(t3.c_str()));
- }
- input = "";
- return;
- }
- void refreshMoney()
- {
- float tempmoney = 0;
- for(int i = 0; i < 8; i++)
- {
- tempmoney += Machine[i].collectMoney();
- }
- Machine[0].setMoney(tempmoney);
- }
- void vend(int i)
- {
- if(Machine[i].getItemQuantity() <= 0)
- {
- cout << "Sold Out" << endl;
- return;
- }
- if(Machine[0].collectUserMoney() >= Machine[i].collectCost())
- {
- float tempcost = Machine[i].collectCost(), tempmoney = Machine[0].collectMoney(), tempusermoney = Machine[0].collectUserMoney();
- Machine[i].dispense();
- Machine[0].setMoney(tempmoney + tempcost);
- Machine[0].setUserMoney(tempusermoney - tempcost);
- }
- else
- {
- cout << "Not enough money, please insert more." << endl;
- }
- }
- void endSession()
- {
- int tempcoins = int(Machine[0].collectUserMoney() / 0.25);
- Machine[0].setCoins(Machine[0].getCoins() - tempcoins);
- Machine[0].setUserMoney(0.0);
- cout << tempcoins << " quarter(s) returned." << endl;
- sleep(3);
- menuRefresh();
- }
Advertisement
Add Comment
Please, Sign In to add comment