Question: // My Code #include #include #include using namespace std; struct Product{ string name; int qty; double price; }; int menu(){ char choice; while(choice < '1'

// My Code

#include #include #include using namespace std; struct Product{ string name; int qty; double price; };

int menu(){ char choice; while(choice < '1' || choice > '8'){ // Display a menu. cout << endl; cout << "1. Add new product "; cout << "2. Sort by Name "; cout << "3. Sort by Qty "; cout << "4. Average Price "; cout << "5. Highest Price "; cout << "6. Display out of stock "; cout << "7. Display all products in inventory "; cout << "8. Exit the program "; cout << "Enter your choice: "; // Get the user's choice.cout << "Enter your choice: cin >> choice; if(choice < '1' || choice > '8'){ cout << "Please enter a valid choice!" << endl; } } return (int)(choice - '0'); }

void addprod(vector *bginventory){ struct Product temp; cout << endl; cout << "Enter product name : "; cin >> temp.name; cout << "Enter product quantity: "; cin >> temp.qty; cout << "Enter product price : "; cin >> temp.price; bginventory->push_back(temp); }

void sortbyname(vector *bginventory){ int size = bginventory->size(); for (int i = 0; i < size - 1; i++) { int min = i; for (int j = i + 1; j < size; j++) { if(strcmp(bginventory->at(min).name.c_str(), bginventory->at(j).name.c_str()) > 0) { min = j; } } if (min != i){ swap(bginventory->at(i), bginventory->at(min)); } } }

void sortbyqty(vector *bginventory){ int size = bginventory->size(); for (int i = 0; i < size - 1; i++) { int min = i; for (int j = i + 1; j < size; j++) { if(bginventory->at(min).qty > bginventory->at(j).qty) { min = j; } } if (min != i){ swap(bginventory->at(i), bginventory->at(min)); } } }

void showavgprice(vector bginventory){ double sum = 0; int count = bginventory.size(); vector::iterator iter; for(iter = bginventory.begin(); iter != bginventory.end(); ++iter){ sum += (*iter).price; } cout << endl; cout << "Average price is " << sum / (double)count << endl; }

void showhiprice(vector bginventory){ string name = ""; double hiprice = -1; vector::iterator iter; for(iter = bginventory.begin(); iter != bginventory.end(); ++iter){ if((*iter).price > hiprice){ hiprice = (*iter).price; name = (*iter).name; } } cout << endl; cout << "Product name : " << name << endl; cout << "Product price: " << hiprice << endl; }

void showoutofstock(vector bginventory){ cout << endl; cout << "Name\tQty\tPrice" << endl; vector::iterator iter; for(iter = bginventory.begin(); iter != bginventory.end(); ++iter){ if((*iter).qty == 0){ cout << (*iter).name << "\t" << (*iter).qty << "\t" << (*iter).price << endl; } } }

void showinventory(vector bginventory){ cout << endl; cout << "Name\tQty\tPrice" << endl; vector::iterator iter; for(iter = bginventory.begin(); iter != bginventory.end(); ++iter){ cout << (*iter).name << "\t" << (*iter).qty << "\t" << (*iter).price << endl; } }

int main(){ int choice; vector bginventory; choice = menu(); while(choice != 8){ if(choice == 1){ addprod(&bginventory); } else if(choice == 2){ sortbyname(&bginventory); } else if(choice == 3){ sortbyqty(&bginventory); } else if(choice == 4){ showavgprice(bginventory); } else if(choice == 5){ showhiprice(bginventory); } else if(choice == 6){ showoutofstock(bginventory); } else{ showinventory(bginventory); } choice = menu(); } return 0; }

// Instructions

Now that you have completed your first program. Your task is to modify that program. This task will not require a lot of coding, but it is an excellent precursor to your next program and a great introduction to how objects differ from structs. You will convert your program into a program that contains a Product class instead of a Product struct. Here are the details.

1. Create three files to achieve your new task.

a.Product.h Will contain the class definition that will consist of either overloaded constructors or a constructor with default parameters, getter and setter functions for all data members, a new function call isEmpty and your existing data members made private.

b.Product.cpp Must contain at least three getter or setter functions (some of the getter or setter functions can be made inline functions.). The new function isEmpty will return the boolean true if the qty is zero, otherwise return false. (see Figure 4.15.1 to return a bool)

c.ProductClient.cpp Will contain your main function along with the supporting functions. You will need to modify the code now that you are using objects instead of structs. You will also modify your OutOfStock function to use the isEmpty member function in your if statement condition.

// Additional Instructions

b. In-line comments: There should be an in-line comment for each main step in your program. In general, this means a comment with each group of statements that handles the input, the processing and the output steps of your program.

c. Function comments. A sentence or two should be included at the beginning of each function to explain its purpose.3. Declare all variables used by a function at the beginning of the function.

4. Use meaningful variable names

p.s in c++ please

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!