This program should be written using C++ it is based on the World Wide Widget Wo
ID: 645066 • Letter: T
Question
This program should be written using C++ it is based on the World Wide Widget Works code listed below. The current program is an inventory management system for WWWW. Your job is to enhance the program with the following features:
1. Use inheritance, and create classes for WidgetA, WidgetB, and WidgetC. The Widget class will be the base class. Update the Widget class as needed.
2. Use the factory design pattern to create Widgets with one common interface
3. Instead of list of Widget objects for on_hand, you will have a list of Widget*. This list will be able to hold pointers to WidgetA, WidgetB, and WidgetC objects. The id numbers 0, 1, 2 should be matched to WidgetA, WidgetB, and WidgetC types respectively.
4. Write a function that is called at start up to populate the on_hand inventory with least 5 widgets of different types.
5. Create a menu system with the five options:
1) process an order,
2) receive a shipment,
3) show on hand,
4) show on order, and
5) exit.
This is the example program:
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
//each class will have its own heard file and cpp file
class Widget {
public:
// constructors
Widget () : id_number (0) { }
Widget (int a) : id_number (a) { }
// operations
int id () { return id_number; }
void operator = (Widget & rhs)
{ id_number = rhs.id_number; }
bool operator == (Widget & rhs)
{ return id_number == rhs.id_number; }
bool operator < (Widget & rhs)
{ return id_number < rhs.id_number; }
protected:
int id_number; // widget identification number
};
ostream & operator << (ostream & out, Widget & w)
// output printable representation of widget
{
return out << "Widget " << w.id();
}
class WidgetTester {
public:
WidgetTester (int id) { test_id = id; }
int test_id;
// define the function call operator
bool operator () (Widget & wid)
{ return wid.id() == test_id; }
};
//
// class inventory
// manage inventory control
class inventory {
public:
void order (int wid); // process order for widget type wid
void receive (Widget wid); // receive widget of type wid in shipment
protected:
list on_hand;
list on_order;
};
void inventory::receive (Widget wid)
// process a widget received in shipment
{
cout << "Received shipment of widget " << wid << endl;
list::iterator we_need =
find (on_order.begin(), on_order.end(), wid.id());
if (we_need != on_order.end()) {
cout << "Ship " << wid << " to fill back order" << endl;
on_order.erase(we_need);
}
else
on_hand.push_front(wid);
}
void inventory::order (int wid)
// process an order for a widget with given id number
{
cout << "Received order for widget type " << wid << endl;
WidgetTester wtest(wid); // create the tester object
list::iterator we_have =
find_if(on_hand.begin(), on_hand.end(), wtest);
if (we_have != on_hand.end()) {
cout << "Ship " << *we_have << endl;
on_hand.erase(we_have);
}
else {
cout << "Back order widget of type " << wid << endl;
on_order.push_front(wid);
}
}
//
// driver program
//
void main() {
inventory wworks;
wworks.receive (Widget(1));
wworks.receive (Widget(2));
wworks.receive (Widget(3));
wworks.order (2);
wworks.order (2);
wworks.receive (Widget(3));
wworks.receive (Widget(2));
wworks.order (1);
system ("Pause");
}
Thank you
//Here is a close example but dosent fulfil all the requirements
#include
#include
#include
using namespace std;
class Widget {
public:
// constructors
Widget ()
{
id_number =0;
}
Widget (int a)
{
id_number =a;
}
// operations
int id ()
{
return id_number;
}
void operator = (Widget &w)
{
id_number = w.id_number;
}
bool operator == (Widget & w)
{
return id_number == w.id_number;
}
bool operator < (Widget & w)
{
return id_number < w.id_number;
}
protected:
int id_number; // widget identification number
};
ostream & operator << (ostream & out, Widget & w)
// output printable representation of widget
{
return out << "Widget " << w.id();
}
class WidgetTester
{
public:
WidgetTester (int id)
{
test_id = id;
}
int test_id;
// define the function call operator
bool operator () (int id)
{
Widget w;
id=test_id;
bool a;
if (w.id() == id)
a=true;
else
a=false;
return a;
}
};
class inventory
{
public:
void order (int wid); // process order for widget type wid
void receive (Widget wid); // receive widget of type wid in shipment
void displayonhand();
void displayonorder();
protected:
std::list on_hand;
std::list on_order;
};
void inventory::displayonhand()
{ list::const_iterator it;
for(it=on_hand.begin(); it!=on_hand.end(); ++it)
{
cout <<"On Hand"<< *it << endl; // each element on a separate line
}
}
void inventory::displayonorder()
{ list::const_iterator it;
for(it=on_order.begin(); it!=on_order.end(); ++it)
{
cout <<"On Order"<< *it << endl; // each element on a separate line
}
}
void inventory::receive (Widget wid)
// process a widget received in shipment
{
cout << "Received shipment of widget " << wid << endl;
std::list::iterator we_need =
std::find(on_order.begin(), on_order.end(), wid.id());
if (we_need != on_order.end()) {
cout << "Ship " << wid << " to fill back order" << endl;
on_order.erase(we_need);
}
else
on_hand.push_front(wid.id());
}
void inventory::order (int wid)
// process an order for a widget with given id number
{
cout << "Received order for widget type " << wid << endl;
WidgetTester wtest(wid); // create the tester object
std::list::iterator we_have =
find_if(on_hand.begin(), on_hand.end(), wtest);
if (we_have != on_hand.end()) {
cout << "Ship " << *we_have << endl;
on_hand.erase(we_have);
}
else {
cout << "Back order widget of type " << wid << endl;
on_order.push_front(wid);
}
}
void main()
{
int menu=0;
inventory wworks;
do
{
cout<<"1) process an order"< cout<<"2) receive a shipment"< cout<<"3) show on hand"< cout<<"4) show on order"< cout<<"5) exit"< cout<<"Enter a option:";
cin>>menu;
switch(menu)
{
case 1:
{
int num;
cout<<"Enter the Widget No from 0 to 2:"< cin>>num;
wworks.order(num);
}
break;
case 2:
{
int wid;
cout<<"Enter the Wiget No to receive Shipment:"< cin>>wid;
wworks.receive(Widget(wid));
}
break;
case 3:
{
wworks.displayonhand();
}
break;
case 4:
{
wworks.displayonorder();
}
break;
default:
cout<<"Proper Option"< break;
}
}
while(menu!=5);
//wworks.receive (Widget(1));
//wworks.receive (Widget(2));
//wworks.receive (Widget(3));
//wworks.order (2);
//wworks.order (2);
//wworks.receive (Widget(3));
//wworks.receive (Widget(2));
//wworks.order (1);
system ("Pause");
return 0;
}
Explanation / Answer
#include
#include
#include
using namespace std;
class Widget {
public:
// constructors
Widget ()
{
id_number =0;
}
Widget (int a)
{
id_number =a;
}
// operations
int id ()
{
return id_number;
}
void operator = (Widget &w)
{
id_number = w.id_number;
}
bool operator == (Widget & w)
{
return id_number == w.id_number;
}
bool operator < (Widget & w)
{
return id_number < w.id_number;
}
protected:
int id_number; // widget identification number
};
ostream & operator << (ostream & out, Widget & w)
// output printable representation of widget
{
return out << "Widget " << w.id();
}
class WidgetTester
{
public:
WidgetTester (int id)
{
test_id = id;
}
int test_id;
// define the function call operator
bool operator () (int id)
{
Widget w;
id=test_id;
bool a;
if (w.id() == id)
a=true;
else
a=false;
return a;
}
};
class inventory
{
public:
void order (int wid); // process order for widget type wid
void receive (Widget wid); // receive widget of type wid in shipment
void displayonhand();
void displayonorder();
protected:
std::list on_hand;
std::list on_order;
};
void inventory::displayonhand()
{ list::const_iterator it;
for(it=on_hand.begin(); it!=on_hand.end(); ++it)
{
cout <<"On Hand"<< *it << endl; // each element on a separate line
}
}
void inventory::displayonorder()
{ list::const_iterator it;
for(it=on_order.begin(); it!=on_order.end(); ++it)
{
cout <<"On Order"<< *it << endl; // each element on a separate line
}
}
void inventory::receive (Widget wid)
// process a widget received in shipment
{
cout << "Received shipment of widget " << wid << endl;
std::list::iterator we_need =
std::find(on_order.begin(), on_order.end(), wid.id());
if (we_need != on_order.end()) {
cout << "Ship " << wid << " to fill back order" << endl;
on_order.erase(we_need);
}
else
on_hand.push_front(wid.id());
}
void inventory::order (int wid)
// process an order for a widget with given id number
{
cout << "Received order for widget type " << wid << endl;
WidgetTester wtest(wid); // create the tester object
std::list::iterator we_have =
find_if(on_hand.begin(), on_hand.end(), wtest);
if (we_have != on_hand.end()) {
cout << "Ship " << *we_have << endl;
on_hand.erase(we_have);
}
else {
cout << "Back order widget of type " << wid << endl;
on_order.push_front(wid);
}
}
void main()
{
int menu=0;
inventory wworks;
do
{
cout<<"1) process an order"< cout<<"2) receive a shipment"< cout<<"3) show on hand"< cout<<"4) show on order"< cout<<"5) exit"< cout<<"Enter a option:";
cin>>menu;
switch(menu)
{
case 1:
{
int num;
cout<<"Enter the Widget No from 0 to 2:"< cin>>num;
wworks.order(num);
}
break;
case 2:
{
int wid;
cout<<"Enter the Wiget No to receive Shipment:"< cin>>wid;
wworks.receive(Widget(wid));
}
break;
case 3:
{
wworks.displayonhand();
}
break;
case 4:
{
wworks.displayonorder();
}
break;
default:
cout<<"Proper Option"< break;
}
}
while(menu!=5);
//wworks.receive (Widget(1));
//wworks.receive (Widget(2));
//wworks.receive (Widget(3));
//wworks.order (2);
//wworks.order (2);
//wworks.receive (Widget(3));
//wworks.receive (Widget(2));
//wworks.order (1);
system ("Pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.