I want C++ program with output. please don\'t post that solution that are alread
ID: 3806260 • Letter: I
Question
I want C++ program with output. please don't post that solution that are already in Chegg.
Assignment 1
Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that shows that the stack works for at least two different data types.
Assignment 2
In this program you will use the stack class you created in Assignment 1. First you will create a class called InventoryItem. This class will have its class declaration in InventoryItem.h and its implementation in InventoryItem.cpp. It will have three private data members, an integer serialNum which holds the part’s serial number, manufactDate which should be a string that holds the date the item was manufactured, then lotNum which will be an integer that holds the part’s lot number. The program should then create a stack with a data type of InventoryItem (stack). The program should loop asking the user to enter in new items to the inventory stack or to remove an item from the inventory stack. The loop should continue until the user indicates they are done. This should be menu driven. When adding an item, the program should ask the user for the information it needs for the 3 data members of the InventoryItem class and add a new item to the stack. When removing an item from the stack, the program should display all of the information in the InventoryItem object that was popped off the stack. When the program ends, it should pop all of the remaining items off the stack and display the data that is in the Inventory items as it pops them off. There should be 3 utility functions that main uses.
void popItem(DynStack* stack) // pops the item off the stack and displays it.
void pushItem(DynStack* stack) // pushes the item onto the stack
int menu(); // displays the menu and returns the user’s choice.
Explanation / Answer
/************ InventoryItem.h ***********/
/* This file contains the decleration of InventoryItem class */
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <string>
using namespace std;
class InventoryItem
{
private:
int serialNum;
string manufactDate;
int lotNum;
public:
// Default Constructor
InventoryItem();
// Parameterized Constructor - serialNum, manufactDate, lotNum
InventoryItem(int, string, int);
// Methods to read the data from user
int get_serialNum() const;
string get_ManDate() const;
int get_LotNum() const;
// Methods to set the data
void set_serialNum(int);
void set_ManDate(string);
void set_LotNum(int);
};
#endif
/************ InventoryItem.cpp ***********/
/* This file contains the Implementation of InventoryItem class */
#include "InventoryItem.h"
// Default constructor
InventoryItem::InventoryItem()
{
}
// Parameterized Constructor - serialNum, manufactDate, lotNum
InventoryItem::InventoryItem(int Snum, string ManD, int Lnum)
{
serialNum = Snum;
manufactDate = ManD;
lotNum = Lnum;
}
// get functions or Accessors
int InventoryItem::getLotNum() const
{
return lotNum;
}
int InventoryItem::getSerNum() const
{
return serialNum;
}
string InventoryItem::getManDate() const
{
return manufactDate;
}
// set functions or Mutators
void InventoryItem::setLotNum(int lNum)
{
lotNum = lNum;
}
void InventoryItem::setSerNum(int sNum)
{
serialNum = sNum;
}
void InventoryItem::setManDate(string manu)
{
manufactDate = manu;
}
/************ DynStack.h ***********/
/* This file contains the decleration of DynStack class */
// Declaration file for the DynStack class. This class is a template version of
// a dynamic stack class.
#ifndef DYNSTACK_H
#define DYNSTACK_H
template <class T>
class DynStack
{
private:
// Struct for stack nodes
struct StackNode{
T value; // holds the node's value
StackNode *next; // ptr to next node
};
StackNode *top; // ptr to stack top
public:
// Constructor
DynStack();
// Destructor
~DynStack();
// Stack operations
void push(T);
bool pop(T &);
bool isEmpty();
void displayStack() const;
};
#endif
/************ DynStack.cpp ***********/
// This file contains Implementation for the DynStack class. This class is a template version of
// a dynamic stack class.
#include "DynStack.h"
// Default constructor
template <class T>
DynStack<T>::DynStack()
{
top = nullptr;
}
// Destructor - deletes the stack node by node
template <class T>
DynStack<T>::~DynStack()
{
StackNode *nodePtr;
StackNode *nextNode;
// aim nodePtr at top of stack
nodePtr = top;
// travel list and delete each node
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
///////////////////////
// Stack operations //
/////////////////////
// push() adds the argument onto the stack
template <class T>
void DynStack<T>::push(T item)
{
StackNode *newNode = nullptr;
// Creates a new node and stores argument there
newNode = new StackNode;
newNode->value = item;
// If the list is empty, make newNode the first node
if (isEmpty() == true)
{
top = newNode;
newNode->next = nullptr;
}
else
{
newNode->next = top;
top = newNode;
}
}
// If the stack is empty, then pop() simply returns false.
// If a node exists, pop() returns the top item, deletes it from the stack,
// and then returns true.
template <class T>
bool DynStack<T>::pop(T &item)
{
StackNode *temp = nullptr;
bool status;
// Check that the stack isn't empty
if (isEmpty() == true)
{
status = false;
}
else // Pop value off top of stack
{
item = top->value;
temp = top->next;
delete top;
top = temp;
status = true;
}
return status;
}
// isEmpty() returns true if stack is empty; otherwise it returns false.
template <class T>
bool DynStack<T>::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
// displayStack() simply prints each item in a given stack.
template <class T>
void DynStack<T>::displayStack() const
{
StackNode *nodePtr;
nodePtr = top;
if (nodePtr == nullptr)
cout << "Stack is empty.";
else
{
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
}
/*********** InventoryItemMain.cpp ***********/
/*
This is a further demonstration of the DynStack dynamic stack template class,
combined with an abstract data type.
A menu is displayed, offering the user the choice to add or remove nodes, or
exit the program. Adding items demonstrates an application of the
DynStack::push() member function, combined with an interface so the user may
populate the node with the lot and serial numbers, as well as manufacturing
date.
Removing items demonstrates an application of the DynStack::pop() member
function. If a node is found, its data is transferred to a temporary
InventoryItem object, then printed. If no nodes are found, it returns to
the menu.
Upon exit, any remaining items are popped off the stack and have their
contents displayed to the user. If no items remain, a single exit message
prints and the program ends.
*/
#include "DynStack.h"
#include "InventoryItem.h"
#include <iostream>
using namespace std;
void popItem(DynStack<InventoryItem>*); // pops the item off the stack and displays it.
//calls pop() from DynStack within, snags the item info, and prints it
void pushItem(DynStack<InventoryItem>*); // pushes the item onto the stack
//calls push() from DynStack within, smushes item onto stack
int menu(); // displays the menu and returns the user’s choice.
int main()
{
DynStack<InventoryItem>* invStack;
invStack = new DynStack<InventoryItem>;
// Holds the menu selection from menu()
int menuChoice = 0;
// Variable for menu loop
bool valid = true;
// Values for switch statement
const int ADDITEM = 1;
const int REMITEM = 2;
const int EXIT = 3;
// Used when printing out contents upon program exit
InventoryItem temp;
while (valid)
{
menuChoice = menu();
switch (menuChoice)
{
case ADDITEM: // to add items to the stack
pushItem(invStack);
valid = true;
break;
case REMITEM: // to remove items from the stack and display their contents
popItem(invStack);
valid = true;
break;
case EXIT:
// On exit, any inventory must be popped off the stack and
// printed, in sequence. Otherwise the program simply ends.
if (invStack->isEmpty() != true)
{
cout << "Here are the remaining items in inventory: ";
while (invStack->pop(temp) == true) // Loop runs until stack is empty
{
cout << "Lot Number: " << temp.getLotNum() << " ";
cout << "Serial Number: " << temp.getSerNum() << " ";
cout << "Manufacturing Date: " << temp.getManDate() << " ";
}
// Display msg once all nodes are popped off
if (invStack->isEmpty() == true)
cout << "No further inventory. ";
}
else
cout << "No inventory to display; exiting program. ";
return 0;
}
}
return 0;
}
// popItem() is used to remove items from the stack and display the contents
// to the user. If the stack has an item, it will use DynStack's pop() function
// to populate the InventoryItem temp obj's data members, then print that data.
// If the stack is empty, it returns an error and goes back to the menu screen.
void popItem(DynStack<InventoryItem>* stack)
{
InventoryItem temp;
char repeat = 'y'; // to control our loop
// While loop repeats until user is done removing items.
while (repeat == 'y' || repeat == 'Y')
{
if (stack->pop(temp) == true)
{
cout << " ------------------------- ";
cout << "| Remove Item | ";
cout << "------------------------- ";
cout << "Lot Number: " << temp.getLotNum() << " ";
cout << "Serial Number: " << temp.getSerNum() << " ";
cout << "Manufacturing Date: " << temp.getManDate() << " ";
cout << "Remove and display another item y/n? ";
cin >> repeat;
}
else // If stack is empty, display error message
{
cout << "No items in inventory. ";
repeat = 'n'; // Set repeat to n to return to main menu
}
}
}
// pushItem() asks the user for input for the lot number, serial number and
// manufacturing date for each item. It then writes those to the temp
// InventoryItem object, which is used with DynStack's push() function to
// add the new item to the inventory.
void pushItem(DynStack<InventoryItem>* stack) // pushes the item onto the stack
{
InventoryItem temp;
int lNum, sNum;
string manDate;
char repeat = 'y'; // to control our loop
while (repeat == 'y' || repeat == 'Y')
{
cout << " ------------------------- ";
cout << "| Add Item | ";
cout << "------------------------- ";
cout << "Enter lot number: ";
// Verify input so it is a non-zero number on lot num and serial num
cin >> lNum;
while (lNum < 0)
{
cout << "Lot number must be a non-negative number. ";
cout << "Enter lot number: ";
cin >> lNum;
}
cout << "Enter serial number: ";
cin >> sNum;
while (sNum < 0)
{
cout << "Serial number must be a non-negative number. ";
cout << "Enter serial number: ";
cin >> sNum;
}
cout << "Enter manufacturing date in MM/DD/YYYY format: ";
cin >> manDate;
temp.setLotNum(lNum);
temp.setSerNum(sNum);
temp.setManDate(manDate);
stack->push(temp);
cout << "Add another item to inventory y/n? ";
cin >> repeat;
}
}
int menu() // displays the menu and returns the user’s choice.
{
int choice;
cout << "------------------------- ";
cout << "| Inventory item Menu | ";
cout << "------------------------- ";
cout << "1) Add item to the Inventory ";
cout << "2) Remove item from Inventory ";
cout << "3) Exit program ";
cout << "Selection number: ";
cin >> choice;
// Check that it's a valid choice
while (choice <= 0 || choice > 3)
{
cout << "Invalid choice. Pick 1, 2, or 3: ";
cin >> choice;
}
return choice;
}
/**********************END***********************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.