In this assignment, you’ll make an inventory system for a store’s items, includi
ID: 3781395 • Letter: I
Question
In this assignment, you’ll make an inventory system for a store’s items, including produce and books. The starter program is an inventory system for only produce.
1. Include the price of an item by adding to the Item class the protected data member int priceInDollars that stores the price in dollars of an individual item.
Write a public function SetPrice with a single int parameter prcInDllrs and returns nothing. SetPrice assigns the value of prcInDllrs to priceInDollars.
Modify the AddItemToInventory to prompt the user to “Enter the price per item: $”, then set the Produce’s price to the userentered value.
Modify Produce’s Print function output to include the item’s individual price. Here is an example output for 20 grape bundles at $3 each that expire on May 22.
Grape bundle x20 for $3 (Expires: May 22)
2. Add a new class Book that derives from Item. The Book class has a private data member that is a string named author. The Book class has a public function member SetAuthor with the parameter string authr that sets the private data member author’s value. SetAuthor returns nothing.
The class Book overloads the Print member function with a similar output as the Produce’s Print function, except “Expires” is replaced with “Author” and the variable “expiration” is replaced with “author”. Here is an example output for 22 copies of To Kill a Mockingbird by Harper Lee that sells for $5 each:
To Kill a Mockingbird x22 for $5 (Author: Harper Lee)
3. Modify the AddItemToInventory function to allow the user to choose between adding a book (b) and produce (p). If the user does not enter ‘b’ or ‘p’, then output “Invalid choice” then return the AddItemToInventory function.
4. Create a class named Inventory. The Inventory class should have one private data member:
vector<Item*> inventory. The Inventory class has four public function members that have no parameters and return nothing: PrintInventory, AddItemToInventory, UpdateItemQtyInInventory, and RemoveItemFromInventory.
Development suggestion: First convert only the PrintInventory function to be a member function of Inventory with the other functions disabled (commented out). Then, convert the other functions one by one.
5. Add to the Inventory class a private data member int totalInvPriceInDollars that stores the total inventory price in dollars.
Write a private member function SumInv in Inventory class that has no parameters and returns nothing. The SumInv function should set the total inventory value as a price in dollars. Since the Inventory class cannot access an Item’s quantity and priceInDollars, you’ll need to write a public member function GetTotalValueAsPrice to the Item class that returns the multiplication of the Item’s quantity and priceInDollars.
The SumInv function should be called at the end (but before the return statement) of each Inventory member function that modifies the inventory: AddItemToInventory, UpdateItemQtyInInventory, and RemoveItemFromInventory.
Modify the PrintInventory function to also output the total inventory value in dollars. Here is an example:
0 Grapes x2 for $3 (Expires: May)
1 To Kill a Mockingbird x4 for $5 (Author: Harper Lee)
Total inventory value: $26
Here is an example program execution (user input is highlighted here for clarity):
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:a
Enterchoiceofadding(b)ookor(p)roduce:p
Enternameofnewproduce:Grapebundle
Enterquantity:20
Enterexpirationdate:May12
Enterthepriceperitem:$2
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:p
0-Grapebundlex20for$2(Expires:May12)
Totalinventoryvalue:$40
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:a
Enterchoiceofadding(b)ookor(p)roduce:b
Enternameofnewbook:Illiad
Enterquantity:10
Enterauthor:Homer
Enterthepriceperitem:$5
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:p
0-Grapebundlex20for$2(Expires:May12)
1-Illiadx10for$5(Author:Homer)
Totalinventoryvalue:$90
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:a
Enterchoiceofadding(b)ookor(p)roduce:b
Enternameofnewbook:ToKillaMockingbird
Enterquantity:2
Enterauthor:HarperLee
Enterthepriceperitem:$10
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:u
0-Grapebundlex20for$2(Expires:May12)
1-Illiadx10for$5(Author:Homer)
2-ToKillaMockingbirdx2for$10(Author:HarperLee)
Totalinventoryvalue:$110
Updatewhichitem#:2
Enternewquantity:25
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:p
0-Grapebundlex20for$2(Expires:May12)
1-Illiadx10for$5(Author:Homer)
2-ToKillaMockingbirdx25for$10(Author:HarperLee)
Totalinventoryvalue:$340
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:r
0-Grapebundlex20for$2(Expires:May12)
1-Illiadx10for$5(Author:Homer)
2-ToKillaMockingbirdx25for$10(Author:HarperLee)
Totalinventoryvalue:$340
Removewhichitem#:1
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:p
0-Grapebundlex20for$2(Expires:May12)
1-ToKillaMockingbirdx25for$10(Author:HarperLee)
Totalinventoryvalue:$290
Enter(p)rint,(a)dd,(u)pdate,(r)emove,or(q)uit:
Starter code used for this assignment--
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Item {
public:
void SetName(string nm)
{ name = nm; };
void SetQuantity(int qnty)
{ quantity = qnty; };
virtual void Print()
{ cout << name << " " << quantity << endl; };
virtual ~Item()
{ return; };
protected:
string name;
int quantity;
};
class Produce : public Item { // Derived from Item class
public:
void SetExpiration(string expir)
{ expiration = expir; };
void Print()
{ cout << name << " x" << quantity
<< " (Expires: " << expiration << ")"
<< endl;
};
private:
string expiration;
};
// Print all items in the inventory
void PrintInventory(vector<Item*> inventory);
// Dialogue to create a new item, then add that item to the inventory
vector<Item*> AddItemToInventory(vector<Item*> inventory);
// Dialogue to update the quantity of an item, then update that item in the inventory
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory);
// Dialogue to remove a specific item, then remove that specific item from the inventory
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory);
int main() {
vector<Item*> inventory;
string usrInptOptn = "default";
while (true) {
// Get user choice
cout << " Enter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
getline(cin, usrInptOptn);
// Process user choice
if (usrInptOptn.size() == 0) {
continue;
} else if (usrInptOptn.at(0) == 'p') {
PrintInventory(inventory);
} else if (usrInptOptn.at(0) == 'a') {
inventory = AddItemToInventory(inventory);
} else if (usrInptOptn.at(0) == 'u') {
inventory = UpdateItemQtyInInventory(inventory);
} else if (usrInptOptn.at(0) == 'r') {
inventory = RemoveItemFromInventory(inventory);
} else if (usrInptOptn.at(0) == 'q') {
cout << " Good bye." << endl;
break;
}
}
return 0;
}
void PrintInventory(vector<Item*> inventory) {
unsigned int i = 0;
if (inventory.size() == 0) {
cout << "No items to print." << endl;
} else {
for (i=0; i<inventory.size(); ++i) {
cout << i << " - ";
inventory.at(i)->Print();
}
}
return;
}
vector<Item*> AddItemToInventory(vector<Item*> inventory) {
Produce* prdc;
string usrInptName = "";
string usrInptQntyStr = "";
istringstream inSS;
int usrInptQnty = 0;
string usrInptExpr = "";
cout << "Enter name of new produce: ";
getline(cin, usrInptName);
cout << "Enter quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
cout << "Enter expiration date: ";
getline(cin, usrInptExpr);
prdc = new Produce;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
inventory.push_back(prdc);
return inventory;
}
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory) {
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
istringstream inSS;
string usrInptQntyStr = "";
int usrInptQnty = 0;
if (inventory.size() == 0) {
cout << "No items to update." << endl;
} else {
PrintInventory(inventory);
do {
cout << "Update which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < inventory.size()) );
cout << "Enter new quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
inventory.at(usrIndexChoice)->SetQuantity(usrInptQnty);
}
return inventory;
}
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory) {
istringstream inSS;
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
string usrInptQntyStr = "";
if (inventory.size() == 0) {
cout << "No items to remove." << endl;
} else {
PrintInventory(inventory);
do {
cout << "Remove which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < inventory.size()) );
inventory.erase( inventory.begin() + usrIndexChoice );
}
return inventory;
}
Explanation / Answer
I have made changes in the starter program according to 1 and 2.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Item {
public:
void SetName(string nm)
{ name = nm; };
void SetQuantity(int qnty)
{ quantity = qnty; };
virtual void Print()
{ cout << name << " " << quantity << endl; };
virtual ~Item()
{ return; };
void SetPrice(int prcInDllrs)
{ priceInDollars = prcInDllrs; }
protected:
string name;
int quantity;
int priceInDollars;
};
class Produce : public Item { // Derived from Item class
public:
void SetExpiration(string expir)
{ expiration = expir; };
void Print()
{ cout << name << " x" << quantity << " for $" << priceInDollars
<< " (Expires: " << expiration << ")"
<< endl; //Grape bundle x20 for $3 (Expires: May 22)
};
private:
string expiration;
};
//class Book that derives from Item
class Book : public Item {
private:
string author;
public:
void SetAuthor(string authr)
{ author = authr; };
void Print()
{ cout << name << " x" << quantity << " for $" << priceInDollars
<< " (Author: " << author << ")"
<< endl; //To Kill a Mockingbird x22 for $5 (Author: Harper Lee)
};
};
// Print all items in the inventory
void PrintInventory(vector<Item*> inventory);
// Dialogue to create a new item, then add that item to the inventory
vector<Item*> AddItemToInventory(vector<Item*> inventory);
// Dialogue to update the quantity of an item, then update that item in the inventory
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory);
// Dialogue to remove a specific item, then remove that specific item from the inventory
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory);
int main() {
vector<Item*> inventory;
string usrInptOptn = "default";
while (true) {
// Get user choice
cout << " Enter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
getline(cin, usrInptOptn);
// Process user choice
if (usrInptOptn.size() == 0) {
continue;
} else if (usrInptOptn.at(0) == 'p') {
PrintInventory(inventory);
} else if (usrInptOptn.at(0) == 'a') {
inventory = AddItemToInventory(inventory);
} else if (usrInptOptn.at(0) == 'u') {
inventory = UpdateItemQtyInInventory(inventory);
} else if (usrInptOptn.at(0) == 'r') {
inventory = RemoveItemFromInventory(inventory);
} else if (usrInptOptn.at(0) == 'q') {
cout << " Good bye." << endl;
break;
}
}
return 0;
}
void PrintInventory(vector<Item*> inventory) {
unsigned int i = 0;
if (inventory.size() == 0) {
cout << "No items to print." << endl;
} else {
for (i=0; i<inventory.size(); ++i) {
cout << i << " - ";
inventory.at(i)->Print();
}
}
return;
}
vector<Item*> AddItemToInventory(vector<Item*> inventory) {
//Modify the AddItemToInventory to prompt the user to “Enter the price per item: $”, then set the Produce’s price to the userentered value.
/* string name;
int quantity;
int priceInDollars;
string expiration;
*/
Produce* prdc;
string usrInptName = "";
string usrInptQntyStr = "";
string usrInptPrcInDllrsStr = "";
istringstream inSS;
int usrInptQnty = 0;
int usrInptPrcInDllrs = 0;
string usrInptExpr = "";
cout << "Enter name of new produce: ";
getline(cin, usrInptName);
cout << "Enter quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
cout << "Enter expiration date: ";
getline(cin, usrInptExpr);
cout << "Enter the price per item: $";
getline(cin, usrInptPrcInDllrsStr);
inSS.str(usrInptPrcInDllrsStr);
inSS >> usrInptPrcInDllrs;
inSS.clear();
prdc = new Produce;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
prdc->SetPrice(usrInptPrcInDllrs);
inventory.push_back(prdc);
return inventory;
}
vector<Item*> UpdateItemQtyInInventory(vector<Item*> inventory) {
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
istringstream inSS;
string usrInptQntyStr = "";
int usrInptQnty = 0;
if (inventory.size() == 0) {
cout << "No items to update." << endl;
} else {
PrintInventory(inventory);
do {
cout << "Update which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < inventory.size()) );
cout << "Enter new quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
inventory.at(usrIndexChoice)->SetQuantity(usrInptQnty);
}
return inventory;
}
vector<Item*> RemoveItemFromInventory(vector<Item*> inventory) {
istringstream inSS;
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
string usrInptQntyStr = "";
if (inventory.size() == 0) {
cout << "No items to remove." << endl;
} else {
PrintInventory(inventory);
do {
cout << "Remove which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < inventory.size()) );
inventory.erase( inventory.begin() + usrIndexChoice );
}
return inventory;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.