Design and implement the following two C++ classes that manage the inventory of
ID: 3685337 • Letter: D
Question
Design and implement the following two C++ classes that manage the inventory of a small store.
(First Class) Product: For each product, you should store the following info: product name (i.e. “Apple iPhone 3GS 8GB”, may contain spaces in it, not unique) locator (string with no spaces, used to physically locate product, not unique) quantity (how many of this product in stock, greater than or equal to 0) price (in dollars and cents, greater than 0) Note: For a given product, the product name AND locator together must be unique. So you may have two entries for “iPhone 5c” if one has “box3” for the locator and the other has “shelf12” .
You should implement the following operations for a Product:
• You should implement two constructors: one that takes no arguments (quantity and price are 0, product name and locator are empty strings), and one that accepts a value for each of the four member variables.
• set and get all instance variables (make the instance variables private).
• bool isEqual(Product): this product is equal to another if they have the same product name and locator values.
• bool greaterThan(Product): this product is greaterThan another if its product name is greater than the others, OR if they have the same product names, if this product’s locator is greater than the other’s locator.
(Second Class) ProductInventory: When a product inventory object is created, it should dynamically allocate an array of Product, using a constructor parameter to specify the size. (You should also implement a destructor).
You should implement the following operations over the small store inventory:
addProduct: takes a product and adds it to the inventory. If the inventory is full, it should call the resize function first (see below). If a product with the same name and locator is already in the inventory, the add should fail and return false. If the quantity or price are invalid, it should fail and return false.
removeProduct: takes a product name and locator and removes any matching Product from the inventory. Returns true if a product was removed.
showInventory: displays a listing of the store inventory to the screen, one product entry per line. Output the locator, then quantity, then price, then product name.
sortInventory: reorders the products in the list, using the greaterThan(Product) function (does not display them).
getTotalQuantity: returns the total number of units of all of the products in the inventory (this is not the size of the inventory array).
resize: internal function that doubles the size of the inventory array (somewhat like duplicateArray). Allocates a new array, and copies all the existing products to it. Be sure to clean up.
Input/Output: The main function should be a driver program that tests the functionality of the Product and ProductInventory classes.
You do NOT need to use binary search.
You do NOT need to keep the array sorted.
If someone wants the inventory to be sorted, they will need to call sortInventory.
Do not add extra I/O to the class functions! Only showInventory should do I/O.
Driver:
int main()
{
ProductInventory pi(40);
Product product1("box1", 2, 105.75, "Apple iPhone 3GS");
Product product2("box2", 3, 172.50, "Samsung Captivate SGH-I897");
bool result = pi.addProduct(product1);
cout << "add result = " << result << endl;
result = pi.addProduct(product2);
cout << "add result = " << result << endl;
pi.showInventory();
}
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
#include "ProductInventory.h"
int main()
{
Product product0;
product0.setLocator("box1");
product0.setQuantity(2);
product0.setPrice(111.99);
product0.setProductName("Apple iPod Touch 4th Generation 8GB");
cout << setw(6) << product0.getLocator() << " " << product0.getQuantity()
<< " $" << product0.getPrice()
<< " " << product0.getProductName() << endl;
ProductInventory pi(40);
Product P1("box1", 2, 105.75, "Apple iPhone 3GS");
Product P2("box1", 3, 172.50, "Samsung Captivate SGH-I897");
Product P3("box3", 2, 322.50, "Apple iPad 2 16GB WiFi");
bool result = pi.addProduct(P1);
cout << "add result = " << result << endl;
pi.showInventory();
result = pi.addProduct(P2);
cout << "add result = " << result << endl;
pi.showInventory();
result = pi.addProduct(P3);
cout << "add result = " << result << endl;
pi.sortInventory();
pi.showInventory();
bool delResult;
delResult = pi.removeProduct(P2.getProductName(),P2.getLocator());
cout << "delete result = " << delResult << endl;
delResult = pi.removeProduct(P3.getProductName(),P3.getLocator());
cout << "delete result = " << delResult << endl;
pi.showInventory();
cout << "total quantity =" << pi.getTotalQuantity() << endl;
result = pi.addProduct(P5);
cout << "add result = " << result << endl;
pi.showInventory();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.