Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates a drink machine. *** NOTE: this is not the standa

ID: 644387 • Letter: W

Question

Write a program that simulates a drink machine. *** NOTE: this is not the standard drink machine coding question!

You are given the following structures.

struct DrinkItem

struct DrinkItem
{
   int drinkID;
   string drinkName;
   double drinkCost;
   int numberThisDrink;
   int numberDrinksPurchased;
};

struct DrinkMachine
{
   int versionNumber;
   int numberDrinkItemStruct;
   DrinkItem* drinkArray; // array of DrinkItems
   int currentLocation;
};

Here is the programming task:

Simulate the Drink Machine. Your program will get the number of drink types and drink information from a file, and there will be several drink types in the machine. First read the number of the drink types from the input file and create (dynamically) the array of structures. An example file is:

8

Cola 1.25 25

Root-beer 1.25 20

Lemon-lime 1.25 25

Water 1.00 40

Orange 1.25 5

Iced-tea 1.25 35

Grape 1.30 15

Iced-Coffee 2.00 35

You can read in using the cin operator, and the order is the drink name, the drink cost, and then the number of drinks in the machine at the beginning. You will create your Drink Machine code in a .cpp and put the function prototypes and structure definitions in a file called drinkmachine.h.

Functions:

create - dynamically create a DrinkMachine structure and return the address of this structure when you return from the create function. Set the version number in the DrinkMachine structure to 1. Create a global const int in the drinkmachine.cpp file and the name should be invalid_index and should be initialized to -1. Initialize the current location value of the DrinkMachine structure to invalid_index. Open the input file, "drink_machine.txt." If it doesn't exist, delte the structure and return a nullpt back to the caller.Read in the first value (8) and create the structure dynamically (could be set to any number). Create an array of DrinkItem structures. Dynamically allocate it using the number of DrinkItems read in above. A loop will execute once for every Drink item - inside the loop, read in the info for a DrinkItem, and put that info in the structure. the drink ID for the first 1 is 1, 2 for the 2nd, etc. This returns the address of DrinkMachine to the calling function.

destroy - input parameter is a pointer to DrinkMachine. Should delete the array of DrinkItem structures you created, then finally delete the DrinkMachine structure.

firstDrink and nextDrink will iterate through all drinks in the DrinkMachine. They are defined as:

firstDrink - address of DrinkMAchine structure is passed to firstDrink. The firstDrink function returns back the address of a DrinkItem structure (or the nullptr). The firstDrink function needs to return back the address of the first of the DrinkItem strucctures in the array. If there are no array entries, the current location in the DrinkMachine structure is set to invalid_index and returns nullptr. The current location should be initalized to 0. Current location is updated by nextDrink.

nextDrink - address of DrinkMachine is passed to nextDrink. The nextDrink function returns back the address of a DrinkItem structure (or nullptr). If current location is invalid_index, should return nullptr. Otherwise, increment current location.

items - parameter is the pointer to DrinkMachine structure. Returns an int, the number of DrinkItem structures in the Drink Machine.This is the size of the array.

available - two inputs (pointer to DrinkMachine structure and drinkId of a DrinkItem structure). Check to see if there is a drink available. Return a bool; true if available and false if unavailable.

cost - two inputs (pointer to DrinkMachine structure and drinkId of DrinkItem structure). Will return back a value of type double. Returns negative if drinkID is invalid.

purchase - four parameters (pointer to DrinkMachine structure, drinkID of DrinkItem structure, amount of money being used to buy drink (double), and the change returned to the customer (double, pass by reference)). Returns enum Purchase = { purchased, invalid, not_available, insufficient funds }.

dumpDM - takes pointer to DrinkMachine structure as only parameter, does not return value. Displays the contents of the drink machine.

Explanation / Answer

#include <iostream>
#include <string>
#include "Drink.h"

using namespace std;

int main()
{
   Drink vending;
   int choice,
       loopChoice;
   double moneyBack;

  
   const int DRINKS = 5; // Max number of drinks

   // New array using structure constructor to fill in data for all the drinks, costs, and inventory
   Drink data[DRINKS] = {Drink("Coke", .75, 20),
                       Drink("Root Beet", .75, 20),
                       Drink("Orange Soda", .75, 20),
                       Drink("Grape Soda", .75, 20),
                       Drink("Bottled Water", 1.00, 20)};
   cout << "To use the vending machine enter 1. "
   << "Otherwise, enter 0 to quit. ";
   cin >> loopChoice;

   while (loopChoice != 0)
   {
       choice = vending.displayChoices();
       moneyBack = vending.buyDrink(choice);

       switch (choice)
       {
       case 1:
      
           if (data[0].count > 0)
               data[0].count = vending.dailyReport(moneyBack, data[0].count);
               cout << data[0].name << endl << data[0].cost << endl
                   << data[0].count << endl;
           break;

       case 2:
      
           if (data[1].count > 0)
               data[1].count = vending.dailyReport(moneyBack, data[1].count);
               cout << data[1].name << endl << data[1].cost << endl
                   << data[1].count << endl;
           break;

       case 3:
      
           if (data[2].count > 0)
               data[2].count = vending.dailyReport(moneyBack, data[2].count);
               cout << data[2].name << endl << data[2].cost << endl
                   << data[2].count << endl;
           break;

       case 4:
      
           if (data[3].count > 0)
               data[3].count = vending.dailyReport(moneyBack, data[3].count);
               cout << data[3].name << endl << data[3].cost << endl
                   << data[3].count << endl;
           break;

       case 5:
      
           if (data[4].count > 0)
               data[4].count = vending.dailyReport(moneyBack, data[4].count);
               cout << data[4].name << endl << data[4].cost << endl
                   << data[4].count << endl;
           break;

       default:
           break;
       }

   cout << "If you would like to make another purchase"
       << " enter 1 to continue or 0 to quit. ";
   cin >> loopChoice;

   }

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote