Product - UPC:int - manufacturer:string - productName:string - price:double -qua
ID: 3547428 • Letter: P
Question
Product
- UPC:int
- manufacturer:string
- productName:string
- price:double
-quantity:int
+ Product(iManufacturer:string, iProductName:string, iPrice:double, iQuantity:int)
+ Product()
+ getUPC( ):int
+ getManufacturer( ):string
+ getProductName( ):string
+ getPrice( ):double
+ getQuantity( ):int
+ getValueOfInventory( ):double
+ setManufacturer(input:string):void
+ setProductName(input:string):void
+ setPrice(input:double):void
+ setQuantity(input:int):void
+ incrementQuantity( ):void
+ decrementQuantity( ):void
+ toString( ):string
This is the UML of the code
This is a vector based program using class vector Your vector will start with a size of zero (0) and it will be declared inside of main.
This will still be a menu driven program. When the program starts running your vector will not have any products in it. Your menu will now be:
Add a product will prompt the user for each piece of information and then it will create a new product in the vector. Delete product will remove the selected product from the vector.
For this class the overloaded constructor will generate a random UPC number (notice that the variable is a constant). The default constructor will also generate a UPC number, but will set the other values to the default
The get and set functions will either return the values from the private fields or set them to the inputted values.
The function getValueOfInventory will compute the value of the inventory and return that value as a double.
The toString method will format the complete output for one product so that if you wanted to print the product out you just have to call the toString method and the returned string will be in the proper format. For this function you will need to use the get functions and not directly access the private fields.
The increment and decrement functions will increment or decrement the private field Quantity by 1.
Product
- UPC:int
- manufacturer:string
- productName:string
- price:double
-quantity:int
+ Product(iManufacturer:string, iProductName:string, iPrice:double, iQuantity:int)
+ Product()
+ getUPC( ):int
+ getManufacturer( ):string
+ getProductName( ):string
+ getPrice( ):double
+ getQuantity( ):int
+ getValueOfInventory( ):double
+ setManufacturer(input:string):void
+ setProductName(input:string):void
+ setPrice(input:double):void
+ setQuantity(input:int):void
+ incrementQuantity( ):void
+ decrementQuantity( ):void
+ toString( ):string
Explanation / Answer
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#include<sstream>
using namespace std;
class Product
{
private:
int UPC;
string manufacturer;
string productName;
double price;
int quantity;
public:
Product(string iManufacturer,string iProductName,double iPrice,int iQuantity)
{
UPC = rand()%10000+1;
manufacturer = iManufacturer;
productName = iProductName;
price = iPrice;
quantity = iQuantity;
}
Product()
{
UPC = rand()%10000+1;
manufacturer = "";
productName = "";
price = 0.0;
quantity = 0;
}
int getUPC()
{
return UPC;
}
string getManufacturer()
{
return manufacturer;
}
string getProductName()
{
return productName;
}
double getPrice()
{
return price;
}
int getQuantity()
{
return quantity;
}
double getValueOfInventory()
{
return price*quantity;
}
void setManufacturer(string input)
{
manufacturer = input;
}
void setProductName(string input)
{
productName = input;
}
void setPrice(double input)
{
price = input;
}
void setQuantity(int input)
{
quantity = input;
}
void incrementQuantity()
{
quantity++;
}
void decrementQuantity()
{
quantity--;
}
string toString()
{
stringstream ss;
ss<< "UPC name is " << getUPC() << "Manufactuer is " << getManufacturer()
<< "Product Name is" << getProductName()
<< "Price is " << getPrice()
<< "Quantity is " <<quantity;
return ss.str();
}
};
int main()
{
vector<Product> Vec_array;
srand(time(NULL));
int choice;
while(true)
{
cout <<"1. Display Products" << endl;
cout <<"2. Add a Product" << endl;
cout <<"3. Edit a Product " << endl;
cout <<"4. Delete a Product" << endl;
cout <<"5. Exit" << endl;
cout <<"Enter Your Choice :";
cin >> choice;
cout << endl;
switch(choice)
{
case 1:
{
for(vector<Product>::iterator it=Vec_array.begin(); it!=Vec_array.end(); it++)
{
(*it).toString();
}
}
break;
case 2:
{
string manufacturer;
cout <<"Enter manufacturer name :";
cin >> manufacturer;
cout << endl;
string productName;
cout <<"Enter productName name :";
cin >> productName;
cout << endl;
double price;
cout <<"Enter price :";
cin >> price;
cout << endl;
int quantity;
cout <<"Enter quantity :";
cin >> quantity;
cout << endl;
Product P(manufacturer,productName,price,quantity);
Vec_array.push_back(P);
}
break;
case 3:
{
string productName;
cout <<"Enter productName name :";
cin >> productName;
cout << endl;
for(vector<Product>::iterator it=Vec_array.begin(); it!=Vec_array.end(); it++)
{
if(productName.compare((*it).getProductName())==0)
{
cout <<"Enter New product name :";
cin >> productName;
cout << endl;
(*it).setProductName(productName);
break;
}
}
}
break;
case 4:
{
string productName;
cout <<"Enter productName name :";
cin >> productName;
cout << endl;
for(vector<Product>::iterator it=Vec_array.begin(); it!=Vec_array.end(); it++)
{
if(productName.compare((*it).getProductName())==0)
{
Vec_array.erase(it);
break;
}
}
}
break;
case 5: system("pause"); return 0;
}
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.