Question
Can someone do this in C++ please! Thank you very much. For this lab, you will be creating a program that simulates a simple vending
Can someone do this in C++ please! Thank you very much.
For this lab, you will be creating a program that simulates a simple vending machine that sells chips, candy, and soda. Each of these snacks has a different price and will have a different amount in stock. When the program is running, the current contents of the vending machine will be displayed to the user, and then a prompt to enter a number of quarters will appear. After the user enters a number, the amount of money they entered will appear in dollars and cents. For example, if the user entered 7 quarters, $1.75 would be displayed. Next, the user will be prompted to make a selection from the vending machine. If they have entered enough money and the snack is in-stock, the item will be dispensed, and the number in-stock will decrease by one. If the user entered more money than necessary to make the purchase, a message will be displayed indicated that the appropriate amount of change has been returned. See the sample run at the end for clarification.
Requirements First, design a Snack class that... 1. contains the following private member variables: string - name of snack double - price of snack int - quantity in stock int - number of items sold You will also need to create the appropriate accessor and mutator functions for these member variables.
2. has two constructors A default constructor that initializes the name of the snack to an empty string, and the price, quantity, and number of items sold to 0 An overloaded constructor that accepts a string, a double, and an int and assigns these values to the name, price, and quantity member variables, respectively. The number of items sold should be initialized to 0.
3. has a destructor that display the quantity still in stock, the number of items sold, and the money gained from the sales. Remember that destructors are called when objects are destroyed, so you can think of the destructor in this case as a closing report for the days sales. See the sample run at the end for the format.
4. has a public buyItem method that: Has the following function header: bool buyItem(double& moneyEntered) If moneyEntered >= price, take away the appropriate value from the moneyEntered argument, decrease the quantity in stock by one, increase the number of items sold by one, tell the user that the item has been dispensed, and return true If moneyEntered < price, display an appropriate error message and return false if the item is sold-out, display an appropriate error message and return false note that we are passing the moneyEntered variable by reference meaning that if something is bought, the variable will be decreased after the function call Inside main, create an array to hold three objects from the Snack class: chips, candy, and soda. the chips object should use the default constructor; this means you will need to use the mutator functions to set the member variables the candy and soda objects can use the overloaded constructor to initialize their member variables.
Inside main, create an array to hold three objects from the Snack class: chips, candy, and soda. the chips object should use the default constructor; this means you will need to use the mutator functions to set the member variables the candy and soda objects can use the overloaded constructor to initialize their member variables
Use the following table to initialize the objects: Name Chips Candy Soda Price $1.75 $1.25 $1.00 Quantity in stock 3 5 2 Once the array has been created, create a loop in main that: first displays the contents of the array in a formatted fashion then asks the user to either enter a number of quarters or exit the program if the user exits, the program ends (destructors should be called) once the user has inputted a valid number of quarters, ask them to make a selection from the vending machine once a valid selection has been made, call the objects buyItem method and loop again
To accomplish this, you will need to create the following functions: void displayVendingMachine(const Snack [], int); This function is responsible for displaying the vending machines contents. It accepts the array of Snack objects and the number of elements in the array and displays the contents in a formatted fashion (see sample run for how to format and what to include)
int getQuarters(); This function should prompt the user to enter a number of quarters. If the number entered is less than 1, display an error message and prompt them again. Once a valid number is entered, return that value.
void userBuyItem(Snack [], int); This function accepts the array of Snack objects and the number of elements in the array and should first display the contents of the vending machine and prompt the user to enter a number of quarters (you have functions for this, so this can be accomplished in two lines). It will then display the amount entered in dollars and cents (see sample run for clarification). The user will then be prompted to enter an item number to buy. If an invalid item number is entered, display an error message and prompt them again. Once a valid choice has been made, use the appropriate method on the appropriate Snack object to buy the item. Remember that you need to pass the buyItem method of the Snack class a double representing the amount of money entered; also remember that the method will automatically decrement the variable if something is bought. If the user has any change remaining after the transaction, tell the user how much change they are getting.
bool promptToContinue(); This function will ask the user if they want to continue running the program. The user input should be case-insensitive. If the user enters Y or y, return true; otherwise, return false Once you have the above completed, you should have a functioning vending machine! Only move onto this part if you have the above code working.
This additional requirement is to ensure that you havent forgot how to use pointers. Currently, your destructor displays closing information for the Snack. For example: Closing info for Soda: 0 in stock 2 sold for a profit of $2.00 You will now add an additional part to your destructor that will display the times at which successful transactions occurred. A successful transaction means that a snack was sold. For example, your destructor should now output: Closing info for Soda: 0 in stock 2 sold for a profit of $2.00 Transactions occurred at: 21:36:11 21:36:20
In order to accomplish this, you will need to change a few things. First, you will need to add a private method, getCurrentTime, to the Snack class. This method uses things not covered in 22B, so the code is given to you below. However, you are responsible for its proper integration and use. string getCurrentTime() { time_t t = time(0); struct tm ts; char buff[10]; ts = *localtime(&t); strftime(buff, sizeof(buff), "%X", &ts); return buff; } IMPORTANT! include the ctime library for the above code to work (#include ) Remember: do not worry about the contents of the function, just worry about the return type so that you can properly use it This method should be called whenever an item is successfully bought to store the current time that the transaction occurred. The time of the transaction should be stored in a dynamicallyallocated array. This means you will need add a string pointer private member variable to your class. There are a few important things to keep in mind: 1. The size of the dynamically-allocated array will be determined by the number of items instock. When the first item is sold, the time of the transaction should be stored in the first index of the array. The second successful sale should store the item in the second index, and so on.
2. Remember that your class has two constructors. If the default constructor is called, remember that you do not yet know the number of items in stock. If the overloaded constructor is called, you will know how many items are in stock, so you will create your dynamically-allocated array. Because you are required to use the default constructor for the chips object, add code to dynamically-allocate the array in the mutator function for the member variable that holds the number in stock.
3. Your destructor should output the times of the transaction ONLY if the array has times. If no times are stored (meaning nothing was bought), do not output anything. Remember that you need to appropriately clean the heap.
Sample Run Welcome to the vending machine! Item # Item Name Price # in-stock --------------------------------------------- 1 Chips 1.75 3 2 Candy 1.25 5 3 Soda 1.00 2 Enter a number of quarters: -1 Please enter a number greater than 0 Enter a number of quarters: 8 Amount Entered: $2.00 Enter a number between 1 and 3 to make your selection: 3 Item has been dispensed below $1.00 dispensed below Continue? (Y / N): y Item # Item Name Price # in-stock --------------------------------------------- 1 Chips 1.75 3 2 Candy 1.25 5 3 Soda 1.00 1 Enter a number of quarters: 4 Amount Entered: $1.00 Enter a number between 1 and 3 to make your selection: 3 Item has been dispensed below Continue? (Y / N): y Item # Item Name Price # in-stock --------------------------------------------- 1 Chips 1.75 3 2 Candy 1.25 5 3 Soda 1.00 0 Enter a number of quarters: 4 Amount Entered: $1.00 Enter a number between 1 and 3 to make your selection: 3 Error: item is sold-out! $1.00 dispensed below Continue? (Y / N): y Item # Item Name Price # in-stock --------------------------------------------- 1 Chips 1.75 3 2 Candy 1.25 5 3 Soda 1.00 0 Enter a number of quarters: 10 Amount Entered: $2.50 Enter a number between 1 and 3 to make your selection: 1 Item has been dispensed below $0.75 dispensed below Continue? (Y / N): y Item # Item Name Price # in-stock --------------------------------------------- 1 Chips 1.75 2 2 Candy 1.25 5 3 Soda 1.00 0 Enter a number of quarters: 5 Amount Entered: $1.25 Enter a number between 1 and 3 to make your selection: 2 Item has been dispensed below Continue? (Y / N): y Item # Item Name Price # in-stock --------------------------------------------- 1 Chips 1.75 2 2 Candy 1.25 4 3 Soda 1.00 0 Enter a number of quarters: 9 Amount Entered: $2.25 Enter a number between 1 and 3 to make your selection: 1 Item has been dispensed below $0.50 dispensed below Continue? (Y / N): n Closing info for Soda: 0 in stock 2 sold for a profit of $2.00 Transactions occurred at: 00:25:55 00:26:03 Closing info for Candy: 4 in stock 1 sold for a profit of $1.25 Transactions occurred at: 00:26:24 Closing info for Chips: 1 in stock 2 sold for a profit of $3.50 Transactions occurred at: 00:26:17 00:26:38
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started