Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DrinkMachine.h" //necessary include to use the header file we made with all of its other details as well as link to main
- //bool function which will create the array and its information stored by reading in the data
- //from the file and putting it into their respective places
- bool create(DrinkMachine &drinkMachine)
- {
- ifstream file;
- file.open("drink_machine.txt"); //opens the file and verify it opens properly
- if (file)
- {
- file >> drinkMachine.dCurrent;
- for (unsigned int i = 0; i < drinkMachine.dCurrent; i++)
- {
- file >> drinkMachine.dItems[i].dName;
- file >> drinkMachine.dItems[i].dPrice; //for each current item, it reads in the Name Price and Stock
- file >> drinkMachine.dItems[i].dStock;
- drinkMachine.dItems[i].drinkId = (i + 1);
- }
- file.close(); //Close file
- return true;
- }
- else //Handle exception for file not opening
- {
- cout << "Unable to open file";
- return false;
- }
- drinkMachine.vNum = 1; //set the drinkmachine version number to 1
- }
- void destroy(DrinkMachine &drinkMachine) //Dump contnents of drink machine to file before exiting
- {
- ofstream file;
- file.open("drink_machine.txt"); //open file
- if (file)
- {
- //file << "Writing this to a file.\n";
- file << drinkMachine.dCurrent << "\n";
- for (unsigned int i = 0; i < drinkMachine.dCurrent; i++)
- {
- file << drinkMachine.dItems[i].dName << "\t";
- file << drinkMachine.dItems[i].dPrice << " "; //output the items to the file
- file << drinkMachine.dItems[i].dStock << "\n";
- }
- file.close();
- }
- else
- {
- cout << "no file \n";
- }
- file.close();
- }
- DrinkItem & drink(DrinkMachine & drinkMachine, unsigned int drinkId)
- {
- return drinkMachine.dItems[drinkId - 1]; //get item
- }
- unsigned int size(DrinkMachine & drinkMachine)
- {
- return drinkMachine.dCurrent; //get size
- }
- bool available(DrinkMachine & drinkMachine, unsigned int drinkId)
- {
- if (drinkMachine.dCurrent > drinkId)
- {
- return false;
- }
- else if (0 < drinkMachine.dItems[drinkId - 1].dStock)
- {
- return true;
- }
- return false;
- }
- double price(DrinkMachine & drinkMachine, unsigned int drinkId)
- {
- if (drinkMachine.dCurrent > drinkId)
- {
- return drinkMachine.dItems[drinkId - 1].dPrice;
- }
- else
- {
- return -1.0;
- }
- }
- string name(DrinkMachine & drinkMachine, unsigned int drinkId)
- {
- if (drinkMachine.dCurrent > drinkId)
- {
- return drinkMachine.dItems[drinkId - 1].dName;
- }
- else
- {
- return "Invalid";
- }
- }
- unsigned int quantity(DrinkMachine & drinkMachine, unsigned int drinkId)
- {
- if (drinkMachine.dCurrent > drinkId)
- {
- return drinkMachine.dItems[drinkId - 1].dStock;
- }
- else
- {
- return 0;
- }
- }
- unsigned int sold(DrinkMachine & drinkMachine, unsigned int drinkId)
- {
- if (drinkMachine.dCurrent > drinkId)
- {
- return drinkMachine.dItems[drinkId - 1].dPurchased;
- }
- else
- {
- return 0;
- }
- }
- bool purchase(DrinkMachine & drinkMachine, int unsigned drinkId, double amount, double & change)
- {
- if (drinkMachine.dCurrent > drinkId)
- {
- if (drinkMachine.dItems[drinkId - 1].dStock > 0 && amount >= drinkMachine.dItems[drinkId - 1].dPrice)
- {
- drinkMachine.dItems[drinkId - 1].dPurchased++; //increment purchased and decrement
- drinkMachine.dItems[drinkId - 1].dStock--; //stock if item is purchased
- }
- if (drinkMachine.dItems[drinkId - 1].dPrice == amount)
- {
- change = 0.0; //Check if no change due
- }
- else
- {
- change = (amount - drinkMachine.dItems[drinkId - 1].dPrice); //get change due
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- void dumpDrinkMachine(const DrinkMachine & drinkMachine) //dump contents of drink machine to the screen
- {
- cout << "Drink Machine version " << drinkMachine.vNum << "\n" << "\n";
- cout << "Drink Id \t" << " Drink " << " Cost " << " Quantity " << " Sold\n";
- for (unsigned int drinkid = 0; drinkid < drinkMachine.dCurrent; drinkid++)
- {
- cout << setw(8) << right << drinkMachine.dItems[drinkid].drinkId << "\t";
- cout << setw(15) << right << drinkMachine.dItems[drinkid].dName;
- cout << setw(10) << right << drinkMachine.dItems[drinkid].dPrice;
- cout << setw(11) << right << drinkMachine.dItems[drinkid].dStock;
- cout << setw(10) << right << drinkMachine.dItems[drinkid].dPurchased << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement