Question

In: Computer Science

C++ code Inventory Item Stack You are writing an Inventory program that will allow the user...

C++ code

Inventory Item Stack

You are writing an Inventory program that will allow the user to enter a part into the inventory, take a part from the inventory, or quit. You are provided with the InvItem class (InvItem.h) and a partial Driver.cpp. You will be creating a DynamicStack class that should implement a Stack data structure. The DynamicClass should be implemented as a template class to allow any data type be added/removed from the stack.

You will submit three files: InvItem.h, DynamicStack.h, and Driver.cpp.

----------------------------------------------------------------------------------------------------------------------

1- STACK Class (DynamicStack.h)

You will have a header file that contains a DynamicStack class specification and member function definitions. This should be a template class that can accept any data type. The stack should be implemented using a linked list.

The Stack class should have the following member functions:

  • Constructor
  • Push
  • Pop
  • isEmpty

2- Inventory ITem Class (InvItem.h)

This class is provided for you.

3- Driver (Driver.cpp)

Comments that begin with “LOOK!” are provided in the given Driver.cpp file that indicate what code needs to be added.

----------------------------------------------------------------------------------------------------------------------

InvItem.h file


#ifndef INVITEM_H
#define INVITEM_H
#include <string>
using namespace std;

class InventoryItem
{
private:
   long serialNum;           // Serial number
   string manufactDate;   // Manufacture date
   int lotNum;               // Lot number

public:
   // Default constructor
   InventoryItem()
       { serialNum = 0; manufactDate = ""; lotNum = 0; }

   // Constructor
   InventoryItem(long s, string m, int lot)
       { serialNum = s; manufactDate = m; lotNum = lot; }

   void setSerialNum(long s)
       { serialNum = s; }

   void setManufactDate(string m)
       { manufactDate = m; }

   void setLotNum(int lot)
       { lotNum = lot; }

   long getSerialNum() const
       { return serialNum; }

   string getManufactDate()
       { return manufactDate; }

   int getLotNum() const
       { return lotNum; }
};

#endif

​​​​​​​----------------------------------------------------------------------------------------------------------------------

Driver.cpp.

//LOOK!! ---------------------put your comment block here with title, author, date, & purpose


#include <iostream>
#include "InvItem.h"
#include "DynStack.h"
using namespace std;

int main()
{
   //LOOK!! -------------------create your InventoryItem stack on the line below and name it "stack"
  
   //LOOK!! ---------------------create an inventory item object and name it "item"
  

   int choice;                       // Menu choice
   long serial;                   // Serial number
   string mDate;                   // Manufacture date

   do
   {
       // Display the menu.
       cout << "\n------ Inventory Menu --------\n\n";
       cout << "1. Enter a part into the inventory.\n";
       cout << "2. Take a part from the inventory.\n";
       cout << "3. Quit.\n\n";
       cout << "Please make a choice (1, 2, or 3): ";
       cin >> choice;

       // Validate choice
       while (choice < 1 || choice > 3)
       {
           cout << "Please enter 1, 2, or 3: ";
           cin >> choice;
       }

       // Act on the user's choice.
       switch(choice)
       {

       case 1:
           // Enter a part into inventory.
           cout << "\nYou have chosen to add an item to the inventory bin.\n\n";
           cout << "Enter the item's serial number: ";
           cin >> serial;
           item.setSerialNum(serial);
           cout << "Enter the item's manufacture date: ";
           cin >> mDate;
           item.setManufactDate(mDate);
           stack.push(item);
           break;

       case 2:
           // Take a part out of inventory.
           cout << "\nYou have chosen to remove an item from the inventory bin.\n\n";
           if (stack.isEmpty())
               cout << "No parts to remove.\n";
           else
           {
               stack.pop(item);
               cout << "\nThe part you removed was:\n";
               cout << "\tSerial number: " << item.getSerialNum() << endl;
               cout << "\tManufacture date: " << item.getManufactDate() << endl;
               cout << endl;
           }
           break;

       case 3:
           cout << "\nGoodbye!\n\n";
           break;
       }
   } while (choice != 3);

   return 0;
}

​​​​​​​----------------------------------------------------------------------------------------------------------------------

sample output

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 1

You have chosen to add an item to the inventory bin.

Enter the item's serial number: 3561565

Enter the item's manufacture date: 04/02/1996

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 1

You have chosen to add an item to the inventory bin.

Enter the item's serial number: 488963

Enter the item's manufacture date: 10/31/2019

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 1

You have chosen to add an item to the inventory bin.

Enter the item's serial number: 6866781

Enter the item's manufacture date: 11/18/2019

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 2

You have chosen to remove an item from the inventory bin.

The part you removed was:

        Serial number: 6866781

        Manufacture date: 11/18/2019

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 2

You have chosen to remove an item from the inventory bin.

The part you removed was:

        Serial number: 488963

        Manufacture date: 10/31/2019

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 2

You have chosen to remove an item from the inventory bin.

The part you removed was:

        Serial number: 3561565

        Manufacture date: 04/02/1996

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 2

You have chosen to remove an item from the inventory bin.

No parts to remove.

------ Inventory Menu --------

1. Enter a part into the inventory.

2. Take a part from the inventory.

3. Quit.

Please make a choice (1, 2, or 3): 3

Goodbye!

Solutions

Expert Solution

Our first task is to write code for DynStack.h file. In this file, we need to implement the DynamicStack class specification and member function definitions.

There are 2 things that we need to do in this file:

1. Creating a template class for the Node of the stack(implemented as linked list)

2. Creating a template class for the stack which internally uses a linked list.

Please note that we are using the name of this file to be DynStack.h as in the driver program the following header is added:

#include "DynStack.h"

Please find all the explanation to the code within the code itself as comments.

We have added the code snippets of different parts of the program for clarity. Please find the full code at the end.

Let's see how push and pop works:

PUSHING INTO THE STACK:

Let say the initial configuration of the stack(implemented as a linked list) looks like this:

Then, we will create a new Node to add at the top of the stack.

Connecting the new Node to the existing list:

Our topOfStack will now point to the newNode.

So, we have successfully pushed a new node to our stack.

POPPING OUT FROM THE STACK:

Let say the initial configuration of the stack(implemented as a linked list) looks like this:

In order for us to delete the memory taken by the top node, we need to store it somewhere and then manipulate our topOfStack pointer. Therefore, we create a pointer which points to the topOfStack:

Now, we will update our topOfStack pointer, so that it points to the correct Node. And delete (shown by lines in the image) the memory taken by the previous top(which is pointed by toDelete).

Here is the full code for DynStack.h file:

# ifndef DYNSTACK_H
# define DYNSTACK_H
# include <string> 

using namespace std;

// a template class Node for implementing our linked list
template <typename T>
class Node {
        public:
                // this will hold our item's value
                T data;

                // this will point to the next item in the stack
                Node * next;

                // a simple constructor for our linked list Node
                Node (T data) {
                        this -> data = data;
                }
};

// the template class DynamicStack
template <typename T>
class DynamicStack {

        private: 

                // a Node pointer which always points to the top of the linked list
                Node<T> * topOfStack;

        public:

                // constructor for our class
                DynamicStack () {
                        // as the linked list is initially empty
                        // we will set the topOfStack to NULL
                        topOfStack = NULL;
                }

                // this function returns a boolean
                // to give information about stack emptiness
                bool isEmpty () {
                        // if topOfStack is found to be NULL
                        // our stack is empty
                        if (topOfStack == NULL) {
                                return true;
                        }

                        // otherwise, not empty
                        return false;
                }

                // Please refer to the typed explanation under the heading -
                // "PUSHING INTO THE STACK" for better understanding
                // the push function to "Enter a part into the inventory"
                void push (T item) {

                        // if our linked list is empty
                        // that means, our topOfStack is NULL,
                        // therefore, this must be our first node in the linked list
                        if (isEmpty ()) {
                                topOfStack = new Node<T> (item);
                        }

                        // otherwise
                        else {
                                // we will create a new Node
                                Node<T> * newNode = new Node<T> (item);

                                // we then, point our newNode to point to the topOfStack
                                newNode -> next = topOfStack;

                                // We then, update our topOfStack to point to the newNode
                                // (topOfStack) always points to the last added Node
                                topOfStack = newNode;
                        }
                }

                // the pop function to "Take a part from the inventory"
                // Please refer to the typed explanation under the heading -
                // "POPPING INTO THE STACK" for better understanding

                // in the driver program. When item is passed to the pop function
                // the top element our stack is assigned to the variable "item"
                // that is why it is passed as a reference, so that the caller can access it
                // then, we can delete that element and return
                void pop (T &item) {

                        // set the topOfStack to the item variable we got as a reference
                        item = topOfStack -> data;

                        // save the current topOfStack pointer (so that we can delete it later)
                        Node <T> * toDelete = topOfStack;

                        // update topOfStack to point to the next item in stack
                        topOfStack = topOfStack -> next;

                        // delete the intended item;
                        delete toDelete;
                }
};

#endif

Let's now see how to complete the driver program given.

We need to look for the "LOOK!" comments. (For convenience in finding the code added in the driver program, we are not removing the "LOOK!" comments)

1. We need to add the comment up at the top which can be like this:

/*
        Title: driver.cpp
        Author: "Please put your name here"
        Date: "Please put the date here"
        Purpose: This program serves as a driver program to check
                         the working of DynamicStack class. 
*/

Please fill the author, date and the title of your choice.

2. We need to initialize an InventoryItem stack which can be done like this:

DynamicStack <InventoryItem> stack;

3. We need to initialize an InventoryItem object:

InventoryItem item;

Therefore, the full code can be written as(Please note that "LOOK" comments have NOT been removed from the code for convenience in finding the added code)

//LOOK!! ---------------------put your comment block here with title, author, date, & purpose
/*
        Title: driver.cpp
        Author: "Please put your name here"
        Date: "Please put the date here"
        Purpose: This program serves as a driver program to check
                         the working of DynamicStack class. 
*/

#include <iostream>
#include "InvItem.h"
#include "DynStack.h"
using namespace std;

int main()
{
   //LOOK!! -------------------create your InventoryItem stack on the line below and name it "stack"
        // creating an InventoryItem stack
        DynamicStack <InventoryItem> stack;
  
   //LOOK!! ---------------------create an inventory item object and name it "item"
        // creating an InventoryItem object
        InventoryItem item;

   int choice;                       // Menu choice
   long serial;                   // Serial number
   string mDate;                   // Manufacture date

   do
   {
       // Display the menu.
       cout << "\n------ Inventory Menu --------\n\n";
       cout << "1. Enter a part into the inventory.\n";
       cout << "2. Take a part from the inventory.\n";
       cout << "3. Quit.\n\n";
       cout << "Please make a choice (1, 2, or 3): ";
       cin >> choice;

       // Validate choice
       while (choice < 1 || choice > 3)
       {
           cout << "Please enter 1, 2, or 3: ";
           cin >> choice;
       }

       // Act on the user's choice.
       switch(choice)
       {

       case 1:
           // Enter a part into inventory.
           cout << "\nYou have chosen to add an item to the inventory bin.\n\n";
           cout << "Enter the item's serial number: ";
           cin >> serial;
           item.setSerialNum(serial);
           cout << "Enter the item's manufacture date: ";
           cin >> mDate;
           item.setManufactDate(mDate);
           stack.push(item);
           break;

       case 2:
           // Take a part out of inventory.
           cout << "\nYou have chosen to remove an item from the inventory bin.\n\n";
           if (stack.isEmpty())
               cout << "No parts to remove.\n";
           else
           {
               stack.pop(item);
               cout << "\nThe part you removed was:\n";
               cout << "\tSerial number: " << item.getSerialNum() << endl;
               cout << "\tManufacture date: " << item.getManufactDate() << endl;
               cout << endl;
           }
           break;

       case 3:
           cout << "\nGoodbye!\n\n";
           break;
       }
   } while (choice != 3);

   return 0;
}

Please refer to the screenshots of the code for understanding the indentation.

DynStack.h

driver.cpp

Let's see the output of the code for the input given in the problem statement.


Related Solutions

***USING JAVA Scenario: You will be writing a program that will allow a user to find...
***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word. Requirements: Do not use Arrays for this assignment. Do not use any String class methods (.phrase(), replace methods, regex methods)...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in...
C++ Code Writing prompt: Grade Calculation: Write a program that asks the user to enter in a number greater than or equal to zero and less than or equal to 100. If they do not you should alert them and end the program. Next, determine the letter grade associated with the number. For example, A is any grade between 90 and 100. Report the letter grade to the user.
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
n this assignment, you will need to code a Web page that will allow a user...
n this assignment, you will need to code a Web page that will allow a user to enter their first and last names and indicate how many pets they have, if any. If they have pets, the user will be able to list up to three of their names. You will code validations on some of the fields and display error messages on the page when a field does not pass the validation test. If all the data entered is...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments....
Write a C++ program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to do the following: 1....
C++ only Large Program Create a magical creature (or monster) zoo inventory program that will allow...
C++ only Large Program Create a magical creature (or monster) zoo inventory program that will allow a zookeeper to add magical creatures (either manually or from a file), delete a creature, and display all creatures in the zoo. The zoo creatures will be organized in a linked list. ---------------------------------------------------------------------------- Zoo.cpp – this file will contain your main function that will allow the user to enter, delete, and print creatures. The main function will create the linked list of creatures. Creature.h...
You are writing a billing program that adds up the items a user enters. Your program...
You are writing a billing program that adds up the items a user enters. Your program should use a loop and prompt the user for the item number for each item in inventory to add to the bill. (You can ask the user for the number of items you will enter and use a counter controlled loop or a sentinel controlled loop that has a stop number. Either way is fine.) Here is the table of items in inventory: Item...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT