linuxid10t

Vending Machine Final

Dec 13th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.91 KB | None | 0 0
  1. //David Masson
  2. //11-22-12
  3. //Vending Machine
  4.  
  5. #include<iostream>
  6. #include<sstream>
  7. #include<iomanip>
  8. #include<fstream>
  9. #include<string>
  10. #include<cctype>
  11. #include<cstdlib>
  12. #include<algorithm>
  13. #include<unistd.h>
  14.  
  15. using namespace std;
  16.  
  17. class Stuff
  18. {
  19.     private:
  20.         string item;
  21.         int quantity;
  22.         float cost;
  23.         float money;
  24.         float usermoney;
  25.         int bills;
  26.         int coins;
  27.         string password;
  28.         ifstream inventory;
  29.     public:
  30.         Stuff(string = "Item", int = 0, float = 0.0);
  31.         string stuff_in();
  32.         void setItem(string, int, float);
  33.         void changeItem(string, int, float);
  34.         void dispense();
  35.         void money_in();
  36.         void display();
  37.         bool pass();
  38.         void replenish(int, int);
  39.         float collectMoney();
  40.         float collectUserMoney();
  41.         float collectCost();
  42.         void setMoney(float);
  43.         void setUserMoney(float);
  44.         int getCoins();
  45.         void setCoins(int);
  46.         void initMachine();
  47.         int getItemQuantity();
  48. };
  49.  
  50. Stuff::Stuff(string itempub, int quantitypub, float costpub)
  51. {
  52.     item = itempub;
  53.     quantity = quantitypub;
  54.     cost = costpub;
  55. }
  56.  
  57. string Stuff::stuff_in()
  58. {
  59.     string inv;
  60.     ifstream inventory("initial_inventory.txt");
  61.     if (inventory.is_open())
  62.     {
  63.         if(inventory.good() )
  64.         {
  65.             getline(inventory,inv);
  66.         }
  67.         inventory.close();
  68.     }
  69.     else
  70.         cout << "Unable to open file";
  71.     return inv;
  72. }
  73.  
  74. void Stuff::setItem(string it, int qu, float co)
  75. {
  76.     item = it;
  77.     quantity = qu;
  78.     cost = co;
  79.     return;
  80. }
  81.  
  82. void Stuff::display()
  83. {
  84.     cout << fixed << setprecision(2);
  85.     cout << left << setw(14) << item;
  86.     if(quantity <= 0)
  87.         cout << right << setw(15) << "Sold Out";
  88.     else
  89.         cout << right << setw(15) << quantity;
  90.     cout << right << setw(15) << cost << endl;
  91.     return;
  92. }
  93.  
  94. void Stuff::dispense()
  95. {
  96.     quantity -= 1;
  97.     cout << "Item dispensed" << endl;
  98.     return;
  99. }
  100.  
  101. void Stuff::money_in()
  102. {
  103.     int temp;
  104.     bool mongo = true;
  105.    
  106.     if(coins <= 3)
  107.     {
  108.         cout << "Use exact change." << endl;
  109.         cout << "Enter money in (quarters): ";
  110.         cin >> temp;
  111.         money += 0.25 * float(temp);
  112.         usermoney += 0.25 * float(temp);
  113.         coins += temp;
  114.         cin.ignore();
  115.         cout << "Money entered: $" << usermoney << endl;
  116.     }
  117.     else
  118.     {
  119.         while(mongo)
  120.         {
  121.             cout << "Enter money in ($1 bills): ";
  122.             cin >> temp;
  123.             if(temp > 2)
  124.             {
  125.                 cout << "Woah there big spender, enter $2 or less." << endl;
  126.                 temp = 0;
  127.             }
  128.             else
  129.                 mongo = false;
  130.         }
  131.         money += float(temp);
  132.         usermoney += float(temp);
  133.         bills += temp;
  134.         cout << "Enter money in (quarters): ";
  135.         cin >> temp;
  136.         money += 0.25 * float(temp);
  137.         usermoney += 0.25 * float(temp);
  138.         coins += temp;
  139.         cin.ignore();
  140.         cout << "Money entered: $" << usermoney << endl;
  141.     }
  142.    
  143.     return;
  144. }
  145.  
  146. bool Stuff::pass()
  147. {
  148.     bool yesno = false, running = true;
  149.  
  150.     while(running)
  151.     {
  152.         string tempstr;
  153.         cout << "Enter password (blank is return to menu): ";
  154.         getline(cin, tempstr);
  155.         transform(tempstr.begin(), tempstr.end(), tempstr.begin(),::tolower);
  156.         if(tempstr == password)
  157.         {
  158.             yesno = true;
  159.             break;
  160.         }
  161.         else if(tempstr == "")
  162.         {
  163.             break;
  164.         }
  165.         else
  166.         {
  167.             continue;
  168.         }
  169.     }
  170.     return yesno;
  171. }
  172.  
  173. void Stuff::replenish(int i, int amount)
  174. {
  175.     if(i == 0)
  176.         coins += amount;
  177.     else
  178.         quantity += amount;
  179. }
  180.  
  181. float Stuff::collectMoney()
  182. {
  183.     return money;
  184. }
  185.  
  186. float Stuff::collectUserMoney()
  187. {
  188.     return usermoney;
  189. }
  190.  
  191. void Stuff::setMoney(float tmoney)
  192. {
  193.     money = tmoney;
  194.     return;
  195. }
  196.  
  197. void Stuff::setUserMoney(float tmoney)
  198. {
  199.     usermoney = tmoney;
  200.     return;
  201. }
  202.  
  203. float Stuff::collectCost()
  204. {
  205.     return cost;
  206. }
  207.  
  208. int Stuff::getCoins()
  209. {
  210.     return coins;
  211. }
  212.  
  213. void Stuff::setCoins(int i)
  214. {
  215.     coins = i;
  216.     return;
  217. }
  218.  
  219. void Stuff::initMachine()
  220. {
  221.     money = 2.50;
  222.     usermoney = 0.0;
  223.     bills = 0;
  224.     coins = 10;
  225.     password = "2770";
  226.     return;
  227. }
  228.  
  229. int Stuff::getItemQuantity()
  230. {
  231.     return quantity;
  232. }
  233.  
  234. Stuff Machine[8];
  235.  
  236. void showMachineContent();
  237. void initialize();
  238. void run();
  239. void admin();
  240. void indexStuff();
  241. string findString(string);
  242. string findInt(string);
  243. string findFloat(string);
  244. string removeAt(string);
  245. void refreshMoney();
  246. void vend(int);
  247. void endSession();
  248. void adminHelp();
  249. void userHelp();
  250. void menuRefresh();
  251.  
  252. int main()
  253. {
  254.     initialize();
  255.     run();
  256.    
  257.     return 0;
  258. }
  259.  
  260. void initialize()
  261. {
  262.     system("clear");
  263.     cout << "Loading..." << endl;
  264.     sleep(1);
  265.     cout << "64 KB RAM...";
  266.     sleep(1);
  267.     cout << " OK" << endl;
  268.     cout << "4 MHz Z80 CPU...";
  269.     sleep(1);
  270.     cout << " OK" << endl;
  271.     sleep(2);
  272.     Machine[0].setItem("Machine", 1, 0.0);
  273.     Machine[0].initMachine();
  274.     indexStuff();
  275.     menuRefresh();
  276.    
  277.     return;
  278. }
  279.  
  280. void userHelp()
  281. {
  282.     cout << "To insert money into the machine, enter 0." << endl;
  283.     cout << "To select item, enter item number (1-7)." << endl;
  284.     cout << "To redisplay machine contents and show this prompt again, enter 00." << endl;
  285.     cout << "To end your session, enter 9.\n" << endl;
  286.     return;
  287. }
  288.  
  289. void menuRefresh()
  290. {
  291.     system("clear");
  292.     userHelp();
  293.     cout << "$" << setprecision(2) << Machine[0].collectUserMoney() << " inserted\n" << endl;
  294.     showMachineContent();
  295.     return;
  296. }
  297.  
  298. void run()
  299. {
  300.     bool running = true;
  301.     while(running) //main loop
  302.     {
  303.         string tempstr;
  304.         cout << "Enter command: ";
  305.         getline(cin, tempstr);
  306.         transform(tempstr.begin(), tempstr.end(), tempstr.begin(),::tolower);
  307.        
  308.         if(tempstr == "00")
  309.         {
  310.             menuRefresh();
  311.         }
  312.         else if(tempstr == "0")
  313.         {
  314.             Machine[0].money_in();
  315.             sleep(1);
  316.             menuRefresh();
  317.         }
  318.         else if(tempstr == "1")
  319.         {
  320.             vend(1);
  321.             sleep(1);
  322.             menuRefresh();
  323.         }
  324.         else if(tempstr == "2")
  325.         {
  326.             vend(2);
  327.             sleep(1);
  328.             menuRefresh();
  329.         }
  330.         else if(tempstr == "3")
  331.         {
  332.             vend(3);
  333.             sleep(1);
  334.             menuRefresh();
  335.         }
  336.         else if(tempstr == "4")
  337.         {
  338.             vend(4);
  339.             sleep(1);
  340.             menuRefresh();
  341.         }
  342.         else if(tempstr == "5")
  343.         {
  344.             vend(5);
  345.             sleep(1);
  346.             menuRefresh();
  347.         }
  348.         else if(tempstr == "6")
  349.         {
  350.             vend(6);
  351.             sleep(1);
  352.             menuRefresh();
  353.         }
  354.         else if(tempstr == "7")
  355.         {
  356.             vend(7);
  357.             sleep(1);
  358.             menuRefresh();
  359.         }
  360.         else if(tempstr == "9")
  361.         {
  362.             endSession();
  363.         }
  364.         else if(tempstr == "1337")
  365.         {
  366.             system("clear");
  367.             if(Machine[0].pass())
  368.             {
  369.                 system("clear");
  370.                 admin();
  371.             }
  372.         }
  373.         else if(tempstr == "")
  374.             continue;
  375.         else
  376.         {
  377.             system("clear");
  378.             cout << "\tInvalid command" << endl;
  379.             sleep(1);
  380.             menuRefresh();
  381.             continue;
  382.         }
  383.     }
  384.     return;
  385. }
  386.  
  387. void adminHelp()
  388. {
  389.     cout << left << setw(15) << "Command";
  390.     cout << left << setw(15) << "Description" << endl;
  391.     cout << left << setw(15) << "00";
  392.     cout << right << "Show this help menu." << endl;
  393.     cout << left << setw(15) << "0";
  394.     cout << right << "Go back to main menu." << endl;
  395.     cout << left << setw(15) << "1";
  396.     cout << right << "Reindexes inventory from file." << endl;
  397.     cout << left << setw(15) << "2";
  398.     cout << right << "Displays machine contents." << endl;
  399.     cout << left << setw(15) << "3";
  400.     cout << right << "Replenish coins." << endl;
  401.     cout << left << setw(15) << "4";
  402.     cout << right << "Replenish items." << endl;
  403.     cout << left << setw(15) << "555";
  404.     cout << right << "Shutdown vending machine." << endl;
  405.     return;
  406. }
  407.  
  408. void admin()
  409. {
  410.     bool running = true;
  411.    
  412.     adminHelp();
  413.     while(running) //admin loop
  414.     {
  415.         string tempstr;
  416.         cout << "@Enter command: ";
  417.         getline(cin, tempstr);
  418.         transform(tempstr.begin(), tempstr.end(), tempstr.begin(),::tolower);
  419.        
  420.         if(tempstr == "00")
  421.         {
  422.             system("clear");
  423.             adminHelp();
  424.         }
  425.         else if(tempstr == "1")
  426.         {
  427.             indexStuff();
  428.             system("clear");
  429.         }
  430.         else if(tempstr == "2")
  431.         {
  432.             system("clear");
  433.             cout << left << setw(15) << "Machine Money";
  434.             cout << left << setw(15) << "User Money";
  435.             cout << left << setw(15) << "Machine Quarters" << endl;
  436.             cout << left << "$" << left << setw(14) << Machine[0].collectMoney();
  437.             cout << left << "$" << left << setw(14) << Machine[0].collectUserMoney();
  438.             cout << left << setprecision(0) << Machine[0].getCoins() << " quarters" << endl;
  439.         }
  440.         else if(tempstr == "3")
  441.         {
  442.             int tnum;
  443.             cout << "Enter amount of coins to replenish: ";
  444.             cin >> tnum;
  445.             cin.ignore();
  446.             Machine[0].replenish(0, tnum);
  447.         }
  448.         else if(tempstr == "4")
  449.         {
  450.             int tnum, tnum2 = 0;
  451.             while(tnum2 < 1 || tnum2 > 7)
  452.             {
  453.                 cout << "Enter item position to replenish: ";
  454.                 cin >> tnum2;
  455.             }
  456.             cout << "Enter amount of item to replenish: ";
  457.             cin >> tnum;
  458.             cin.ignore();
  459.             Machine[tnum2].replenish(1, tnum);
  460.         }
  461.         else if(tempstr == "555")
  462.         {
  463.             system("clear");
  464.             exit(0);
  465.         }
  466.         else if(tempstr == "420")
  467.             cout << "Go home vending machine, you're high." << endl;
  468.         else if(tempstr == "0")
  469.         {
  470.             system("clear");
  471.             userHelp();
  472.             showMachineContent();
  473.             break;
  474.         }
  475.         else if(tempstr == "")
  476.             continue;
  477.         else
  478.         {
  479.             cout << "\tInvalid command" << endl;
  480.             continue;
  481.         }
  482.     }
  483.     return;
  484. }
  485.  
  486. void showMachineContent()
  487. {
  488.     cout << left << setw(15) << "Item number/item";
  489.     cout << right << setw(15) << "Quantity";
  490.     cout << right << setw(15) << "Price" << endl;
  491.     for(int i = 1; i < 8; i++)
  492.     {
  493.         cout << i << " ";
  494.         Machine[i].display();
  495.     }
  496.     return;
  497. }
  498.  
  499. string findString(string str)
  500. {
  501.     string temp, temp2;
  502.     char nextChar = str.at(0);
  503.    
  504.     for(int i = 0; i < int(str.length()); i++)
  505.     {
  506.         nextChar = str.at(i);
  507.         if(nextChar != ',')
  508.         {
  509.             temp = str.at(i);
  510.             temp2 += temp;
  511.         }
  512.         else if (nextChar == '@')
  513.             continue;
  514.         else
  515.             break;
  516.     }
  517.     return temp2;
  518. }
  519.  
  520. string findInt(string str)
  521. {
  522.     string temp, temp2, temp3;
  523.     char nextChar;
  524.    
  525.     for(int i = 0; i < int(str.length()); i++)
  526.     {
  527.         nextChar = str.at(i);
  528.         temp3 = nextChar;
  529.         if(nextChar != ',' && isdigit(nextChar))
  530.         {
  531.             temp = str.at(i);
  532.             temp2 += temp;
  533.         }
  534.         else if(nextChar == '@')
  535.             continue;
  536.         else
  537.             break;
  538.     }
  539.     return temp2;
  540. }
  541.  
  542. string findFloat(string str)
  543. {
  544.     string temp, temp2, temp3;
  545.     char nextChar;
  546.    
  547.     for(int i = 0; i < int(str.length()); i++)
  548.     {
  549.         nextChar = str.at(i);
  550.         temp3 = nextChar;
  551.         if(nextChar != ',' && nextChar != '@')
  552.         {
  553.             temp = str.at(i);
  554.             temp2 += temp;
  555.         }
  556.         else if(nextChar == '@')
  557.             continue;
  558.         else
  559.             break;
  560.     }
  561.     return temp2;
  562. }
  563.  
  564. string removeNums(string str)
  565. {
  566.     string temp, temp2;
  567.     char nextChar;
  568.    
  569.     for(int i = 0; i < int(str.length()); i++)
  570.     {
  571.         nextChar = str.at(i);
  572.         if(!isdigit(nextChar))
  573.         {
  574.             temp = str.at(i);
  575.             temp2 += temp;
  576.         }
  577.     }
  578.     return temp2;
  579. }
  580.  
  581. string removeAt(string str)
  582. {
  583.     string temp, temp2;
  584.     char nextChar;
  585.    
  586.     for(int i = 0; i < int(str.length()); i++)
  587.     {
  588.         nextChar = str.at(i);
  589.         if(nextChar != '@')
  590.         {
  591.             temp = str.at(i);
  592.             temp2 += temp;
  593.         }
  594.     }
  595.     return temp2;
  596. }
  597.  
  598. void indexStuff()
  599. {
  600.     string input, tempstr1;
  601.     int j = 1;
  602.    
  603.     input = Machine[0].stuff_in();
  604.    
  605.     tempstr1 = input;
  606.    
  607.     for(int i = 1; i < 8; i++)
  608.     {
  609.         string tempstr2 = findString(tempstr1);
  610.         tempstr1.replace(0, tempstr2.length() + j, "@");
  611.         j = 2;
  612.        
  613.         string t2 = findInt(tempstr1);
  614.         tempstr1.replace(0, t2.length() + j, "@");
  615.         j = 2;
  616.        
  617.         string t3 = findFloat(tempstr1);
  618.         tempstr1.replace(0, t3.length() + j, "@");
  619.         j = 1;
  620.        
  621.         Machine[i].setItem(removeAt(tempstr2), atoi(t2.c_str()), atof(t3.c_str()));
  622.     }
  623.    
  624.     input = "";
  625.     return;
  626. }
  627.  
  628. void refreshMoney()
  629. {
  630.     float tempmoney = 0;
  631.     for(int i = 0; i < 8; i++)
  632.     {
  633.         tempmoney += Machine[i].collectMoney();
  634.     }
  635.     Machine[0].setMoney(tempmoney);
  636. }
  637.  
  638. void vend(int i)
  639. {
  640.     if(Machine[i].getItemQuantity() <= 0)
  641.     {
  642.         cout << "Sold Out" << endl;
  643.         return;
  644.     }
  645.     if(Machine[0].collectUserMoney() >= Machine[i].collectCost())
  646.     {
  647.         float tempcost = Machine[i].collectCost(), tempmoney = Machine[0].collectMoney(), tempusermoney = Machine[0].collectUserMoney();
  648.         Machine[i].dispense();
  649.         Machine[0].setMoney(tempmoney + tempcost);
  650.         Machine[0].setUserMoney(tempusermoney - tempcost);
  651.     }
  652.     else
  653.     {
  654.         cout << "Not enough money, please insert more." << endl;
  655.     }
  656. }
  657.  
  658. void endSession()
  659. {
  660.     int tempcoins = int(Machine[0].collectUserMoney() / 0.25);
  661.     Machine[0].setCoins(Machine[0].getCoins() - tempcoins);
  662.     Machine[0].setUserMoney(0.0);
  663.     cout << tempcoins << " quarter(s) returned." << endl;
  664.     sleep(3);
  665.     menuRefresh();
  666. }
Advertisement
Add Comment
Please, Sign In to add comment