Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Rewrite Project 3 using an array of objects, rather than the three parallel a

ID: 3694371 • Letter: 1

Question

1.    Rewrite Project 3 using an array of objects, rather

than the three parallel arrays of Project 3.

Create a class named inventory, there are 3 data fields:

itemId, storeNr and quantity. Design the necessary header file and implementation file.

2. Write three C++ functions as described below:

      The variables listed below have the following meaning:

      fileName    - the Windows name of the file created in step 1

      newFileName - the Windows file name of the extracted records

      requestId   - the product id number used to extract data

      ORDER_VALUE - the quantity value used to extract data, make this

                     a global constant with a value of 500

      invList     - the array of inventory objects

     

      count       - the actual number of cells filled in the array

      newcount    - the number of extracted records written to newFileName

a)    bool  loadArray(char fileName[], inventory invList[],

                          int & count, int maxCells)

            Will load the object array, invList, from the disk file and use count for the number of cells loaded.  Be certain to check that you do not load more cells than you have dimensioned.

            Return true if you were able to load all of the data,

            return false otherwise. The variable count will hold the

            number of cells loaded.

b)    void printArray(ostream & where, const inventory invList[], int count)

            Will print to the stream where the data in the array of

            objects. Count tells the number of cells filled.

   c) bool extractData(char newFileName[],int requestId, int baseQty,

               const inventory invList[], int count, int & newcount)

            Will use the value in requestId to write only the records with that id number that have a quantity below baseQty to the new file. The variable newcount will hold the number of records actually written. The return value will be false if the

            file could not be opened, otherwise the return value will be true.

3. Write a main program to test the three functions. Always check to see that a file was successfully opened. If the file was NOT successfully opened, write an error message and exit the program.

4. Turn in:

                  - C++ source program

- Two execution outputs – one with all data loaded and one

- where there was not room for all data.

                  - A printed copy of your file created in step 1

                  - A printed copy of the file created by extractData

project 3

1. Using an editor create a text file that has around 20 lines of information. Each line of information will include a product id, a store number, and a quantity.

2. Write three C++ functions as described below:

      The variables listed below have the following meaning:

      fileName    - the Windows name of the file created in step 1

      newFileName - the Windows file name of the file of extracted records

      requestId    - the product id number used to extract data

      ORDER_VALUE - the quantity value used to extract data, make this

                     a global constant with a value of 500

      idArray     - the array of id numbers

      storeArray - the array of store numbers

      qtyArray    - the array of quantities

      count       - the actual number of cells filled in the arrays

      newcount    - the number of extracted records written to newFileName

a)   bool  loadArrays(const char fileName[],long idArray[],

              int storeArray[], int qtyArray[], int & count, int maxCells)

            Will load the three arrays from data from the disk file and use count for the number of cells loaded. Be certain to check that you do not load more cells than you have dimensioned.

            Return true if you were able to load all of the data,

            return false otherwise. The variable count will hold the

            number of cells loaded.

b)   void printArrays(ostream & where, const long idArray[],

                const int storeArray[], const int qtArray[], int count)

            Will print to the stream where the data in the arrays.

            Count tells the number of cells filled.

   c) bool extractData(const char newFileName[],int requestId, int baseQty,

               const long idArray[], const int storeArray[],

               const int qtArray[], int count, int & newcount)

            Will use the value in requestId to write only the records with that id number that have a quantity below baseQty(the ORDER_VALUE) to the new file. The variable newcount will hold the number of records actually written. The return value will be false if the

            file could not be opened, otherwise the return value will be true.

3. Write a main program to test the three functions. Always check to see that a file was successfully opened. If the file was NOT successfully opened, write an error message and exit the program.

4. Turn in:

                  - C++ source program

- Two execution outputs – one with all data loaded and one

-             where there was not room for all data.

                  - A copy of your file created in step 1

                  - A copy of the file created by extractData

Explanation / Answer

parallel.cpp

#include <iostream>
#include <fstream>

using namespace std;

const int ORDER_VALUE = 500;
const int MAX_SIZE = 30;

// loads 3 arrays from file and use count to load cell dimensions
bool loadArrays(const char fileName[], int idArray[], int storeArray[], int qtyArray[], int &count, int maxCells);

// prints location of data in array and number of cells filled
void printArrays(ostream &where, const int idArray[], const int storeArray[], const int qtArray[], int count);

// loads data specifically from requestID provided
bool extractData(const char newFileName[], int requestId, int baseQty, const int idArray[], const int storeArray[], const int qtArray[], int count, int &newcount);

int main()
{
   int idProduct[MAX_SIZE];
   int storeNumber[MAX_SIZE];
   int quantity[MAX_SIZE];

   int howMany = 0; // how many cells loaded from original file
   int newCount = 0; // the number of extracted records written to new file
   int searchID; // product ID

   char fileName[MAX_SIZE];
   char newFileName[MAX_SIZE];

   cout << "Enter the name of the file to read: ";
   cin >> fileName;

   // change last parameter to 10 to test where no room for all data
   if (loadArrays(fileName, idProduct, storeNumber, quantity, howMany, 10))
   {
       cout << "All data loaded." << endl;
   }
   else
   {
       cout << "Unable to load all of the records." << endl;
   }

   cout << howMany << " cells loaded." << endl << endl;

   printArrays(cout, idProduct, storeNumber, quantity, howMany);

   cout << endl;
   cout << "Please enter new file name: ";
   cin >> newFileName;
   cout << "Please enter product ID number: ";
   cin >> searchID;

   extractData(newFileName, searchID, ORDER_VALUE, idProduct, storeNumber, quantity, howMany, newCount);
  
   cout << newCount << " cells written to file." << endl;

   return 0;
}

bool loadArrays(const char fileName[], int idArray[], int storeArray[], int qtyArray[], int &count, int maxCells)
{
   ifstream in;
   in.open(fileName);

   if (!in)
   {
       cout << "Invalid file. Please try again." << endl;
       exit(1); // terminate program
   }

   int i = 0;
   while ((i < maxCells) && (in >> idArray[i] >> storeArray[i] >> qtyArray[i]))
   {
       i++;
   }
   count = i;

   // check if there is more data
   if (in.good())
   {
       return false;
   }
  
   in.close();
   return true;
}

void printArrays(ostream &where, const int idArray[], const int storeArray[], const int qtArray[], int count)
{
   where << "Product " << "Store " << "Quantity" << endl;
   for (int i = 0; i < count; i++)
   {
       where << idArray[i] << " " << storeArray[i] << " " << qtArray[i] << endl;
   }
}

bool extractData(const char newFileName[], int requestId, int baseQty, const int idArray[], const int storeArray[], const int qtArray[], int count, int &newcount)
{
   ofstream outfile;
   outfile.open(newFileName);

   if (!outfile.is_open())
   {
       cout << "Unable to open file." << endl;
       return false;
   }

   outfile << "Product ID " << requestId << endl;
   outfile << "Stores " << "Quantity" << endl;

   for (int i = 0; i < count; i++)
   {
       if (idArray[i] == requestId && qtArray[i] < baseQty)
       {
           outfile << storeArray[i] << " " << qtArray[i] << endl;
           newcount++;
       }
   }

   outfile.close();
   return true;  
}

parallel.txt
34230 1425 46
24098 1425 94
13133 1425 12
23246 1090 23
59324 1090 58
13133 3802 78
13133 3802 47
83473 3802 38
42424 3802 43
35020 1314 85
34230 1314 10
42424 1458 0
59324 1458 66
24098 3529 53
83473 3529 58
64837 5032 18
34230 5032 22
23246 5032 30
74537 4585 14
42424 4585 25

sample output

Enter the name of the file to read: parallel.txt
All data loaded.
20 cells loaded.

Product Store   Quantity
34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85
34230   1314    10
42424   1458    0
59324   1458    66
24098   3529    53
83473   3529    58
64837   5032    18
34230   5032    22
23246   5032    30
74537   4585    14
42424   4585    25

Please enter new file name: output.txt
Please enter product ID number: 42424
3 cells written to file.
Press any key to continue . . .

--------- Second execution ------------------
Enter the name of the file to read: parallel.txt
Unable to load all of the records.
10 cells loaded.

Product Store   Quantity
34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85

Please enter new file name: output2.txt
Please enter product ID number: 42424
1 cells written to file.
Press any key to continue . . .

main.cpp

#include <iostream>
#include <fstream>
#include "inventory.h"

const int ORDER_VALUE = 500; // quantity value used to extract data
const int MAX_ARR_SIZE = 25; // max array size
const int MAX_FILENAME_LEN = 40; // max length for filename string

// will load data from the disk file and returns true if all data are loaded
bool loadArray(char fileName[], inventory invList[], int & count, int maxCells);

// prints the data in the array of objects to the stream "where"
void printArray(ostream & where, const inventory invList[], int count);

// will write all data with matching requestId and under quantity of baseQty to new file
bool extractData(char newFileName[], int requestId, int baseQty,
   const inventory invList[], int count, int & newcount);

int main()
{
   inventory invList[MAX_ARR_SIZE];

   int count = 0; // holds the number of cells found in original file
   int newcount = 0; // hold the number of extracted records written to new file
   int requestId;

   char fileName[MAX_FILENAME_LEN];
   char newFile[MAX_FILENAME_LEN];

   cout << "Enter the name of the file to be read: ";
   cin >> fileName; // inventory.txt

   // change last parameter to 10 to test when no room for all data
   if (loadArray(fileName, invList, count, MAX_ARR_SIZE))
   {
       cout << "All data loaded." << endl;
   }
   else
   {
       cout << "Insufficent storage. Unable to load all data." << endl;
   }

   cout << count << " record(s) found" << endl << endl;
   printArray(cout, invList, count);

   cout << endl;
   cout << "Enter new file name: ";
   cin >> newFile;
   cout << "Enter product ID to search for: ";
   cin >> requestId; // product id number

   extractData(newFile, requestId, ORDER_VALUE, invList, count, newcount);
   cout << newcount << " record(s) written to file." << endl << endl;

   return 0;
}

void printArray(ostream & where, const inventory invList[], int count)
{
   for (int i = 0; i < count; i++)
   {
       where << invList[i].getId() << " "
           << invList[i].getStoreNr() << " "
           << invList[i].getQuantity() << endl;
   }
}

bool loadArray(char fileName[], inventory invList[], int & count, int maxCells)
{
   ifstream file;
   file.open(fileName);

   if (!file)
   {
       cout << "Unable to open file." << endl;
       exit(1); // terminate program
   }

   count = 0;
   for (int i = 0; i < maxCells; i++)
   {
       int prod, store, qty; // temp variables
       file >> prod >> store >> qty;
       invList[i].setId(prod);
       invList[i].setStoreNr(store);
       invList[i].setQuantity(qty);
       count++; // tracks the number of cells loaded
   }

   // check if there is more data
   if (file.good())
   {
       return false;
   }

   file.close();

   return true;
}

bool extractData(char newFileName[], int requestId, int baseQty,
   const inventory invList[], int count, int & newcount)
{
   ofstream outfile;
   outfile.open(newFileName);

   if (!outfile.is_open())
   {
       cout << "Unable to open file." << endl;
       return false;
   }

   newcount = 0;
   for (int i = 0; i < count; i++)
   {
       if (invList[i].getId() == requestId && invList[i].getQuantity() < baseQty)
       {
           outfile << invList[i].getId() << " "
               << invList[i].getStoreNr() << " "
               << invList[i].getQuantity() << endl;
           newcount++;
       }
   }

   outfile.close();

   return true;
}


inventory.cpp
#include "inventory.h"

inventory::inventory()
{
   itemId = 0;
   storeNr = 0;
   quantity = 0;
}

inventory::inventory(int id, int store, int qt)
{
   itemId = id;
   storeNr = store;
   quantity = qt;
}

void inventory::setId(int id)
{
   itemId = id;
}

void inventory::setStoreNr(int store)
{
   storeNr = store;
}

void inventory::setQuantity(int qt)
{
   quantity = qt;
}

int inventory::getId(void) const
{
   return itemId;
}

int inventory::getStoreNr(void) const
{
   return storeNr;
}

int inventory::getQuantity(void) const
{
   return quantity;
}

inventory.h


#ifndef inventory_h
#define inventory_h
#include <iostream>

using namespace std;

/**
* Class Inventory represents an item. Inventory shows which store has the item and how many. */
class inventory
{
public:
   inventory();
   inventory(int id, int store, int qt);

   void setId(int id);
   void setStoreNr(int store);
   void setQuantity(int qt);

   int getId(void) const;
   int getStoreNr(void) const;
   int getQuantity(void) const;

private:
   int itemId;
   int storeNr;
   int quantity;
};
#endif

inventory.txt
34230 1425 46
24098 1425 94
13133 1425 12
23246 1090 23
59324 1090 58
13133 3802 78
13133 3802 47
83473 3802 38
42424 3802 43
35020 1314 85
34230 1314 10
42424 1458 0
59324 1458 66
24098 3529 53
83473 3529 58
64837 5032 18
34230 5032 22
23246 5032 30
74537 4585 14
42424 4585 25

sample output

Enter the name of the file to be read: inventory.txt
All data loaded.
25 record(s) found.

34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85
34230   1314    10
42424   1458    0
59324   1458    66
24098   3529    53
83473   3529    58
64837   5032    18
34230   5032    22
23246   5032    30
74537   4585    14
42424   4585    25
42424   4585    25
42424   4585    25
42424   4585    25
42424   4585    25
42424   4585    25

Enter new file name: all_data_loaded.txt
Enter ID to search for: 42424
8 record(s) written to file.

Press any key to continue . . .

--------------------------------
----- No room for all data -----
--------------------------------

Enter the name of the file to be read: inventory.txt
Insufficent storage. Unable to load all data.
10 record(s) found

34230   1425    46
24098   1425    94
13133   1425    12
23246   1090    23
59324   1090    58
13133   3802    78
13133   3802    47
83473   3802    38
42424   3802    43
35020   1314    85

Enter new file name: no_room.txt
Enter product ID to search for: 13133
3 record(s) written to file.

Press any key to continue . . .