/*

  FuncList.cpp

  Componants: InventoryRecord.h, FuncList.h, InventoryRecord.cpp, FuncList.cpp, Try.cpp

  Author: Ahmad Mobasseri

*/

 

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include "InventoryRecord.h"

using namespace std;

 

class FuncList

{

      vector<InventoryRecord> XList;            // NOTE: do NOT need this

public:

      FuncList(){};

      void AddList      (vector<InventoryRecord>&);

      void ViewList     (vector<InventoryRecord>);

      void FindList     (vector<InventoryRecord>);

      void RemoveList   (vector<InventoryRecord>&);

      void WriteList    (vector<InventoryRecord>);

      void ReadList     (vector<InventoryRecord>);

};

 

void FuncList::AddList(vector<InventoryRecord>& XList)

{

       InventoryRecord Value;

 

       cout << "Enter Values Below, Stop To Quit" << endl;

       while(true)

       {

             cin >> Value;                   // operator << overload is used.

             if(Value == "Stop")

                   break;

             XList.push_back(Value);

       }

     cin.ignore(1,'\n');

}

 

void FuncList::ViewList(vector<InventoryRecord> XList)

{

      vector<InventoryRecord>::iterator XIterator;

     

      for(XIterator = XList.begin(); XIterator != XList.end(); XIterator++)

            cout << *XIterator << endl;  // using operator<< overload. *XIterator is type InventoryRecord

}

 

void FuncList::FindList(vector<InventoryRecord> XList)

{

      string SearchFor;  bool Found=false;

      cout << "Enter Search Item: ";

    cin >> SearchFor;            

 

      vector<InventoryRecord>::iterator XIterator;

      for(XIterator = XList.begin() ; XIterator != XList.end() ; XIterator++)

            if(*XIterator == SearchFor)

            {

              cout<<"Here is the found item: ";

              cout << *XIterator << endl;          // Operator << overloaded is used

              Found = true;

            }

      if(Found == false)

            cout<<"Item was not found"<<endl;

      cin.ignore(1,'\n');

}

 

void FuncList::RemoveList(vector<InventoryRecord>& XList)

{

      string RemoveItem; int Offset=0;  bool Removal=false;

      cout << "Enter Item To Be Removed: ";

    cin >> RemoveItem;            

 

      vector<InventoryRecord>::iterator XIterator;

      for(XIterator = XList.begin(); XIterator != XList.end(); XIterator++)

      {

            if(*XIterator == RemoveItem)          // NOTE: on remove of the last item, pgm abends ?!

            {                                     // I think on "XIterator != XList.end()" because 

              XList.erase(XList.begin() + Offset);// on removal of last item then XList.end() changes

              cout<<"Item was removed"<<endl;     // Does not happen when iterator is Not used. 

              Removal = true;

            }

          Offset++;

      }

      if(Removal == false)

            cout<<"Item was not removed, because it was not found"<<endl;

    cin.ignore(1,'\n');

}

 

void FuncList::WriteList(vector<InventoryRecord> XList)

{

      string FName;

      cout << "Enter file name: ";

    getline(cin, FName);        

     

      fstream OutFile (FName.c_str(), ios::out);       // Open file for Write

      if (!OutFile.good())

      {

            cout<<"Unable to open for write file: "<< FName <<endl;

            return;

      }

 

      vector<InventoryRecord>::iterator XIterator;

      for(XIterator = XList.begin(); XIterator != XList.end(); XIterator++) // NOTE: How to prevent loop of I/Os ??

      {

            OutFile << *XIterator <<endl;                // Write to file.  Using operator<< overload

      }

      OutFile.close();                                 // Close the file

}

 

 

void FuncList::ReadList(vector<InventoryRecord> XList)

{

      string FName; bool First_Read = true;

    cout << "Enter file name: ";

    getline(cin, FName);         

    fstream InFile (FName.c_str(), ios::in);       // Open file for Read

      try

      {

        if(!InFile.good())

             throw "OpenErr";

         

        string inBuffer;

        cout<<"Record read from file, are: "<<endl;

        while(true)

        {

             getline(InFile, inBuffer);                 // Read from file.

             if(InFile.eof())

                throw "EOF";

             First_Read = false;

             cout<< inBuffer <<endl;                    // Display

        }

//      InFile.close();                               // NOTE: no need for 'close' here as it will always go to catch

      }                                               // end of try blk

     

      catch (char * ex)

      {

            if( ex == "OpenErr")                        // Open file error

            {

                  cout<<"Unable to open for read, file: "<< FName <<endl;

                  return;

            }

            else if( ex == "EOF")                       // End-Of-File

            {

                  if(First_Read == true)

                     cout<<"This file is empty."<<endl;

                  InFile.close();                        // close the file

            }

      }                                              // end of catch

}

 

 

 

/***  NOTE: following is Not using "iterator" *********************

 

void ViewList(vector<InventoryRecord> XList)

{

      for(int K=0; K < XList.size(); K++)

            cout << "--->" << XList[K] << endl;

}

 

void FindList(vector<InventoryRecord> XList)

{

      string SearchFor;  bool Found=false;

      cout << "Enter Search Item: ";

    cin >> SearchFor;             // NOTE: getline(cin, SearchFor); doesn't work proparely ! 

 

      for(int K=0; K < XList.size(); K++)

            if( XList[K] == SearchFor)

            {

              cout<<"Here is the found item: ";

              cout << XList[K] << endl;

              Found = true;

            }

      if(Found == false)

            cout<<"Item was not found"<<endl;

      cin.ignore(1,'\n');

}

 

void RemoveList(vector<InventoryRecord>& XList)

{ 

      string RemoveItem; int Offset=0;  bool Removal=false;

      cout << "Enter Item To Be Removed: ";

    cin >> RemoveItem;            

 

      for(int K=0; K < XList.size(); K++)

      {

            if( XList[K] == RemoveItem)

            {

              XList.erase(XList.begin() + Offset);

              cout<<"Item was removed"<<endl;

              Removal = true;

            }

          Offset++;

      }

      if(Removal == false)

            cout<<"Item was not removed, because it was not found"<<endl;

    cin.ignore(1,'\n');

}

***/