ower lengstate / Bownloads / Lab 1 1%201) pdf In this lab, we will create an abs
ID: 3706943 • Letter: O
Question
ower lengstate / Bownloads / Lab 1 1%201) pdf In this lab, we will create an abstraction of a vending machine in C++, using structs to describe each item in the machine, and a vector to maintain a list of all the items in the machine. Use the following struct declaration for vending machine items: struct item string name: // name of the item int code: // numerical code entered to select the item float price; // price per unit of the item int stock: // number of items available in the machine Use the following vector declaration for the vending machine: int mainO vector vendingMachine; Complete the three following function headers shown below: float totalValue (vector vendingMachine) //return the total value of all items in the vending machine void stock (string name, int count, vectorkitem &vendingMachine;) //add items to the vending machine with the given "name" //add "count" items to the vending machine //if the item to be added was not already in the machine, prompt the user for its code and price (in dollars) //if the code is already being used by the machine, prompt the user ftor a different codeExplanation / Answer
// FileName: VendingMachine.cpp
#include<iostream>
#include<iomanip>
#include<string>
#include <vector>
using namespace std;
// Structure item definition
struct item
{
// To store the name of the item
string name;
// To store the code of the item
int code;
// To store the per unit price of the item
float price;
// To store number of items available in the machine
int stock;
};// End of structure
// Function to return total value of all the items in the vending machine
float totalValue(vector<item> vendingMachince)
{
// To store total amount in vending machine
float total = 0;
// Loops till end of the record
for(int c = 0; c < vendingMachince.size(); c++)
// Calculates the total
total += (vendingMachince[c].price * vendingMachince[c].stock);
// Returns the total amount
return total;
}// End of function
/*
* Function to add items to vending machine with the given "name" add "count" items to the vending machine
* If the item to be added was not already in the machine, prompt the user for its code and price (in dollar)
* If the code is already being used by the machine, prompt the user for a different code.
*/
void stock(string name, int count, vector<item> &vendingMachine)
{
// Initial found status is zero for not found
int found = 0;
// Loops till end of the record
for(int c = 0; c < vendingMachine.size(); c++)
{
// Checks each vendor name in the vector with the parameter name
if(vendingMachine[c].name.compare(name) == 0)
{
// If found set the status to 1
found = 1;
// Add the count to the existing stock
vendingMachine[c].stock += count;
// Display the updated information
cout<<" Your have added "<<count<<" units of "<<name<<". Now there are "<<vendingMachine[c].stock<<" units in the machine."<<endl;
// Come out of the loop
break;
}// End of if condition
}// End of for loop
// Checks if the found value is zero the item not found
if(found == 0)
{
// To store code found status
int codeFound;
// To store code entered by the user
int code;
cout<<" The item you are adding, "<<name<<", was not in the machine.";
cout<<" Please enter the following information for this item: "<<endl;
// Accept an item code from the user
cout<<" Code: ";
cin>>code;
// Loops till valid item code
do
{
// Code found status is zero for not found
codeFound = 0;
// Loops till end of the record
for(int c = 0; c < vendingMachine.size(); c++)
{
// Checks each vector object item code with the code entered by the user
if(vendingMachine[c].code == code)
{
// If found set it to one
codeFound = 1;
// Display the message to re-enter the code
cout<<" The code is already being used by "<<vendingMachine[c].name<<" Enter a new code: ";
cin>>code;
// Come out of the loop
break;
}// End of if condition
}// End of for loop
// Checks if the code found value is zero then code not exits
if(codeFound == 0)
// Come out of the loop
break;
}while(1); // End of do - while loop
// Declares an object of item
item newItem;
// Stores the code and name
newItem.code = code;
newItem.name = name;
// Accepts price
cout<<" Price: $";
cin>>newItem.price;
newItem.stock = count;
// Adds the item object to the vector vendingMachine
vendingMachine.push_back(newItem);
// Displays the updated information
cout<<" Your have added "<<count<<" units of "<<name<<". Now there are "<<count<<" units in the machine."<<endl;
}// End of if condition
}// End of function
/*
* Make a purchase from the vending machine
* "balance" is the amount of money (in dollar) inserted
* code is the item to purchase
* If code is not mapped to an item, display error message.
* If the item is more expensive than "balance", display error message.
* If purchase is successful, display change to be returned in dollar and update the stock of that item.
*/
void purchase(float balance, int code, vector<item> &vendingMachine)
{
// Initial found status is zero for not found
int found = 0;
// Loops till end of the record
for(int c = 0; c < vendingMachine.size(); c++)
{
// Checks each vector object item code with the parameter code
if(vendingMachine[c].code == code)
{
// If found set it to one
found = 1;
// Checks if the parameter balance is less than found item price
if(balance < vendingMachine[c].price)
{
// Displays the message
cout<<" Your have selected to purchase "<<vendingMachine[c].name<<", which costs $"<<vendingMachine[c].price;
cout<<" Your balance is $"<<balance<<". Not enough money."<<endl;
}// End of if condition
// Checks if the found item stock is zero the display out of stock message
else if(vendingMachine[c].stock == 0)
cout<<" Your have selected to purchase "<<vendingMachine[c].name<<" is out of stock."<<endl;
// Otherwise purchase the item
else
{
// Display the updated message
cout<<" Your have selected to purchase "<<vendingMachine[c].name<<", which costs $"<<vendingMachine[c].price;
// Calculates the change to return and displays it
cout<<" Thank you for purchasing! "<<" Your change is: $"<<balance - vendingMachine[c].price<<endl;
// Subtract the found item stock
vendingMachine[c].stock--;
}// End of else
// Come out of the loop
break;
}// End of if outer condition
}// End of for loop
// Checks if the found value is zero display item not found
if(found == 0)
cout<<" Item not found."<<endl;
}// End of function
// main function definition
int main()
{
// Creates a vector object to store item objects
vector<item> vendingMachine;
// Calls the function add a item
stock("Double-mint Gum", 1, vendingMachine);
cout<<"---------------------------------------------------------------"<<endl;
// Calls the function to purchase an item
purchase(0.50, 100, vendingMachine);
cout<<"---------------------------------------------------------------"<<endl;
// Calls the function add a item
stock("Chocolate Chip Cookies", 10, vendingMachine);
cout<<"---------------------------------------------------------------"<<endl;
// Calls the function add a item
stock("Chocolate Chip Cookies", 5, vendingMachine);
cout<<"---------------------------------------------------------------"<<endl;
// Calls the function to purchase an item
purchase(0.75, 100, vendingMachine);
cout<<"---------------------------------------------------------------"<<endl;
// Calls the function to purchase an item
purchase(1.00, 100, vendingMachine);
cout<<" The vending machine now has $"<<totalValue(vendingMachine)<<fixed<<setprecision(2)<<" worth of items."<<endl;
return 0;
}// End of main function function
Sample Output:
The item you are adding, Double-mint Gum, was not in the machine.
Please enter the following information for this item:
Code: 100
Price: $0.75
Your have added 1 units of Double-mint Gum. Now there are 1 units in the machine.
---------------------------------------------------------------
Your have selected to purchase Double-mint Gum, which costs $0.75
Your balance is $0.5. Not enough money.
---------------------------------------------------------------
The item you are adding, Chocolate Chip Cookies, was not in the machine.
Please enter the following information for this item:
Code: 100
The code is already being used by Double-mint Gum
Enter a new code: 101
Price: $2.00
Your have added 10 units of Chocolate Chip Cookies. Now there are 10 units in the machine.
---------------------------------------------------------------
Your have added 5 units of Chocolate Chip Cookies. Now there are 15 units in the machine.
---------------------------------------------------------------
Your have selected to purchase Double-mint Gum, which costs $0.75
Thank you for purchasing! Your change is: $0
---------------------------------------------------------------
Your have selected to purchase Double-mint Gum is out of stock.
The vending machine now has $30 worth of items.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.