Wednesday, May 19, 2010

Drink Machine Simulator

// Programming Environment: Bloodshed
// Program Description:
// This program uses struct to create structure of data so that we could use it
// for efficient programming experience. It creates an array of five structures
// with drink name, drink cost and number in machine. It lists the number of
// drinks, its costs and type of drink. The user is allowed to either pick a
// drink or leave the program by pressing any key except for the keys assigned
// for the respective drinks. The program takes amount from user for particular
// drink and then gives the output to the user about their change or informs user
// if the fund is insufficient. It only accepts amount >= zero and Less than
// equal to one. If the drinks are sold out then the user is informed to select
// other available drinks and can't choose sold out drinks. The program is designed
// to serve more than one transaction by one user untill they want to quit the
// program. Finally, the user is informed the updated result of all the transaction
// including the amount of money the machine earned from the user.        
//------------------------------------------------------------------------------        
// Date/Time: 4/29/2010  5:17:39 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
const int DRINK_SIZE = 27;
const int NUM_DRINKS = 5; // Valid for 5 drinks.
const char INPUT_PATH_FILE[] =
    "C:\\c++ 242\\Assignments\\assignment4\\drink_machine_input.txt";
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------
struct Drink_Structure {
       // Creating structure of Drink_Name, Cost and Quantity.
       char Drink_Name[DRINK_SIZE];
       double Cost;
       int Quantity;
       };
//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
void Simulator(Drink_Structure[], int);

bool Do_Again (void);
//---------------------------------------------------------------------------
int main (void) {

    cout.setf (ios::fixed, ios::floatfield);
    cout.setf (ios::showpoint);
    cout.precision (2);

    do {
    ifstream InFile; // Input file stream.
    InFile.open (INPUT_PATH_FILE); // Opening the file.
    if (InFile) { // Performing operations if the input file is open.
        // Input file opened successfully.              
        Drink_Structure machine[NUM_DRINKS];
        //Creating an array of Drink_Structure type with NUM_DRINKS number
        // of structures        
        int index = 0;      
        while (InFile >> machine[index].Drink_Name >> machine[index].Cost
                      >> machine[index].Quantity) {
    // Replace underscore characters in drink name with spaces.
               for (int i = 0; i < strlen (machine[index].Drink_Name); ++i) {
                   if (machine[index].Drink_Name[i] == '_') {
                       machine[index].Drink_Name[i] = ' ';
                   }    
               }
               if (++index == NUM_DRINKS) {
               // if increasing index value reaches NUM_DRINKS then it breaks
               // out of the loop.
               break;    
               }    
        }        
        InFile.close();      
        Simulator(machine, NUM_DRINKS);
        // Calling the function to simulate the tasks.    
    
        } else {
        cout << endl << "\a Error::Cannot Open Input File";
        // Displays error if the program can't open the input file.    
    }
  
    } while (Do_Again ());
    return 0;
}
//------------------------------------------------------------------------
// Function Definitions
//------------------------------------------------------------------------
bool Do_Again (void) {

    (void) fflush (stdin);
    cout << endl << endl << "\aDo Again? (y/n): ";
    return static_cast(tolower (_getch ())) == 'y';
}

void Simulator(Drink_Structure Machine[], int numDrinks) {
     // This function defination takes the input from the user on which drink they
     // like to drink and gives the change or asks for additional amount if insufficient.
     // If the user chooses to quit the program then it displays the updated
     // situation of the number of drinks in the machine and the amount of money
     // earned by the machine.
     char choice; // Choice as 1 to 5 drinks user want to have.
     double amountInserted = 0.0; // Amount of money user enters at first.
     double changeAmount = 0.0; // Amount of money user gets back or is in need.
     double totalSales = 0.0; // Total sales amount for the machine.
  
     do {
         // Displaying the Drink Machine information.
     cout << endl << endl << "Welcome to College Vending Machine Simulator!";
     cout << endl << "---------------------------------------------" << endl;
     cout << endl << "Options\t" << "Drink Name\t" << "Cost\t" << "Number in Machine";
     cout << endl << "-------\t" << "----------\t" << "----\t" << "-----------------";
     for (int j = 0; j < numDrinks; ++j) {
         cout << endl << setw (4) << char (j + 49) << "\t" << Machine[j].Drink_Name
              << "\t" << Machine[j].Cost << setw (13) << Machine[j].Quantity;
     }
  
     cout << endl << endl << "Enter Drink Option or PRESS ANY KEY to QUIT: ";
     cin >> choice;  
  
     if (choice >= '1' && choice <= '5') {
     // If the choice is in between 1 to 5 then the following code executes.
              
        while (Machine[choice - 49].Quantity == 0) { // If drink number is "0".
              cout << endl << "Oops.....Sold Out!! ";
              cout << endl << endl << "Enter other DRINK OPTIONS except, "
                   << Machine[choice - 49].Drink_Name << ": ";
              cin >> choice; // User is forced to choose drinks except the one
              // which is already sold out.          
        }
      
        cout << endl << "INSERT Amount of Money FOR "
             << Machine[choice - 49].Drink_Name << ": " ;
        cin >> amountInserted; // User's input.
        while (amountInserted < 0 || amountInserted > 1) {
              // Validating input.
              cout << endl << "INVALID AMOUNT!" << endl
                   << "INSERT Amount > $0 or <= $1: ";
              cin >> amountInserted;            
        }
            
        changeAmount = amountInserted - Machine[choice - 49].Cost;
        // Calculation change amount.      
        while (changeAmount < 0.0) {
              // If change amount is negative perform the following.
              cout << endl << "Insufficient Fund!!" << endl << "You are Short:  "
                   << "$" << fabs (changeAmount);
              cout << endl << "Please INSERT this short fund: ";
              cin >> amountInserted;
                  while (amountInserted < 0.0 || amountInserted > 1.0) {
                          // Again validating the input.
                    cout << endl << "Invalid Amount" << endl
                         << "INSERT Amount > $0 or <= $1: ";
                    cin >> amountInserted;
                  }
                  changeAmount = changeAmount + amountInserted;                              
        }
        cout << endl << "Your Change: " << "$" << changeAmount;
        // Displaying the change amount.
        totalSales += Machine[choice - 49].Cost;
        // Calculating the total sales.
        --Machine[choice - 49].Quantity;
        // Decreasing the number of drinks in machine.      
      }      
     }      
     while (choice >= '1' && choice <= '5');
      
        // After the user selects to QUIT the program, displaying the changes
        // in drink_structure called as machine.
        cout << endl << endl << "CLOSING TRANSACTIONS............";
        cout << endl << endl << "Remaining Drinks in College Vending Machine Simulator!";
        cout << endl << "------------------------------------------------------"
             << endl;
        cout << endl << "Drink Name\t" << "Cost\t" << "Number in Machine";
        cout << endl << "----------\t" << "----\t" << "-----------------";
        for (int j = 0; j < numDrinks; ++j) {
            cout << endl << Machine[j].Drink_Name
                 << "\t" << Machine[j].Cost << setw (13) << Machine[j].Quantity;
        }
        cout << endl << "\nTotal Amount of Sales: " << "$" << totalSales;
        // Displaying the total amount of sales done by soft drink machin.    
}

The input file "drink_machine_input.txt" looks like this,
Coca_Cola    0.75  0
Root_Beer    0.75  20
Lemon_Lime   0.75  20
Grape_Soda   0.80  2
Cream_Soda   0.80  20

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.