Question

In this assignment, you’ll make an inventory system for a store’s items, including produce and books....

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 user­entered 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

Starter code for 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 << "\nEnter (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 << "\nGood 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;

}

Homework Answers

Answer #1

#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; };
       void SetPrice(int prcinDllrs)
           { priceInDollars = prcinDllrs; };
        virtual void Print()
            { cout << name << " " << quantity << endl; };
        virtual ~Item()
            { return; };
       int GetTotalValueAsPrice()
           { return (quantity * priceInDollars); };
    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;
            };
    private:
        string expiration;
};

class Book : public Item {
   public:
       void SetAuthor(string authr)
           { author = authr; };
       void Print()
       {
           cout << name << " x" << quantity
               << " for " << priceInDollars
               << " (Author: " << author << ")"
               << endl;
       };
   private:
       string author;
};


class Inventory {
   public:
       void PrintInventory();
       void AddItemToInventory();
       void UpdateItemQtyInInventory();
       void RemoveItemFromInventory();  
   private:
       void SumInv();
       vector<Item*> inventory;
       int totalInvPriceDollars;
};

   // Print all items in the inventory
   void Inventory::PrintInventory() {
       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();
           }
           cout << "Total inventory value: $" << this->totalInvPriceDollars << endl;
       }
       return;
   }

   // Dialogue to create a new item, then add that item to the inventory
   void Inventory::AddItemToInventory() {
       Produce* prdc;
       string usrInptName = "";
       string usrInptQntyStr = "";
       istringstream inSS;
       int usrInptQnty = 0;
       string usrInptExpr = "";
       int usrPrice = 0;
       string usrInpt = "";
       string usrAuthor = "";
       Book* book;

       // Get user choice
       cout << "\nAdd a book (b) or produce (p): ";
       getline(cin, usrInpt);

       if ((usrInpt.at(0) != 'b') && (usrInpt.at(0) != 'p')) {
           cout << "Invalid choice";
           return;
       }

       if (usrInpt.at(0) == 'p') {
           cout << "Enter name of new produce: ";
       }
       else {
           cout << "Enter name of new book: ";
       }

       getline(cin, usrInptName);

       cout << "Enter quantity: ";
       getline(cin, usrInptQntyStr);
       inSS.str(usrInptQntyStr);
       inSS >> usrInptQnty;
       inSS.clear();

       if (usrInpt.at(0) == 'p') {
           cout << "Enter expiration date: ";
           getline(cin, usrInptExpr);
       }
       else {
           cout << "Enter author: ";
           getline(cin, usrAuthor);
       }

       cout << "Enter the price per item: $";
       cin >> usrPrice;

       if (usrInpt.at(0) == 'p') {
           prdc = new Produce;
           prdc->SetName(usrInptName);
           prdc->SetQuantity(usrInptQnty);
           prdc->SetExpiration(usrInptExpr);
           prdc->SetPrice(usrPrice);

           this->inventory.push_back(prdc);
       }
       else {
           book = new Book;
           book->SetName(usrInptName);
           book->SetQuantity(usrInptQnty);
           book->SetAuthor(usrAuthor);
           book->SetPrice(usrPrice);

           this->inventory.push_back(book);
       }
       this->SumInv();
       return;
   }

   // Dialogue to update the quantity of an item, then update that item in the inventory
   void Inventory::UpdateItemQtyInInventory() {
       string usrIndexChoiceStr = "";
       unsigned int usrIndexChoice = 0;
       istringstream inSS;
       string usrInptQntyStr = "";
       int usrInptQnty = 0;

       if (inventory.size() == 0) {
           cout << "No items to update." << endl;
       }
       else {
           this->PrintInventory();

           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);
       }
       this->SumInv();
       return;
   }

   // Dialogue to remove a specific item, then remove that specific item from the inventory
   void Inventory::RemoveItemFromInventory() {
       istringstream inSS;
       string usrIndexChoiceStr = "";
       unsigned int usrIndexChoice = 0;
       string usrInptQntyStr = "";

       if (inventory.size() == 0) {
           cout << "No items to remove." << endl;
       }
       else {
           this->PrintInventory();

           do {
               cout << "Remove which item #: ";
               getline(cin, usrIndexChoiceStr);
               inSS.str(usrIndexChoiceStr);
               inSS >> usrIndexChoice;
               inSS.clear();
           } while (!(usrIndexChoice < inventory.size()));

           inventory.erase(inventory.begin() + usrIndexChoice);
       }
       this->SumInv();
       return;
   }

   // Compute the inventory's Total Price
   void Inventory::SumInv() {
       int total = 0;

       for (int i = 0; i < inventory.size(); ++i) {
           total += inventory.at(i)->GetTotalValueAsPrice();
       }
       this->totalInvPriceDollars = total;

       return;
   }


int main() {
    Inventory* inventory;
    string usrInptOptn = "default";

   inventory = new Inventory;
  
    while (true) {
        // Get user choice      
        cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";      
        getline(cin >> ws, usrInptOptn);

        // Process user choice
        if (usrInptOptn.size() == 0) {
            continue;
        } else if (usrInptOptn.at(0) == 'p') {
            inventory->PrintInventory();
        } else if (usrInptOptn.at(0) == 'a') {
            inventory->AddItemToInventory();
        } else if (usrInptOptn.at(0) == 'u') {
            inventory->UpdateItemQtyInInventory();
        } else if (usrInptOptn.at(0) == 'r') {
            inventory->RemoveItemFromInventory();
        } else if (usrInptOptn.at(0) == 'q') {
            cout << "\nGood bye." << endl;
            break;
        }
    }

    return 0;
}


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
C++ ONLY -- PRACTICE ASSIGNMENT For our practice assignment we have to turn in 2 files...
C++ ONLY -- PRACTICE ASSIGNMENT For our practice assignment we have to turn in 2 files - Driver.cpp and StringQueue.h Driver.cpp is provided for us, but if there are any changes needed to be made to that file please let me know. Based on the instructions below, what should the code look like for StringQueue.h ? Create a class named StringQueue in a file named StringQueue.h. Create a QueueNode structure as a private member of the class. The node should...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...