Inventory Bin Stack C++
Inventory Bin Stack
Design an inventory class that stores the following members:
serialNum:
m.anufactDate:
lotNum.:
An integer that holds a part’s serial number.
A member that holds the date the part was manufactured.
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.
Next, design a stack class that can hold objects of the class described above. If you
wish.
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.
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.
Answer:
// Inventory Bin Queue #include <iostream> #include "InvItem.h" #include "DynStack.h" using namespace std; int main() { DynStack<InventoryItem> stack; // create stack InventoryItem item; // create inventory item object 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 << "Goodbye!\n"; break; } } while (choice != 3); return 0; }
DynStack template
template <class T> class DynStack { private: struct StackNode { T value; StackNode *next; }; StackNode *top; public: DynStack() { top = NULL; } void push(T); void pop(T &); bool isEmpty(); }; //********************************************************* // Member function push pushes the argument onto * // the stack. * //********************************************************* template <class T> void DynStack<T>::push(T num) { StackNode *newNode; // Allocate a new node & store Num newNode = new StackNode; newNode->value = num; // If there are no nodes in the list // make newNode the first node if (isEmpty()) { top = newNode; newNode->next = NULL; } else // Otherwise, insert NewNode before top { newNode->next = top; top = newNode; } } //********************************************************* // Member function pop pops the value at the top * // of the stack off, and copies it into the variable * // passed as an argument. * //********************************************************* template <class T> void DynStack<T>::pop(T &num) { StackNode *temp; if (isEmpty()) { cout << "The stack is empty.\n"; return; } else // pop value off top of stack { num = top->value; temp = top->next; delete top; top = temp; } } //********************************************************* // Member funciton isEmpty returns true if the stack * // is empty, or false otherwise. * //********************************************************* template <class T> bool DynStack<T>::isEmpty() { if (!top) return true; else return false; }
Specification file for the InvItem class
#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
Leave a reply