Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program in C++ that simulates a soft drink machine . The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of

Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem,

DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class.

DrinkItem class

The DrinkItem class will contains the following private data members:

name: Drink name (type of drink read in from a file). Type is std::string.

price: 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. Type is double.

quantity: Number of drinks, of this type, in the machine. Initial value is read in from a file.

This is also updated by the program. Type unsigned int.

purchased: Drinks purchased. Initially 0. Updated whenever a drink is purchased. Type is

unsigned int.

sales: This is used to keep track of the amount of money paid for all of the drinks purchased.

Every time there is a successful purchase the amount of the purchase should be added to sales.

You need to initially set this to 0. Type is double.

You need to have public accessors for all of the above (five) data members. Make sure you accessors are const member functions. Only the price, and name data members should have a mutator functions. So you will have five get member functions (accessors) but only two set member functions (mutators).

Example:

The prototype for the accessor for price would be:

double getPrice() const;

and the mutator would be:

void setPrice(double newPrice);

You will also need the following additional public member functions / constructors:

void addDrinks(unsigned int amount)

When this member function is called your program needs to update the quantity by the specified amount.

bool purchase()

The purchase member function will check to see if there are any drinks left. If there is at least 1 drink

remaining the purchase member function will subtract 1 drink from the number of drinks, add 1 to the

drinks purchased, add the cost of the drink to sales, and return a true value.

If the number of drinks is 0 when this member function is called no member data should be updated and

a value of false should be returned.

DrinkItem constructors (two total)

The DrinkItem class will have one constructor that takes the input values name, price and quantity (in

that order). The purchased and sales member data variables will be set to 0.

The second constructor is a default constructor (no arguments) that will set the numeric values to 0 and

the name to "";

Create a header file drinkitem.h for the class declaration and create a drinkitem.cpp file for the member

functions. You can inline the accessors and mutations. Do not inline any of the other member functions

and do not inline the constructors.

DrinkMachine class

The DrinkMachine class will contain the following data members:

An unsigned int that contains a version number. For this assignment this will have a value

of 1.

An unsigned int that contains the actual number of DrinkItem objects being used in the drink item array.

An array of DrinkItem objects. Each element of the array will be a DrinkItem object. This array will contain a maximum of 25 elements. You will keep track on the actual number in use via the unsigned int value you created above.

An unsigned int that contains the maximum number of DrinkItem objects. This is used internally by the drink machine to make sure you dont put more than 25 DrinkItem objects in the array.

The public member functions in the DrinkMachine class are:

DrinkMachine() constructor

The constructor will take no parameters. The various drink machine values will be read in from the file drink.txt. The constructor will also write the drink machine values (read in from the file) to a file called drink_backup.txt. This way you will have a backup copy of your file in case there are bugs in your code. The first value in the input file, drink.txt, is the number of drink items. There will then be drink information for each of the drink items specified by the first value in the file. A drink item is made up of a name, price and quantity. The first drink item you read in will go into the array of DrinkItem value at index 0. The next one will go into the entry with index 1, and so on.

~DrinkMachine() destructor

The destructor will write the current list of items back to the output file, drink.txt. This will include the new quantity values for the DrinkItem objects.

unsigned int size() const

Returns the current number of DrinkItem entries being used by the drink machine. These are the ones read in from the input file.

unsigned int max_size() const

Returns the maximum number of DrinkItem entries allowed in the drink machine.

DrinkItem& at(unsigned int index)

Return a reference to the drink item at the specified index.

const DrinkItem& at(unsigned int index) const

Returns a const reference to the specified drink item. This allows you to use a function that uses a

const DrinkItem reference. You cannot update the DrinkItem object in this case.

bool available(unsigned int drinkId) const

Return true if the drink item at index drinkId has a quantity of 1 or more. Return false otherwise.

double getPrice(unsigned int drinkId) const

Get the price of the item at the specified index.

Receipt purchase(unsigned int drinkId, double amount)

Purchase an item at the specified index. If the purchase worked return a Receipt object with any

change. If the drink could not be purchased (the quantity was 0) or if the funds were insufficient return

Receipt with change of 0 and either insufficient or empty turned on. Check for the funds amount first.

If the funds are enough for the purchase then check the quantity. Update the quantity and purchased

totals as appropriate. Note your return statement will look similar to the following:

Receipt purchase(unsigned int drinkId, double amount)

{

// code goes here

return Receipt(); // where is the rest of the constructor

}

You are returning back a temporary object here. The return type for the purchase member function is

also Receipt, which is a temporary object. The object will be deleted once it has been assigned in the

calling function. You do not want to return by reference, it needs to be a copied temporary object. You

also do not want to create it with the new operator or you will be creating a memory leak in your

program.

void addDrinks(unsigned int drinkId, unsigned int amount)

Add the specified number of drinks for the specified drink item index.

void print(std::ostream& outStream) const

Print out the current contents of the drink machine including the drink machine version, the number of

drink items in the machine and all of the information about the drink items (index, name, price,

quantity, number of drinks purchased, and the sales for this drink).

Here is a sample of the output (you must have all of this data in your output). Make sure you align the

output as shown here. Your spacing may be different.

Drink Machine Version 1

Number of entries: 8

Id Name Price $ Qty Sold Sales $

0 Cola 1.25 24 1 1.25

1 Root-beer 1.25 19 1 1.25

2 Lemon-lime 1.25 25 0 0.00

3 Water 1.00 39 1 1.00

4 Orange 1.25 0 5 6.25

5 Iced-tea 1.25 35 0 0.00

6 Grape 1.30 15 0 0.00

7 Iced-coffee 2.00 34 1 2.00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

Describe the primary differences between goods and service firms.

Answered: 1 week ago