Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Comments before submission: check the ordering of inputted variables to function, procedure calls
- //Look at the change_due integers
- #include <iostream>
- #include <cmath>
- #include <fstream>
- #include <string>
- using namespace std;
- /*
- Read initial inventory from: inventory.txt
- Read list of transactions from: transactions.txt
- Results should be outputted to:
- report_log.txt - reports transactions
- final_inventory.txt - contains what's left and current moeny
- */
- /*Transaction IDs
- 'I' - Insert cash in demonations of quarters, dimes, and nickels
- 'S' - Select an item to buy, calculate/dispense change (if needed), dispense the time
- 'L' - list the machine's current inventory
- 'O' - order items running low and replenish stock to the machine's inventory
- 'Q' - quit the program
- */
- /* Note:
- If something other than 25, 10, or 5 is inserted, display message and quit.
- Inventory file will be in this format
- <Quarters> <dimes> <nickels>
- <item> <quantity>
- <item> <quantity>
- ...
- <item> is expressed as an integer
- <quanitity> is the intial quantity of the corresponding item, expressed as an integer
- Initialize all items to quantity of 0.
- Text file may contain no items
- */
- ifstream transaction("transaction.txt");
- ifstream inventory("inventory.txt");
- ofstream report("report_log.txt");
- ofstream final("final_inventory.txt");
- const int pop = 1;
- const int iced_tea = 2;
- const int water = 3;
- const int cereal_bar = 4;
- const int chips = 5;
- const int gum = 6;
- /*Function to calculate cost of item
- *******************************************************************************************/
- int cost_of_item (int item_number){
- int item_cost = 0;
- if (item_number == 1 or item_number == 2 or item_number == 3)
- item_cost = 140;
- else if (item_number == 4)
- item_cost = 80;
- else if (item_number == 5)
- item_cost = 75;
- else if (item_number == 6)
- item_cost = 50;
- else
- exit(0);
- return item_cost;
- }
- /*******************************************************************************************
- /*Transaction 'I'
- **************************************************************************************************************/
- void transaction_I (int integer, int & nickels, int & dimes, int & quarters, int & nickels_c, int & dimes_c, int & quarters_c){
- if ( integer / 5 == 1){ //Adds to total amount of Qs,Ns, and Ds available, as well as add total to Qs, Ns, and Ds user inputted himself
- nickels++;
- nickels_c++;}
- else if (integer / 10 == 1){
- dimes++;
- dimes_c++;}
- else if (integer / 25 == 1){
- quarters++;
- dimes_c++;}
- else {
- cout << "Error: Machine only accepts quarters, dimes, and nickels!";
- exit(0);}
- return;
- }
- /*************************************************************************************************************
- /*Transaction 'S'
- *****************************************************************************************/
- void transaction_S (int integer, int & money_inserted, int & number_of_pop, int & number_of_iced_tea, int & number_of_water,
- int & number_of_cereal_bar, int & number_of_chips, int & number_of_gum, int & available_change,
- int & quarters, int & nickels, int & dimes, int & quarters_c, int & dimes_c, int & nickels_c){
- int cost;
- double money_inserted_dollars = money_inserted / 100.0; //converts into dollar amount
- cost = cost_of_item (integer); //Calculates cost of item
- //This serves as the example of functions each item must go through
- if (integer == 1){
- if (number_of_pop = 0){ //If # of item in stock is 0, consumer is requested to make another selection. This gets reported to report file
- report<<"Item out of stock!"<<endl<<"Please make another selection."<<endl;}
- if (number_of_pop > 0){ //If # of item in stock is greater than 0, then it goes through a long list of alogrithms...
- if (cost > money_inserted){ //If the cost is greater than the money the consumer put in, they are request to make another selection
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Pop"<<endl;
- report<<"Insufficient funds!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if (cost = money_inserted){ //If the cost of item equals the money the consumer put in, then exact change is made
- report<<"Money inserted: $"<<money_inserted_dollars<<endl; //No change is neccessary. One unit of the item is subtracted from stock
- report<<"Item selected for purchase: Pop"<<endl;
- report<<"Exact change"<<endl;
- money_inserted = 0;
- number_of_pop--;}
- else if (cost < money_inserted){ //If the cost of the item is less than the money inserted, then a list of algorithms are initiated...
- if ( (money_inserted - cost) > available_change){ //If the difference between money inserted by consumer and the item cost
- report<<"Money inserted: $"<<money_inserted_dollars<<endl; //is greater than the available amount of change. Then vending machine will
- report<<"Item selected for purchase: Pop"<<endl; //report that it is unable to make the correct amount of change and ask the
- report<<"Unable to return correct change!"<<endl; //consumer to make another selection.
- report<<"Please make another selection."<<endl;}
- else if ((money_inserted - cost) < available_change){ //If the difference between the money inserted by the consumer and the item cost
- //is less than the available change, yet another list of alogorithms are initiated...
- int change_due = money_inserted - cost; //The best combination of change that can be made from the available amount of change
- int change_in_quarters = change_due / 25; //in stock is calculated and determined here.
- while (change_in_quarters >= quarters) {
- change_in_quarters--;}
- change_due = change_due - (change_in_quarters * 25);
- int change_in_dimes = change_due / 10;
- while (change_in_dimes >= dimes){
- change_in_dimes--;}
- change_due = change_due - (change_in_dimes * 10) ;
- int change_in_nickels = change_due / 5;
- while (change_in_nickels >= nickels){
- change_in_nickels--;}
- change_due = change_due - (change_in_nickels * 25);
- if (change_due > 0){ //If the amount of change that is due is greater than 0, the vending machine will know that
- //it could not give exact change and ask the consumer to make another selection
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Pop"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else { //Otherwise, quarters, dimes, and nickels available in the machine is updated, and consumer
- quarters = quarters - change_in_quarters; //is told how much he or she got back in change
- dimes = dimes - change_in_dimes;
- nickels = nickels - change_in_nickels;
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for pruchase: Pop"<<endl;
- report<<"Change dispensed: "<<change_in_quarters<<" "
- <<change_in_dimes<<" "<<change_in_nickels<<" "<<endl;
- number_of_pop--;}}}}} //The amount of the item in stock is subtracted by one.
- if (integer == 2){
- if (number_of_iced_tea = 0){
- report<<"Item out of stock!"<<endl<<"Please make another selection."<<endl;}
- if (number_of_iced_tea > 0){
- if (cost > money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Iced Tea"<<endl;
- report<<"Insufficient funds!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if (cost = money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Iced Tea"<<endl;
- report<<"Exact change"<<endl;
- money_inserted = 0;
- number_of_iced_tea--;}
- else if (cost < money_inserted){
- if ( (money_inserted - cost) > available_change){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Iced Tea"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if ((money_inserted - cost) < available_change){
- int change_due = money_inserted - cost;
- int change_in_quarters = change_due / 25;
- while (change_in_quarters >= quarters) {
- change_in_quarters--;}
- change_due = change_due - (change_in_quarters * 25);
- int change_in_dimes = change_due / 10;
- while (change_in_dimes >= dimes){
- change_in_dimes--;}
- change_due = change_due - (change_in_dimes * 10) ;
- int change_in_nickels = change_due / 5;
- while (change_in_nickels >= nickels){
- change_in_nickels--;}
- change_due = change_due - (change_in_nickels * 25);
- if (change_due > 0){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Iced Tea"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else {
- quarters = quarters - change_in_quarters;
- dimes = dimes - change_in_dimes;
- nickels = nickels - change_in_nickels;
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for pruchase: Iced Tea"<<endl;
- report<<"Change dispensed: "<<change_in_quarters<<" "
- <<change_in_dimes<<" "<<change_in_nickels<<" "<<endl;
- number_of_iced_tea--;}}}}}
- if (integer == 3){
- if (number_of_water = 0){
- report<<"Item out of stock!"<<endl<<"Please make another selection."<<endl;}
- if (number_of_water > 0){
- if (cost > money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Water"<<endl;
- report<<"Insufficient funds!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if (cost = money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Water"<<endl;
- report<<"Exact change"<<endl;
- money_inserted = 0;
- number_of_water--;}
- else if (cost < money_inserted){
- if ( (money_inserted - cost) > available_change){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Water"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if ((money_inserted - cost) < available_change){
- int change_due = money_inserted - cost;
- int change_in_quarters = change_due / 25;
- while (change_in_quarters >= quarters) {
- change_in_quarters--;}
- change_due = change_due - (change_in_quarters * 25);
- int change_in_dimes = change_due / 10;
- while (change_in_dimes >= dimes){
- change_in_dimes--;}
- change_due = change_due - (change_in_dimes * 10) ;
- int change_in_nickels = change_due / 5;
- while (change_in_nickels >= nickels){
- change_in_nickels--;}
- change_due = change_due - (change_in_nickels * 25);
- if (change_due > 0){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Water"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else {
- quarters = quarters - change_in_quarters;
- dimes = dimes - change_in_dimes;
- nickels = nickels - change_in_nickels;
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for pruchase: Water"<<endl;
- report<<"Change dispensed: "<<change_in_quarters<<" "
- <<change_in_dimes<<" "<<change_in_nickels<<" "<<endl;
- number_of_water--;}}}}}
- if (integer == 4){
- if (number_of_cereal_bar = 0){
- report<<"Item out of stock!"<<endl<<"Please make another selection."<<endl;}
- if (number_of_cereal_bar > 0){
- if (cost > money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Cereal Bar"<<endl;
- report<<"Insufficient funds!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if (cost = money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Cereal Bar"<<endl;
- report<<"Exact change"<<endl;
- money_inserted = 0;
- number_of_cereal_bar--;}
- else if (cost < money_inserted){
- if ( (money_inserted - cost) > available_change){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Cereal Bar"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if ((money_inserted - cost) < available_change){
- int change_due = money_inserted - cost;
- int change_in_quarters = change_due / 25;
- while (change_in_quarters >= quarters) {
- change_in_quarters--;}
- change_due = change_due - (change_in_quarters * 25);
- int change_in_dimes = change_due / 10;
- while (change_in_dimes >= dimes){
- change_in_dimes--;}
- change_due = change_due - (change_in_dimes * 10) ;
- int change_in_nickels = change_due / 5;
- while (change_in_nickels >= nickels){
- change_in_nickels--;}
- change_due = change_due - (change_in_nickels * 25);
- if (change_due > 0){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Cereal Bar"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else {
- quarters = quarters - change_in_quarters;
- dimes = dimes - change_in_dimes;
- nickels = nickels - change_in_nickels;
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for pruchase: Cereal Bar"<<endl;
- report<<"Change dispensed: "<<change_in_quarters<<" "
- <<change_in_dimes<<" "<<change_in_nickels<<" "<<endl;
- number_of_cereal_bar--;}}}}}
- if (integer == 5){
- if (number_of_chips = 0){
- report<<"Item out of stock!"<<endl<<"Please make another selection."<<endl;}
- if (number_of_chips > 0){
- if (cost > money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Chips"<<endl;
- report<<"Insufficient funds!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if (cost = money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Chips"<<endl;
- report<<"Exact change"<<endl;
- money_inserted = 0;
- number_of_chips--;}
- else if (cost < money_inserted){
- if ( (money_inserted - cost) > available_change){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Chips"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if ((money_inserted - cost) < available_change){
- int change_due = money_inserted - cost;
- int change_in_quarters = change_due / 25;
- while (change_in_quarters >= quarters) {
- change_in_quarters--;}
- change_due = change_due - (change_in_quarters * 25);
- int change_in_dimes = change_due / 10;
- while (change_in_dimes >= dimes){
- change_in_dimes--;}
- change_due = change_due - (change_in_dimes * 10) ;
- int change_in_nickels = change_due / 5;
- while (change_in_nickels >= nickels){
- change_in_nickels--;}
- change_due = change_due - (change_in_nickels * 25);
- if (change_due > 0){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Chips"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else {
- quarters = quarters - change_in_quarters;
- dimes = dimes - change_in_dimes;
- nickels = nickels - change_in_nickels;
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for pruchase: Chips"<<endl;
- report<<"Change dispensed: "<<change_in_quarters<<" "
- <<change_in_dimes<<" "<<change_in_nickels<<" "<<endl;
- number_of_chips--;}}}}}
- if (integer == 6){
- if (number_of_gum = 0){
- report<<"Item out of stock!"<<endl<<"Please make another selection."<<endl;}
- if (number_of_gum > 0){
- if (cost > money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Gum"<<endl;
- report<<"Insufficient funds!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if (cost = money_inserted){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Gum"<<endl;
- report<<"Exact change"<<endl;
- money_inserted = 0;
- number_of_gum--;}
- else if (cost < money_inserted){
- if ( (money_inserted - cost) > available_change){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Gum"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else if ((money_inserted - cost) < available_change){
- int change_due = money_inserted - cost;
- int change_in_quarters = change_due / 25;
- while (change_in_quarters >= quarters) {
- change_in_quarters--;}
- change_due = change_due - (change_in_quarters * 25);
- int change_in_dimes = change_due / 10;
- while (change_in_dimes >= dimes){
- change_in_dimes--;}
- change_due = change_due - (change_in_dimes * 10) ;
- int change_in_nickels = change_due / 5;
- while (change_in_nickels >= nickels){
- change_in_nickels--;}
- change_due = change_due - (change_in_nickels * 25);
- if (change_due > 0){
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for purchase: Gum"<<endl;
- report<<"Unable to return correct change!"<<endl;
- report<<"Please make another selection."<<endl;}
- else {
- quarters = quarters - change_in_quarters;
- dimes = dimes - change_in_dimes;
- nickels = nickels - change_in_nickels;
- report<<"Money inserted: $"<<money_inserted_dollars<<endl;
- report<<"Item selected for pruchase: Gum"<<endl;
- report<<"Change dispensed: "<<change_in_quarters<<" "
- <<change_in_dimes<<" "<<change_in_nickels<<" "<<endl;
- number_of_gum--;}}}}}
- return;
- }
- /**************************************************************************************************
- /*Transaction 'L'
- ******************************************************************************************************************************/
- int transaction_L (int quarters, int dimes, int nickels, int number_of_pop, int number_of_iced_tea, int number_of_water,
- int number_of_cereal_bar, int number_of_chips, int number_of_gum){
- report<<quarters<<" "<<dimes<<" "<<nickels<<endl; //Reports the number of quarters, dimes, and nickels to report_log.txt in this format:
- //<quarter>_<dimes>_<nickels> where "_" is a space
- report<<pop<<" "<<number_of_pop<<endl; //Reports the item number and number of item that is currently stock
- report<<iced_tea<<" "<<number_of_iced_tea<<endl;
- report<<water<<" "<<number_of_water<<endl;
- report<<cereal_bar<<" "<<number_of_cereal_bar<<endl;
- report<<chips<<" "<<number_of_chips<<endl;
- report<<gum<<" "<<number_of_gum<<endl;
- return 0;
- }
- /*Transaction 'O'
- ******************************************************************************************************************************/
- void transaction_O (int request_stock_number, int & number_of_pop, int & number_of_iced_tea, int & number_of_water,
- int & number_of_cereal_bar, int & number_of_chips, int & number_of_gum){
- int amount1=0, amount2=0, amount3=0, amount4=0, amount5=0, amount6=0; //Initialize the amount of stock that needs to be ordered per item
- if (request_stock_number > number_of_pop){ //if requested stock is greater than what's in the inventory is, amount that needs to be request is calculated
- amount1 = request_stock_number - number_of_pop; //Then the amount of inventory will equal the requested stock, as it is being updated.
- number_of_pop = request_stock_number;}
- if (request_stock_number > number_of_iced_tea){
- amount2 = request_stock_number - number_of_iced_tea;
- number_of_iced_tea = request_stock_number;}
- if (request_stock_number > number_of_water){
- amount3 = request_stock_number - number_of_water;
- number_of_water = request_stock_number;}
- if (request_stock_number > number_of_cereal_bar){
- amount4 = request_stock_number - number_of_cereal_bar;
- number_of_cereal_bar = request_stock_number;}
- if (request_stock_number > number_of_chips){
- amount5 = request_stock_number - number_of_chips;
- number_of_chips = request_stock_number;}
- if (request_stock_number > number_of_gum){
- amount6 = request_stock_number - number_of_gum;
- number_of_gum = request_stock_number;}
- report<<amount1<<" Pop "<<amount2<<" Iced Tea "<<amount3<<" Water "<<amount4<<" Cereal Bar "<<amount5 //Report the amount of inventory that was requested to restock to certain
- <<" Chips "<<amount6<<" Gum "<<endl; //amount
- return;
- }
- /*********************************************************************************************************************************
- /*Main function
- ************************************************************************************************************************/
- int main (){
- int number_of_pop=0; //Declare containers for number of certain items
- int number_of_iced_tea=0;
- int number_of_water=0;
- int number_of_cereal_bar=0;
- int number_of_chips=0;
- int number_of_gum=0;
- int nickels = 0; //Declare containers of number of certain coins
- int dimes = 0;
- int quarters = 0;
- int nickels_c=0; //Declare containers for the number of inserted coins by consumer
- int dimes_c=0;
- int quarters_c=0;
- inventory>>nickels; //insert number of coins to containers from inventory.txt
- inventory>>dimes;
- inventory>>quarters;
- int item, number_of_item; //Delcared container file for what inventory.txt will be inputting information to
- while (inventory>>item>>number_of_item){ //While loop to input the items that are in stock and how much of it there is
- if (item == 1)
- number_of_pop = number_of_item;
- else if(item == 2)
- number_of_iced_tea = number_of_item;
- else if(item == 3)
- number_of_water = number_of_item;
- else if(item == 4)
- number_of_cereal_bar = number_of_item;
- else if(item == 5)
- number_of_chips = number_of_item;
- else if(item == 6)
- number_of_gum = number_of_item;
- else
- ;
- }
- int money_inserted = 0; //Delcare container for the money that has been inserted by consumer
- char type; //container for type of transaction
- int coin_inserted; //container for current coin that is inserted. Info will come from trasnaction I lines
- while (transaction>>type){ //start while loop for figuring transactions
- while (type == 'I'){ //Repeat transaction I until there are no more transaction I's in the transaction.txt
- transaction>>coin_inserted;
- transaction_I(coin_inserted, nickels, dimes, quarters, nickels_c, dimes_c, quarters_c);//Go through Transaction I
- transaction>>type>>coin_inserted;
- }
- money_inserted = (nickels_c * 5) + (dimes_c*10) + (quarters_c*25); //Calculates total money inserted by consumer
- int available_change = 0; //Declare container for change the vending machine has available
- available_change = (nickels * 5) + (dimes * 5) + (quarters * 25); //Calculates total available change
- int item_number; //Declare container for item number
- while (type == 'S'){
- transaction>>item_number;
- transaction_S(item_number, money_inserted, number_of_pop, number_of_iced_tea, number_of_water,
- number_of_cereal_bar, number_of_chips, number_of_gum, available_change, quarters,
- dimes,nickels, quarters_c, dimes_c, nickels_c); //Go through Transaction S
- }
- //////////////
- cout<<endl<<quarters_c<<" "<<dimes_c<<" "<<nickels_c<<endl;
- //////////////
- //Start of transaction I
- while (type == 'L'){
- transaction_L(quarters, dimes, nickels, number_of_pop, number_of_iced_tea, number_of_water,
- number_of_cereal_bar, number_of_chips, number_of_gum);
- }
- int request_stock_number; //Start of transaction O
- while (type == 'O'){
- transaction>>request_stock_number;
- transaction_O(request_stock_number, number_of_pop, number_of_iced_tea, number_of_water,
- number_of_cereal_bar, number_of_chips, number_of_gum);
- }
- while (type == 'Q'){
- report<<"End"; //"End" is entered in report log
- final<<quarters<<" "<<dimes<<" "<<nickels<<endl; //Final amount of quarters, dimes, nickels, and invetory is entered in final_stock.txt
- final<<pop<<" "<<number_of_pop<<endl;
- final<<iced_tea<<" "<<number_of_iced_tea<<endl;
- final<<water<<" "<<number_of_water<<endl;
- final<<cereal_bar<<" "<<number_of_cereal_bar<<endl;
- final<<chips<<" "<<number_of_chips<<endl;
- final<<gum<<" "<<number_of_gum<<endl;
- transaction>>type;
- }
- }
- transaction.close(); //Close textfiles.
- inventory.close();
- report.close();
- final.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement