Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WRITE THE FUNCTIONS (DESCRIBED BELOW) FOR A DRINK MACHINE PROGRAM IN C++ USING THE STRUCTURES IN THE GIVEN HEADER FILE: Header File: #ifndef DRINKMACHINE_H_ #define

WRITE THE FUNCTIONS (DESCRIBED BELOW) FOR A DRINK MACHINE PROGRAM IN C++ USING THE STRUCTURES IN THE GIVEN HEADER FILE:

Header File:

#ifndef DRINKMACHINE_H_

#define DRINKMACHINE_H_

#include

#include

#include

using namespace std;

struct drinkItem

{

unsigned int drinkID; //The drink id (assigned by the program)

string drinkName; //Drink name (type of drink read in from a file)

double drinkCost; //Drink cost (the retail cost of one drink). This is the price the customer will pay for one drink of this type. This is also read in from a file

unsigned int numberOfDrinks; //Number of drinks, of this type, in the machine. Initial value is read in from a file. This is also updated by the program

unsigned int drinksPurchased; //Drinks purchased. Initially 0. Updated whenever a drink is purchased

};

const unsigned int MAX_DRINKS = 20; //the maximum number of allowed drink items in the drink machine.

struct DrinkMachine

{

unsigned int version; //Version number. This will have a value of 1 in the create function

unsigned int size; //The number of currently used DrinkItem structures in the drink machine. This will be read in from the input file

drinkItem drinkItems[MAX_DRINKS]; //This is an array of DrinkItem values. The size of the array is the global const unsigned int you defined before the DrinkMachine structure. See the paragraph after DrinkMachine structure above. This const variable has a value of 20

};

----------------------------------------

DESCRIPTION OF FILE THAT WILL BE READ INTO PROGRAM:

Your program will read in the drink items from the input file. For the drink machine there will be 0 or more drink types in the machine.

You must keep track of the number of drink items read in. There can be any number of drink items, but you will only read in the first 20.

Here is a sample file that contains the information for one such Drink Machine that contains 8 drinks.

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 

Note that the drink names do not include any spaces in the text. The drink name is first, the cost of the drink is second and the number of drinks in the machine at start up is third. Each value to be read in from the file is a token and you can use the stream operator >> to read in the values from the file.

-----------------------------------------------------------

FUNCTIONS TO WRITE:

Function: create

bool create(DrinkMachine &drinkMachine, const string &inputFileName);

The first function you need to write for your Drink Machine is the create function. This function takes two parameters.

The first parameter is a reference to a DrinkMachine structure.

Set the DrinkMachine version number to 1. Also set the number of drinks in use to 0.

The 2nd parameter passed to the create function is the input file name. Open up your input file. If the file does not exist you need to return falsefrom the create function.

If the file opened properly read in the drink machine values until you reach end of file, or until you have read in 20 drinks. Each drink name, price and quantity needs to be stored in the next DrinkItem in the array. The id of the first entry is 1, the id of the 2nd entry is 2, and so on. Make sure you set the DrinkMachine number of drinks in use field to the actual number of DrinkMachine array entries you read in (max of 20). Note that you will have to set the drink items purchased field to 0 since this is not read in from the input file.

Make sure your create function closes the input file once it has finished reading in the Drink Item information.

You function returns a bool value of true if the input file exists and was valid. If there is an error the create function must return false.

The create function must not display any error messages to the user and must not call the exit function. It must return value of true or false. It is up to the calling function to display any error messages.

Here is a summary of the cases that will return a value of false:

  • The input file could not be opened
  • The input file did not contain any drink item values

Note that an input file with more than 20 values is NOT an error. Your program must only read in the first 20 and return true.

Function: destroy

void destroy(DrinkMachine &drinkMachine, const string &outputFileName);

The destroy function has two parameters.

The first parameter is a reference to a DrinkMachine structure.

The second parameter is the output file name.

This function needs to write the contents of the drink machine out to the file passed in as the second parameter.

For each drink in the drink machine you need to write out the drink name, drink price, and the current drink quantity to the output file. Make sure you separate the fields with one or more spaces. Output a std::endl after the information for each drink in the machine.

Your destroy function should set the structure size to 0.

The size and drink functions:

DrinkItem& drink(DrinkMachine &drinkMarchine, unsigned int drinkId);

unsigned int size(DrinkMachine &drinkMachine);

The size function take a reference to a DrinkMachine and returns the size field of the drink machine (this is the actual number of drinks in the drink machine).

The drink function takes two parameters. The first is a reference to the DrinkMachine and the second is a drink id. It should return a reference to the appropriate entry in the array (the array entry with the id specified in the 2nd parameter). You can assume the drink id is always valid.

To make the mapping easy you can assume the drinkId is a number from 1 to n where n is the number of drink items in the drink machine. The index values for the array are 0 to (n-1), where n is the number of drink items in the array. You can convert from drinkId to array index by using (drinkId - 1) as the index into the array. You should not actually use the drink id stored in the DrinkItem to lookup the drink. The id stored in the DrinkItem will be used to see how things have changed once the items are sorted at the end of the program.

Note: This is a simplification of the program. The mapping should really be more robust, but for now you need to do it this way.

Note that we are creating functions to access values in the DrinkMachine structure. We are trying to provide an interface to the drink machine that does not require that our application directly access the internals of the structures. This, in theory, could allow us to change how we implement the drink machine and only impact the functions we are providing as part of the interface. Also note that the size and drink functions now give us a way to access individual drinks and could be used to get access to all of the drink items.

You can use the following logic (pseudo code):

DrinkMachine drinkMachine; Bool worked = create(drinkMachine, "drink_machine.txt") If (worked) { For (unsigned int id = 1; id <= size(drinkMachine); id++) { DrinkItem &item = drink(drinkMachine, id) cout << "The address of drink item id is " << &item } } Else { Display The drink machine could not be created } destroy(drinkMachine, "updated_drink_machine.txt") 

Make sure in your tests that all of the entries are being displayed properly.

Function: available

bool available(DrinkItem &drinkItem);

The available function takes one parameter, a const reference to the DrinkItem structure for the specified drink. The function checks to see if there if that drink is available (the quantity is 1 or more). The function returns back a bool. The value will be true if the drink is available and false if it is not available.

Function: price

double price(const DrinkItem &drinkItem);

The price function takesone parameter. The parameter is a reference to the DrinkItem structure.The function will return back a value of type double. This will be the price of that DrinkItem.

Function: name

string name(const DrinkItem &drinkItem);

The name function will return the drink name of the specified DrinkItem.

Function: quantity

unsigned int quantity(const DrinkItem &drinkItem);

The quantity function will return the quantity of the specified DrinkItem.

Function: sold

unsigned int sold(const DrinkItem &drinkItem);

The sold function will return the number of drinks sold for the specified DrinkItem.

Function: purchase

bool purchase(DrinkItem &drinkItem, double amount, double &change);

The purchase function takes three parameters and returns a value of type bool. The first parameter is a reference to the DrinkItem structure. The second parameter is the amount of money the customer is using to purchase the drink. This is of type double. The third parameter needs to be passed by reference. This will be the amount of change, if any, to be returned to the customer. The third parameter is of type double.

The return value is a bool value. A value of true indicates that the purchase succeeded. A value of false indicates that the drink could not be purchased.

If the drink can be purchased the fourth parameter will contain the amount of change (0.0 if there isnt any change) and the function will return true. If the purchase worked the function also needs to decrement the quantity of the DrinkItem structure in the array, and it needs to increment the purchased count for this item.

Function: dumpDrinkMachine

void dumpDrinkMachine(const DrinkMachine &drinkMachine);

The dumpDrinkMachine function takes a reference to a DrinkMachine structure as the only parameter and does not return a value. The function displays the contents of the drinks in the drink machine. Make sure you include the drink machine version number in the output.

The output should be formatted as follows:

Drink Machine version 1 Drink Id Drink Cost Quantity Sold 1 Cola 1.75 25 0 2 Root-beer 2.00 20 0 3 Lemon-lime 1.50 25 0 4 Water 1.00 40 0 5 Iced-tea 1.25 35 0 6 Orange 1.80 5 0 7 Iced-tea 1.65 35 0 

You will need to use setw to specify the column widths. The widths are 10 for columns Drink Id, Cost, Quantity, and Sold. The width is 15 for Drink (name).

Using all of the above functions you can now display the items in the drink machine using the following pseudo-code:

DrinkMachine drinkMachine; Bool worked = create(drinkMachine, "drink_machine.txt") If (worked) { For (unsigned int id = 1; id <= size(drinkMachine); id++) { DrinkItem &item = drink(drinkMachine, id) cout << id << " " << name(item) << " " << price(item) << " " << quantity(item) << endl; } } Else { Display The drink machine could not be created } destroy(drinkMachine, "updated_drink_machine.txt") 

Once a drink has been selected by the application user you can use that as the drink id and do a purchase as follows:

DrinkItem &item = drink(drinkMachine, id) // read in the amount and other logic goes here Bool result = purchase(item, amount, change) 

Note in the above we create a reference to a DrinkItem:

DrinkItem &item ... 

It is important that the item is a reference (the &). This gives us access to the item in the array and allows us to update it in the purchase function.

void sort(DrinkMachine &drinkMachine);

Sort the items in the drink machine in descending order by the sold value. This will update the drink machine passed in as the first parameter.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

d. Is the program accredited?

Answered: 1 week ago