Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Help! (C++ Programming) In this assignment, you are required to write you

ID: 3720696 • Letter: P

Question

Please Help! (C++ Programming)

In this assignment, you are required to write your own version of a class template that will create a dynamic stack of any data type.

Then design an inventory class that stores the following members:

serialNum: An integer that holds a part’s serial number.

manufactDate: a member that holds the date the part was manufactured. (can use string as data type)

lotNum: An integer that holds the part’s lot number.

The class should have appropriate member functions for storing data into, and retrieving data from, these members. Use the stack class you defined above to hold objects of the Inventory class. Last, design a program that uses the stack class described above. The program should have a loop that asks the user if he or she wishes to add a part to inventory, or take a part from inventory. The loop should repeat until the user is finished (you might consider to display a Menu and ask the user to make a choice). If the user wishes to add a part to inventory, the program should ask for the serial number, date of manufacture, and lot number. The data should be stored in an inventory object, and pushed onto the stack.

If the user wishes to take a part from inventory, the program should pop the top-most part from the stack and display the contents of its member variables. When the user finishes the program, it should display the contents of the member values of all the objects that remain on the stack.

(C++ Programming)

Explanation / Answer

#include <iostream>

#define MAX_SIZE 10

using namespace std;

template <class T>

class Stack {

private:

T stackHolder[MAX_SIZE];

int top;

bool overflow;

bool underflow;

public:

Stack(){

top=-1;

overflow=false;

underflow=true;

}

~Stack(){

cout<<" In Destructor"<<endl;

}

bool isEmpty(){

if(top<0){

underflow=true;

}else{

underflow=false;

}

return underflow;

}

bool isFull(){

if(top>=(MAX_SIZE-1)){

overflow=true;

}else{

overflow=false;

}

return overflow;

}

void printStack(){

if(!isEmpty()){

for(int index=top;index>=0;index--){

if(index>0){

cout<<stackHolder[index]<<", ";

}else{

cout<<stackHolder[index];

}

}

cout<<endl;

}else{

cout<<"stack empty" <<endl;

}

}

void push(T data){

if(!isFull()){

top++;

stackHolder[top]=data;

}

}

T pop(){

T value;

if(!isEmpty()){

value=stackHolder[top];

top--;

}

return value;

}

T peek(){

T value;

if(!isEmpty()){

value=stackHolder[top];

}

return value;

}

};

class Inventory {

private:

int serialNum; // An integer that holds a part’s serial number.

string manufactDate; // a member that holds the date the part was manufactured. (can use string as data type)

int lotNum; // An integer that holds the part’s lot number.

  

public:

Inventory(int s, string date, int l){

serialNum = s;

manufactDate = date;

lotNum = l;

}

void setSerialNum(int s) {

serialNum = s;

}

  

void setManufactDate(string date) {

manufactDate = date;

}

  

void setLotNum(int l) {

lotNum = l;

}

  

int getSerialNum() {

return serialNum;

}

  

string getManufactDate() {

return manufactDate;

}

  

int getLotNum() {

return lotNum;

}

};

int main() {

Stack<Inventory> st;

int ch;

while(1) {

cout << "1. Push an item to inventory 2. Remove an item from inventory 3.Exit"<<endl;

cout << "Enter your choice : ";

cin >> ch;

  

switch(ch) {

case 1: {

int s, l;

string date;

cout << "Serial Number ? : ";

cin >> s;

cout << "Manufacture Date ? : ";

cin >> date;

cout << "Lot Number ? : ";

cin >> l;

Inventory in(s, date, l);

st.push(in);

break;

}

case 2: {

Inventory in = st.pop();

cout << "Removed item details : " <<endl;

cout << "Serial Number : " << in.getSerialNum() << endl;

cout << "Manufacture date: " << in.getManufactDate() << endl;

cout << "Lot Number : " << in.getLotNum() << endl;

break;

}

case 3: {

st.printStack();

return 0;

}

}

}

}