SHARE
TWEET

Untitled

a guest Feb 18th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "DrinkMachine.h" //necessary include to use the header file we made with all of its other details as well as link to main
  2.  
  3. //bool function which will create the array and its information stored by reading in the data
  4. //from the file and putting it into their respective places
  5. bool create(DrinkMachine &drinkMachine)
  6. {
  7.     ifstream file;
  8.     file.open("drink_machine.txt"); //opens the file and verify it opens properly
  9.  
  10.     if (file)
  11.     {
  12.         file >> drinkMachine.dCurrent;
  13.         for (unsigned int i = 0; i < drinkMachine.dCurrent; i++)
  14.         {
  15.             file >> drinkMachine.dItems[i].dName;
  16.             file >> drinkMachine.dItems[i].dPrice; //for each current item, it reads in the Name Price and Stock
  17.             file >> drinkMachine.dItems[i].dStock;
  18.             drinkMachine.dItems[i].drinkId = (i + 1);
  19.         }
  20.         file.close(); //Close file
  21.         return true;
  22.     }
  23.     else //Handle exception for file not opening
  24.     {
  25.         cout << "Unable to open file";
  26.         return false;
  27.     }
  28.     drinkMachine.vNum = 1; //set the drinkmachine version number to 1
  29. }
  30.  
  31. void destroy(DrinkMachine &drinkMachine) //Dump contnents of drink machine to file before exiting
  32. {
  33.     ofstream file;
  34.     file.open("drink_machine.txt"); //open file
  35.  
  36.     if (file)
  37.     {
  38.         //file << "Writing this to a file.\n";
  39.         file << drinkMachine.dCurrent << "\n";
  40.         for (unsigned int i = 0; i < drinkMachine.dCurrent; i++)
  41.         {
  42.             file << drinkMachine.dItems[i].dName << "\t";
  43.             file << drinkMachine.dItems[i].dPrice << " "; //output the items to the file
  44.             file << drinkMachine.dItems[i].dStock << "\n";
  45.         }
  46.         file.close();
  47.     }
  48.     else
  49.     {
  50.         cout << "no file \n";
  51.     }
  52.     file.close();
  53. }
  54.  
  55. DrinkItem & drink(DrinkMachine & drinkMachine, unsigned int drinkId)
  56. {
  57.     return drinkMachine.dItems[drinkId - 1];  //get item
  58. }
  59.  
  60. unsigned int size(DrinkMachine & drinkMachine)
  61. {
  62.     return drinkMachine.dCurrent; //get size
  63. }
  64.  
  65. bool available(DrinkMachine & drinkMachine, unsigned int drinkId)
  66. {
  67.     if (drinkMachine.dCurrent > drinkId)
  68.     {
  69.         return false;
  70.     }
  71.     else if (0 < drinkMachine.dItems[drinkId - 1].dStock)
  72.     {
  73.         return true;
  74.     }
  75.     return false;
  76. }
  77.  
  78. double price(DrinkMachine & drinkMachine, unsigned int drinkId)
  79. {
  80.     if (drinkMachine.dCurrent > drinkId)
  81.     {
  82.         return drinkMachine.dItems[drinkId - 1].dPrice;
  83.     }
  84.     else
  85.     {
  86.         return -1.0;
  87.     }
  88. }
  89.  
  90. string name(DrinkMachine & drinkMachine, unsigned int drinkId)
  91. {
  92.     if (drinkMachine.dCurrent > drinkId)
  93.     {
  94.         return drinkMachine.dItems[drinkId - 1].dName;
  95.     }
  96.     else
  97.     {
  98.         return "Invalid";
  99.     }
  100. }
  101.  
  102. unsigned int quantity(DrinkMachine & drinkMachine, unsigned int drinkId)
  103. {
  104.     if (drinkMachine.dCurrent > drinkId)
  105.     {
  106.         return drinkMachine.dItems[drinkId - 1].dStock;
  107.     }
  108.     else
  109.     {
  110.         return 0;
  111.     }
  112. }
  113.  
  114. unsigned int sold(DrinkMachine & drinkMachine, unsigned int drinkId)
  115. {
  116.     if (drinkMachine.dCurrent > drinkId)
  117.     {
  118.         return drinkMachine.dItems[drinkId - 1].dPurchased;
  119.     }
  120.     else
  121.     {
  122.         return 0;
  123.     }
  124. }
  125.  
  126. bool purchase(DrinkMachine & drinkMachine, int unsigned drinkId, double amount, double & change)
  127. {
  128.     if (drinkMachine.dCurrent > drinkId)
  129.     {
  130.         if (drinkMachine.dItems[drinkId - 1].dStock > 0 && amount >= drinkMachine.dItems[drinkId - 1].dPrice)
  131.         {
  132.             drinkMachine.dItems[drinkId - 1].dPurchased++; //increment purchased and decrement
  133.             drinkMachine.dItems[drinkId - 1].dStock--;     //stock if item is purchased
  134.         }
  135.         if (drinkMachine.dItems[drinkId - 1].dPrice == amount)
  136.         {
  137.             change = 0.0; //Check if no change due
  138.         }
  139.         else
  140.         {
  141.             change = (amount - drinkMachine.dItems[drinkId - 1].dPrice); //get change due
  142.         }
  143.         return true;
  144.     }
  145.     else
  146.     {
  147.         return false;
  148.     }
  149. }
  150.  
  151. void dumpDrinkMachine(const DrinkMachine & drinkMachine) //dump contents of drink machine to the screen
  152. {
  153.  
  154.     cout << "Drink Machine version " << drinkMachine.vNum << "\n" << "\n";
  155.     cout << "Drink Id        \t" << "  Drink     " << " Cost  " << " Quantity     " << " Sold\n";
  156.     for (unsigned int drinkid = 0; drinkid < drinkMachine.dCurrent; drinkid++)
  157.     {
  158.         cout << setw(8) << right << drinkMachine.dItems[drinkid].drinkId << "\t";
  159.         cout << setw(15) << right << drinkMachine.dItems[drinkid].dName;
  160.         cout << setw(10) << right << drinkMachine.dItems[drinkid].dPrice;
  161.         cout << setw(11) << right << drinkMachine.dItems[drinkid].dStock;
  162.         cout << setw(10) << right << drinkMachine.dItems[drinkid].dPurchased << endl;
  163.     }
  164. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top