Write a program to handle the flow of widgets into and out of a warehouse. The warehouse
will have numerous deliveries of new widgets and orders for widgets. The widgets
in a filled order are billed at a profit of 50 percent over their cost. Each delivery of new
widgets may have a different cost associated with it. The accountants for the firm have
instituted a last-in, first-out system for filling orders. This means that the newest widgets
are the first ones sent out to fill an order. Also, the most recent orders are filled first.
This function of inventory can be represented using two stacks: orders-to-be-filled and
widgets-on-hand. When a delivery of new widgets is received, any unfilled orders (on the
orders-to-be-filled stack) are processed and filled. After all orders are filled, if there are
354 Chapter 5 Stacks
widgets remaining in the new delivery, a new element is pushed onto the widgets-onhand
stack. When an order for new widgets is received, one or more objects are popped
from the widgets-on-hand stack until the order has been filled. If the order is completely
filled and there are widgets left over in the last object popped, a modified object with
the quantity updated is pushed onto the widgets-on-hand stack. If the order is not completely
filled, the order is pushed onto the orders-to-be-filled stack with an updated
quantity of widgets to be sent out later. If an order is completely filled, it is not pushed
onto the stack.
Write a class with functions to process the shipments received and to process orders.
After an order is filled, display the quantity sent out and the total cost for all widgets in
the order. Also indicate whether there are any widgets remaining to be sent out at a later
time. After a delivery is processed, display information about each order that was filled
with this delivery and indicate how many widgets, if any, were stored in the object
pushed onto the widgets-on-hand stack.
template<class T>
class LinkedStack : public StackInterface < T >
{
private:
Node<T> * topPtr; // Pointer to first node in the chain
// this nodde contains the stack's top
public:
// constructor and destructor
LinkedStack();
LinkedStack(const LinkedStack<T> & aStack); // copy constructor
virtual ~LinkedStack(); // destructor
// stack operations
bool isEmpty() const;
bool push(const T & newItem);
bool pop();
T peek() const;
template<class T>
LinkedStack<T>::LinkedStack(const LinkedStack<T> & aStack)
{
// Point to nodes in original chain
Node<T> * origChainPtr = aStack->topPtr;
if (origChainPtr == nullptr)
topPtr = nullptr; // original bag is empty
else
{
// Copy first node
topPtr = new Node<T>();
topPtr->setItem(origChainPtr->getItem());
// Point ot first node in new chain
Node<T> * newChainPtr = topPtr;
// Copy remaining nodes
while (origChainPtr != nullptr)
{
// Advance original chain pointer
origChainPtr = origChainPtr->getNext();
// Get Next item from original chain
T nextItem = origChainPtr->getItem();
// create a new node containing the next item
Node<T> * newNodePtr = new Node<T>(nextItem);
// link new node to end of new chain
newChainPtr->setNext(newNodePtr);
// Advance pointer to new last node
newChainPtr = newChainPtr->getNext();
}// end while
newChainPtr = newChainPtr->setNext(nullptr); // flag end of chain
}// end if
}// end copy constructor
template<class T>
LinkedStack<T>::~LinkedStack()
{
// pop until stack is empty
while (!isEmpty())
pop();
}// end destructor
int RemainingQty; // Storing the remaining qty of the widgets.
int TotalQty; // Storing the total qty of the widgets.
double TotalPrice; // Storing the total price of the widgets.
int OrderNo; // Storing the value of number of order.
public:
Order(int q, int on); // Default Constructor.
Order(const Order & aOrder); // Copy Constructor.
~Order(); // Destructor.
bool IsSatisfied(); // Return true is this order has been satisfied.
void Fulfill(double c); // Full this order by decreasing the amount of widgets it still requires @param cost of the widget.
void setTotalPrice(); // Setting the TotalPrice.
void setRemainingQty(); // Setting the RemainingQty.
void setTotalQty(); // Setting the TotalQty.
void setOrderNo(); // Setting the OrderNo.
double getTotalPrice()const; // Getting the TotalPrice.
int getRemainingQty()const; // Getting the RemainingQty.
int getTotalQty()const; // Getting the RemainingQty.
int getOrderNo()const; // Getting the RemainingQty.
};// end class Order
#endif
#include "Order.h"
// Default Constructor.
Order::Order(int q, int on)
:TotalQty(q), TotalPrice(0), RemainingQty(q), OrderNo(on) {}
// Return true is this order has been satisfied.
bool Order::IsSatisfied()
{
return RemainingQty == 0;
}
// Full this order by decreasing the amount of widgets it still requires @param cost of the widget.
void Order::Fulfill(double c)
{
RemainingQty--;
TotalPrice += c;
}
int oct = 1;
while (1)
{
cout << "*******************************************\n"
<< "\t 1.Add New Widgets To Inventory\n"
<< "\t 2.Place Order\n"
<< "\t 3.Quit\n"
<< "\t\tENTER YOUR SELECTION: \n";
cout << "*******************************************" << endl;
cout << "Please Enter You Choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the number of widgets arrived: ";
cin >> num;
sv = num;
while (num > 0)
{
double c;
cout << "Enter cost of widget:" << i << " :";
cin >> c;
Widget neww(c);
Widgets_On_Hand.push(neww);
i++;
num--;
}
i = 1;
while (!Orders_To_Be_Filled.isEmpty())
{
// Store the address of top value into the object of the class Order.
Order &o = Orders_To_Be_Filled.top();
while (!Widgets_On_Hand.pop())
{
// Store the address of top value into the object of the class Order.
Widget &w = Widgets_On_Hand.top();
o.Fulfill(1.5*w.getCost());
Widgets_On_Hand.pop();
sv--;
if (o.IsSatisfied())
{
flag = true;
break;
}
if (Widgets_On_Hand.isEmpty())
pful = true;
}
if (flag)
{
cout << endl << "\t\t Order No. \t" << o.getOrderNo() << " Fulfilled By This Delivery\n";
cout << "\t\t Total Quantity of Order:" << o.getTotalQty() << endl;
cout << "\t\t Total Cost of Order:" << o.getTotalPrice() << endl;
Orders_To_Be_Filled.pop();
flag = false;
}
else
{
if (pful)
{
cout << endl << "\t\t Order No.\t" << o.getOrderNo() << "Partially Fulfilled By This Delivery\n";
cout << "\t\t Total Quantity of Order:" << o.getTotalQty() << endl;
cout << "\t\t Remaining Quantity of Order:" << o.getRemainingQty() << endl;
cout << "\t\t Total Cost of Order:" << o.getTotalPrice() << endl;
}
pful = false;
break;
}
}
if (!Widgets_On_Hand.isEmpty())
cout << "\n\n\t\t Total Number of widgets stored in Wharehouse:" << sv << endl;
break;
case 2:
cout << "\n\t\t Enter the Quantity:";
cin >> qty;
if (qty > 0)
{
Order o(qty, oct++);
while (!Widgets_On_Hand.isEmpty())
{
if (o.IsSatisfied())
{
flag = true;
break;
}