Design and implement the following two C++ classes that manage the inventory of
ID: 3684872 • 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.
Explanation / Answer
ProductInventory.h
#ifndef ProductInventory_h
#define ProductInventory_h
#include <iostream>
#include "Product.h"
using namespace std;
class ProductInventory{
private:
Product *products;
void resize();
int size;
int productCount;
//stuff
public:
ProductInventory(int newSize);
bool addProduct(Product p);
bool removeProduct(string name, string locator);
void sortInventory();
int getTotalQuantity();
//stuff
};
#endif
ProductInventory.cpp
#include "Product.h"
#include "ProductInventory.h"
#include <iostream>
using namespace std;
//private:
Product *products; //Our array of products
int size; //Size of our array
int productCount = 0; //Number of products in our array
void ProductInventory::resize(){
//Creates new array to hold our products.
Product *newProducts = new Product[size*2];
//Copies over data from old array to new array.
for (int i=0; i < size; i++){
newProducts[i] = products[i];
}
//Set array to be new array.
products = newProducts;
//Sets new size.
size *= 2;
}
ProductInventory::ProductInventory(int newSize){
//Initialize products and size.
products = new Product[size];
size = newSize;
}
bool ProductInventory::addProduct(Product p){
//check for a duplicate
bool isUnique = true;
int i = 0;
do {
if (p.isEqual(products[i]){
isUnique = false;
}
i++;
} while (i < productCount);
if (isUnique){
//Resizes array if needed.
if (productCount == size) resize();
//Adds product to array.
products[productCount] = p;
//increases our product count
productCount++;
}
return isUnique;
}
bool ProductInventory::removeProduct(string name, string locator){
//Get index of our product (linear search cuz we dont care)
int i = 0;
do {
if (products[i].getName() == name && products[i].getLocator() == locator){
//we got it, now replace it
products[i] = products[productCount];
//and delete it
productCount--;
//and say that we did it
return true;
}
i++;
} while (i < productCount);
//we couldn't find it -_-
return false;
}
void ProductInventory::sortInventory(){
}
int ProductInventory::getTotalQuantity(){
return -1;
}
Product.cpp
#include <string>
#include "Product.h"
using namespace std;
//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)
string name;
//locator (string with no spaces, used to physically locate product, not unique)
string locator;
//quantity (how many of this product in stock, greater than or equal to 0)
int quantity;
//price (in dollars and cents, greater than 0)
float price;
//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)
Product::Product(){
}
//and one that accepts
//a value for each of the four member variables.
Product::Product(string pname, string plocator, int qty, float pr){
name = pname;
locator = plocator;
quantity = qty;
price = pr;
}
//• set and get all instance variables (make the instance variables private).
string Product::getName(){
return name;
}
void Product::setName(string newval){
name = newval;
}
string Product::getLocator(){
return locator;
}
void Product::setLocator(string newval){
locator = newval;
}
int Product::getQuantity(){
return quantity;
}
void Product::setQuantity(int newval){
quantity = newval;
}
float Product::getPrice(){
return price;
}
void Product::setPrice(float newval){
price = newval;
}
//• bool isEqual(Product): this product is equal to another if they have the same
//product name and locator values.
bool Product::isEqual(Product pr){
return((pr.getName() == name) && (pr.getLocator() == locator));
}
//• 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.
bool Product::greaterThan(Product pr){
if (name == pr.getName()){
return (locator > pr.getLocator());
} else {
return (name > pr.getName());
}
}
Product.h
#ifndef Product_h
#define Product_h
#include <string>
using namespace std;
class Product{
private:
string name;
string locator;
int quantity;
float price;
public:
Product();
Product(string pname, string plocator, int qty, float pr);
//• set and get all instance variables (make the instance variables private).
string getName();
void setName(string newval);
string getLocator();
void setLocator(string newval);
int getQuantity();
void setQuantity(int newval);
float getPrice();
void setPrice(float newval);
bool isEqual(Product p);
bool greaterThan(Product p);
};
#endif
main.cpp
#include<iostream>
#include<iomanip>
using namespace std;
#include "ProductInventory.h"
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();
}
Sample output:
add result = 1
add result = 1
box1 2 $105.75 Apple iPhone 3GS
box2 3 $172.50 Samsung Captivate SGH-I897
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.