In: Computer Science
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:
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!
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.