Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. #include "inventory.h"
  2.  
  3. void inventory( istream& in, ostream& out )
  4. {
  5.     Widget myWidget[25];
  6.     int count;
  7.     readwidget( in, myWidget, count);
  8.     string command;
  9.     in >> command;
  10.     while (!in.fail() )
  11.     {
  12.         out << "Command: " << command << endl;
  13.         if (command == "list")
  14.         {
  15.             list( in, out, myWidget, count);
  16.         }
  17.         else if (command == "add")
  18.         {
  19.        
  20.             add( in, out, myWidget, count );
  21.         }
  22.         else if (command == "buy")
  23.         {
  24.             buy(in, out, myWidget, count );
  25.         }
  26.         else
  27.         {
  28.             save( in, out, myWidget, count );
  29.         }
  30.         in >> command;
  31.     }
  32. }
  33.  
  34.  
  35. void readwidget( istream& in2, Widget myWidget[], int &count )
  36. {
  37.     in2.ignore( 200, ':');
  38.     string filename;
  39.     in2 >> filename;
  40.     ifstream in(filename.c_str() );
  41.     Widget widgetdata;
  42.     count = 0;
  43.     in.ignore( 200, ':' );
  44.     in >> widgetdata.name; //read the name of the widget
  45.     in.ignore( 200, ':' );
  46.     getline( in, widgetdata.description); //read the description of the widget
  47.     in.ignore( 200, ':' );
  48.     in >> widgetdata.part;
  49.     in.ignore( 200, '$' );
  50.     in >> widgetdata.cost;
  51.     in.ignore( 200, ':' );
  52.     in >> widgetdata.quantity;
  53.    
  54.     while ( !in.fail() && count < 25)
  55.     {
  56.         myWidget[count] = widgetdata;
  57.         count++;
  58.         in.ignore( 200, ':' );
  59.         in >> widgetdata.name; //read the name of the widget
  60.         in.ignore( 200, ':' );
  61.         getline( in, widgetdata.description); //read the description of the widget
  62.         in.ignore( 200, ':' );
  63.         in >> widgetdata.part;
  64.         in.ignore( 200, '$' );
  65.         in >> widgetdata.cost;
  66.         in.ignore( 200, ':' );
  67.         in >> widgetdata.quantity;
  68.     }
  69. }
  70.  
  71.  
  72. void list( istream& in, ostream& out, Widget myWidget[], int count)
  73. {
  74.     char c;
  75.     c = in.peek();
  76.     if ( c == '\n' )
  77.     {
  78.         for ( int i = 0; i < count; i++)
  79.         {
  80.             out << "Widget: " << myWidget[i].name << endl;
  81.             out << "Description: " << myWidget[i].description << endl;
  82.             out << "Part #: " << myWidget[i].part << endl;
  83.             out << "Cost: $" << myWidget[i].cost << endl;
  84.             out << "Quantity: " << myWidget[i].quantity << endl;
  85.         }
  86.     }
  87.     else
  88.     {
  89.         string partnumber;
  90.         in >> partnumber;
  91.         bool found = false;
  92.         for ( int i=0; i<count; i++ )
  93.         {
  94.             if (myWidget[i].part == partnumber )
  95.             {
  96.                 found = true;
  97.                 out << "Widget: " << myWidget[i].name << endl;
  98.                 out << "Description: " << myWidget[i].description << endl;
  99.                 out << "Part #: " << myWidget[i].part << endl;
  100.                 out << "Cost: $" << myWidget[i].cost << endl;
  101.                 out << "Quantity: " << myWidget[i].quantity << endl;
  102.                 break;
  103.             }
  104.            
  105.         }
  106.         if (!found)
  107.             {
  108.                 out << "Sorry, we don't have " << partnumber << " in stock" << endl;
  109.             }
  110.     }
  111. }
  112.  
  113. void save( istream& in, ostream& out, Widget myWidget[], int count )
  114. {
  115.     string file;
  116.     in >> file;
  117.     ofstream myfile (file.c_str() );
  118.     for ( int i = 0; i < count; i++)
  119.     {
  120.         myfile << "Widget: " << myWidget[i].name << endl;
  121.         myfile << "Description: " << myWidget[i].description << endl;
  122.         myfile << "Part #: " << myWidget[i].part << endl;
  123.         myfile << "Cost: $" << myWidget[i].cost << endl;
  124.         myfile << "Quantity: " << myWidget[i].quantity << endl;
  125.     }
  126.     myfile.close();
  127. }
  128.    
  129.  
  130. void add( istream& in, ostream& out, Widget myWidget[], int &count )
  131. {
  132.     Widget widgetdata;
  133.     bool found = false; // declare a boolian variable as false
  134.     in.ignore( 200, ':' );
  135.     in >> widgetdata.name; //read the name of the widget
  136.     in.ignore( 200, ':' );
  137.     getline( in, widgetdata.description); //read the description of the widget
  138.     in.ignore( 200, ':' );
  139.     in >> widgetdata.part; // read in the part number of the widget
  140.     in.ignore( 200, '$' );
  141.     in >> widgetdata.cost; // read in the cost of the widget (as a double)
  142.     in.ignore( 200, ':' );
  143.     in >> widgetdata.quantity; // read in the quantity of the widget
  144.    
  145.     for ( int i=0; i<count; i++ )
  146.     {
  147.         if (myWidget[i].part == widgetdata.part ) // if the part already exists
  148.         {
  149.             found = true; // if the part matches make the boolian true
  150.             myWidget[i].quantity = myWidget[i].quantity + widgetdata.quantity;
  151.             break; // break the loop once the part is found
  152.         }
  153.            
  154.     }
  155.     if (!found)
  156.     {
  157.         myWidget[count] = widgetdata;
  158.         count++;
  159.     }
  160. }  
  161.        
  162.        
  163.        
  164. void buy( istream& in, ostream& out, Widget myWidget[], int count )
  165. {
  166.     in.ignore( 10, ';' );
  167.     string partnumber;
  168.     int amount;
  169.     in >> partnumber;
  170.     in >> amount;
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement