providing the ProductInventory.cpp file, including the implementation of the Pro
ID: 3688795 • Letter: P
Question
providing the ProductInventory.cpp file, including the implementation of the ProductInventory member functions (described below):
ProductInventory and ~ProductInventory: creates an empty list, and deallocates all the nodes in the list, respectively.
addProduct(Product) ensures the product is unique, and price and quantity are valid. If so, adds a new node containing the product to either the beginning OR the end of the list. Returns true if successful, otherwise false.
removeProduct(string,string) removes a node with the given product name and locator from the linked list. Returns true if successful, otherwise false.
showInventory: displays a listing of the product inventory to the screen, nicely formatted, one product entry per line. Output locator, then quantity, then price, then product name.
getTotalQuantity: returns the total number of units of all of the products in the inventory.
findMinimum: returns the minimum product in the list, using the greaterThan() function over the products.
sortInventory: reorders the products in the list, using findMinimum.
testcase
Explanation / Answer
#include "Product.h"
#include <list>
class ProductInventory
{
private:
struct ProductNode // the Nodes of the linked list
{
Product data; // data is a product
ProductNode *next; // points to next node in list
};
ProductNode *productList; // the head pointer
public:
ProductInventory();
~ProductInventory();
bool addProduct(Product product);
bool removeProduct(string productName);
void showInventory();
int getTotalQuantity();
Product findMinimum(); // should be private, but public for testing
void sortInventory();
};
bool ProductInventory :: addProduct(Product product){
if(product.getQuantity() > 0 && product.getPrice() > 0){
ProductNode temp;
temp.data = product;
productList.insert(temp);
return true;
}
return false;
}
bool ProductInventory :: removeProduct(string productName){
for(int i=0;i<productList.size();i++){
if(productList[i].product.getProductName() == productName){
productList[i].remove();
return true;
}
}
return false;
}
void ProductInventory :: showInventory(){
for(int i=0;i<productList.size();i++){
cout<<" "<<productList[i].product.getProductName()<<" "<<productList[i].product.getLocator()<<" "<<productList[i].product.getPrice()<<" "<<productList[i].product.getQuantity();
}
}
int ProductInventory ::getTotalQuantity(){
int total = 0;
for(int i=0;i<productList.size();i++){
total = total + productList[i].product.getTotalQuantity();
}
}
Product ProductInventory :: findMinimum(){
int min = 9999;
Product temp;
for(int i=0;i<productList.size()-1;i++){
if(!productList[i].product.greaterThan(productList[i+1].product)){
temp = productList[i].product;
}
}
return temp;
}
void ProductInventory :: sortInventory(){
productList.sort();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.