C++ Homework

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.
Show us what you have so far.
/** ADT stack: Linked-Based Implementation.
@file LinkedStack.h */

#ifndef _LINKED_STACK
#define _LINKED_STACK
#include "StackInterface.h"
#include "Node.h"
#include <cassert>

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;

void remove(int);

};// end LinkedStack
#endif // !_LINKED_STACK

template<class T>
LinkedStack<T>::LinkedStack() : topPtr(nullptr){}

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

template<class T>
bool LinkedStack<T>::isEmpty() const
{
return topPtr == nullptr;
}// end isEmpty

template<class T>
bool LinkedStack<T>::push(const T & newItem)
{
Node<T>* newNodePtr = new Node<T>(newItem, topPtr);
topPtr = newNodePtr;
newNodePtr = nullptr;

return true;
}// end push

template<class T>
bool LinkedStack<T>::pop()
{
bool result = false;
if (!isEmpty())
{
// stack is not empty; delete top
Node<T> * nodeToDeletePtr = topPtr;
topPtr = topPtr->getNext();

// return deleted node to system
nodeToDeletePtr->setNext(nullptr);
delete nodeToDeletePtr;
nodeToDeletePtr = nullptr;

result = true;
}// end if

return result;
}

template<class T>
T LinkedStack<T>::peek() const
{
assert(!isEmpty()); // enforce precondition

// stack is not empty; return top
return topPtr->getItem();
}// end peek()

template<class T>
void LinkedStack<T>::remove(int n)
{
int counter = 0;
while (!isEmpty() && (counter < n))
{
pop();
counter++;
} // end while
} // end remove
What part do you need help with? Do you see any errors? Help us to help you, because we are helpers. Not homework doers.
#ifndef _WIDGET_H
#define _WIDGET_H
#include <iostream>
#include <cstddef>

using namespace std;


class Widget
{
private:

double cost; // A widget has a cost.


public:

Widget(double c); // Default Constructor.
Widget(const Widget & aWidget); // Copy Constructor.
~Widget(); // Destructor.

void setCost(); // Setting the cost of the widget.
double getCost()const; // Getting the cost of the widget.


};// end class Widget

#endif

#include "Widget.h"


// Default Constructor.
Widget::Widget(double c)
: cost(c){}


// Copy Constructor.
Widget::Widget(const Widget & aWidget)
: cost(aWidget.cost){}


// Destructor
Widget::~Widget(){}


// Set the cost of the widget.
void Widget::setCost()
{
if (cost < 0.00)
cerr << "ERROR: Invalid cost.\n";
else
this->cost = cost;
}


// Get the cost of the widget.
double Widget::getCost()const
{
return this->cost;
}





#ifndef _Order_H
#define _Order_H
#include <iostream>
#include <cstddef>
#include "Widget.h"

using namespace std;


class Order
{
private:

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) {}


// Copy Constructor.
Order::Order(const Order & aOrder)
: TotalQty(aOrder.TotalQty), TotalPrice(aOrder.TotalPrice), RemainingQty(aOrder.RemainingQty), OrderNo(aOrder.OrderNo) {}


// Destructor
Order::~Order(){}


// 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;
}


// Setting the TotalPrice.
void Order::setTotalPrice()
{
this->TotalPrice = TotalPrice;
}


// Setting the RemainingQty.
void Order::setRemainingQty()
{
this->RemainingQty = RemainingQty;
}


// Setting the TotalQty.
void Order::setTotalQty()
{
this->TotalQty = TotalQty;
}


// Setting the OrderNo.
void Order::setOrderNo()
{
this->OrderNo = OrderNo;
}


// Return the price of this order Acumulated over time.
double Order::getTotalPrice()const
{
return this->TotalPrice;
}



// Return the Remaining Qty of widgets this order still requires.
int Order::getRemainingQty()const
{
return this->RemainingQty;
}



// Return the total qty of the order.
int Order::getTotalQty()const
{
return this->TotalQty;
}


// Return Order No.
int Order::getOrderNo()const
{
return this->OrderNo;
}




//MAIN

#include <iostream>
#include "LinkedStack.h"
#include "Widget.h"
#include "Order.h"

using namespace std;

int main()
{

LinkedStack <Widget> Widgets_On_Hand;
LinkedStack <Order> Orders_To_Be_Filled;

int choice, num, qty, sv, i = 1;

bool flag = false, pful = false;

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;
}

Widget &w = Widgets_On_Hand.front();
o.Fulfill(1.5*w.getCost());
Widgets_On_Hand.pop();
}

if (flag)
{
cout << endl << "\t\t Order No.\t" << o.getOrderNo() << " Fulfilled \n";
cout << "\t\t Total Quantity of Order:" << o.getTotalQty() << endl;
cout << "\t\t Total Cost of Order:" << o.getTotalPrice() << endl;

flag = false;
}

else
{
cout << endl << "\t\t Order No.\t" << o.getOrderNo() << "Is Not Fulfilled\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;

Orders_To_Be_Filled.push(o);
}
}

break;

case 3:
exit(1);
default:
cout << endl << "Wrong Choice\n";
}
}

system("pause");
return 0;

}

The problems are:

Order &o = Orders_To_Be_Filled.top();
Widget &w = Widgets_On_Hand.top();
Widget &w = Widgets_On_Hand.front();
What errors are you getting?
Please be more specific.
Topic archived. No new replies allowed.