Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "inventory.h"
- void inventory( istream& in, ostream& out )
- {
- Widget myWidget[25];
- int count;
- readwidget( in, myWidget, count);
- string command;
- in >> command;
- while (!in.fail() )
- {
- out << "Command: " << command << endl;
- if (command == "list")
- {
- list( in, out, myWidget, count);
- }
- else if (command == "add")
- {
- add( in, out, myWidget, count );
- }
- else if (command == "buy")
- {
- buy(in, out, myWidget, count );
- }
- else
- {
- save( in, out, myWidget, count );
- }
- in >> command;
- }
- }
- void readwidget( istream& in2, Widget myWidget[], int &count )
- {
- in2.ignore( 200, ':');
- string filename;
- in2 >> filename;
- ifstream in(filename.c_str() );
- Widget widgetdata;
- count = 0;
- in.ignore( 200, ':' );
- in >> widgetdata.name; //read the name of the widget
- in.ignore( 200, ':' );
- getline( in, widgetdata.description); //read the description of the widget
- in.ignore( 200, ':' );
- in >> widgetdata.part;
- in.ignore( 200, '$' );
- in >> widgetdata.cost;
- in.ignore( 200, ':' );
- in >> widgetdata.quantity;
- while ( !in.fail() && count < 25)
- {
- myWidget[count] = widgetdata;
- count++;
- in.ignore( 200, ':' );
- in >> widgetdata.name; //read the name of the widget
- in.ignore( 200, ':' );
- getline( in, widgetdata.description); //read the description of the widget
- in.ignore( 200, ':' );
- in >> widgetdata.part;
- in.ignore( 200, '$' );
- in >> widgetdata.cost;
- in.ignore( 200, ':' );
- in >> widgetdata.quantity;
- }
- }
- void list( istream& in, ostream& out, Widget myWidget[], int count)
- {
- char c;
- c = in.peek();
- if ( c == '\n' )
- {
- for ( int i = 0; i < count; i++)
- {
- out << "Widget: " << myWidget[i].name << endl;
- out << "Description: " << myWidget[i].description << endl;
- out << "Part #: " << myWidget[i].part << endl;
- out << "Cost: $" << myWidget[i].cost << endl;
- out << "Quantity: " << myWidget[i].quantity << endl;
- }
- }
- else
- {
- string partnumber;
- in >> partnumber;
- bool found = false;
- for ( int i=0; i<count; i++ )
- {
- if (myWidget[i].part == partnumber )
- {
- found = true;
- out << "Widget: " << myWidget[i].name << endl;
- out << "Description: " << myWidget[i].description << endl;
- out << "Part #: " << myWidget[i].part << endl;
- out << "Cost: $" << myWidget[i].cost << endl;
- out << "Quantity: " << myWidget[i].quantity << endl;
- break;
- }
- }
- if (!found)
- {
- out << "Sorry, we don't have " << partnumber << " in stock" << endl;
- }
- }
- }
- void save( istream& in, ostream& out, Widget myWidget[], int count )
- {
- string file;
- in >> file;
- ofstream myfile (file.c_str() );
- for ( int i = 0; i < count; i++)
- {
- myfile << "Widget: " << myWidget[i].name << endl;
- myfile << "Description: " << myWidget[i].description << endl;
- myfile << "Part #: " << myWidget[i].part << endl;
- myfile << "Cost: $" << myWidget[i].cost << endl;
- myfile << "Quantity: " << myWidget[i].quantity << endl;
- }
- myfile.close();
- }
- void add( istream& in, ostream& out, Widget myWidget[], int &count )
- {
- Widget widgetdata;
- bool found = false; // declare a boolian variable as false
- in.ignore( 200, ':' );
- in >> widgetdata.name; //read the name of the widget
- in.ignore( 200, ':' );
- getline( in, widgetdata.description); //read the description of the widget
- in.ignore( 200, ':' );
- in >> widgetdata.part; // read in the part number of the widget
- in.ignore( 200, '$' );
- in >> widgetdata.cost; // read in the cost of the widget (as a double)
- in.ignore( 200, ':' );
- in >> widgetdata.quantity; // read in the quantity of the widget
- for ( int i=0; i<count; i++ )
- {
- if (myWidget[i].part == widgetdata.part ) // if the part already exists
- {
- found = true; // if the part matches make the boolian true
- myWidget[i].quantity = myWidget[i].quantity + widgetdata.quantity;
- break; // break the loop once the part is found
- }
- }
- if (!found)
- {
- myWidget[count] = widgetdata;
- count++;
- }
- }
- void buy( istream& in, ostream& out, Widget myWidget[], int count )
- {
- in.ignore( 10, ';' );
- string partnumber;
- int amount;
- in >> partnumber;
- in >> amount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement