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

Create a C++ program to process binary file for the hardware store inventory (si

ID: 3660469 • Letter: C

Question

Create a C++ program to process binary file for the hardware store inventory (similar to the Lab Assignment #8) but with additional functions. You may want to solve this problem step-by-step using the following guidelines:

1. First use the Lab Assignment #8.

2. Add another choice to delete record.

3. Add another choice to update record.

4. Add another choice to calculate the total inventory cost. The total inventory cost is the sum of all item inventory cost (quantity * cost).

LAB 8 (Include my lab 8 for the sake of time)

_______________________________________________________________________

lab8-3.cpp

#include <iostream>
#include <fstream>
using namespace std;
using std::ofstream;

#include <string>
using std::string;

#include <iomanip>
using namespace std;

#include "HardwareData.h"

void initializeFile( fstream & );
void inputData( fstream & );
void listHardwareDatas( fstream & );
void insertRecord( fstream & );
bool openFile( fstream & );


int main()
{
fstream file;

int choice = 7; // default to exit condition

do
{
cout << " ***************************************" << endl ;
cout << "Enter 1 for opening data file" << endl ;
cout << "Enter 2 for listing all the records" << endl;
cout << "Enter 3 for entering a new record" << endl;
cout << "Enter 4 to delete a record" << endl;
cout << "Enter 5 to update a record" << endl;
cout << "Enter 6 to total inventory cost" << endl;
cout << "Enter 7 to exit" << endl;
cout << "***************************************" << endl ;
cout << "Choice: " ;
cin >> choice;


switch( choice )
{
case 1:
// open data file
openFile( file );
break;
case 2:
listHardwareDatas( file );
break;
case 3:
inputData( file );
break;
case 4:

default:
continue;
} // end switch

} while ( choice != 4 ); // end do while

file.close();
system("pause");
return 0;
} // end main

bool openFile( fstream &file )
{
char fileName[20];
cout << " Enter the file name: ";
cin.ignore();
cin.getline (fileName, 20);
file.open(fileName, ios::in | ios::out | ios::binary );

if ( file )
{
cout << "file open success. ";
return true;
}

ofstream outFile(fileName, ios::out | ios::binary);
outFile.close();
cout << "File does not exist. create the data file. ";

file.clear();
file.open(fileName, ios::in | ios::out | ios::binary );

initializeFile( file );
return true;
}

void initializeFile( fstream &fRef )
{
HardwareData blankItem; // empty HardwareData object

// fill file with blank records
for ( int i = 0; i < 100; i++ )
fRef.write(
reinterpret_cast< char * >( &blankItem ), sizeof( HardwareData ) );
} // end function initializeFile

// function that receives input
void inputData( fstream &fRef )
{
HardwareData temp; // temporary HardwareData object

// temporary variables used to hold user input
int number;
char name[ LENGTH ];
double price;
int stock;

// ask user for and set partNumber
cout << "Enter the part number (0 - 99, -1 to end input): ";
cin >> number;

// set HardwareData members until -1 is entered
while ( number != -1 )
{
cout << "Enter the tool name: "; // ask user for tool name
cin.ignore(); // ignore the newline on the input stream
cin.getline( name, LENGTH ); // store tool name in variable name
temp.setToolName( name ); // set tool member name
temp.setPartNumber( number ); // set part number

// ask user for quantity and price
cout << "Enter quantity and price: ";
cin >> stock >> price; // store input in temporary variables
temp.setInStock( stock ); // set inStock
temp.setUnitPrice( price ); // set unitPrice

// place file position pointer at next write location
fRef.seekp( ( temp.getPartNumber() ) * sizeof( HardwareData ) );

// write data to file
fRef.write( reinterpret_cast< char * >( &temp ), sizeof( HardwareData ) );

// ask user for next part number
cout << "Enter the part number (0 - 99, -1 to end input): ";
cin >> number;
} // end while
} // end inputData

void listHardwareDatas( fstream &fRef )
{
HardwareData temp;

// display column headings
cout << setw( 7 ) << "Record#" << " " << left
<< setw( 30 ) << "HardwareData name" << left
<< setw( 13 ) << "Quantity" << left << setw( 10 ) << "Cost" << endl;

// continue until 100 tools are displayed or end of file reached
for ( int count = 0; count < 100 ; count++ )
{
// set file position pointer and begin reading
fRef.seekg( count * sizeof( HardwareData ) );
fRef.read( reinterpret_cast< char * >( &temp ), sizeof( HardwareData ) );

// if part number is valid, display HardwareData information
if ( temp.getPartNumber() >= 0 && temp.getPartNumber() < 100 )
{
cout << fixed << showpoint;
cout << left << setw( 7 ) << temp.getPartNumber() << " "
<< left << setw( 30 ) << temp.getToolName() << left
<< setw( 13 ) << temp.getInStock() << setprecision( 2 )
<< left << setw( 10 ) << temp.getUnitPrice() << ' ';
} // end if
} // end for
} // end function listHardwareDa


_______________________________________________________________________________


HardwareData.cpp

#include <string>
using std::string;

#include <cstring>
using std::strncpy;

#include "HardwareData.h"

// default HardwareData constructor
HardwareData::HardwareData( int partNumberValue, string toolNameValue, int inStockValue,
double unitPriceValue )
{
setPartNumber( partNumberValue );
setToolName( toolNameValue );
setInStock( inStockValue );
setUnitPrice( unitPriceValue );
} // end HardwareData constructor

// set part-number value
void HardwareData::setPartNumber( int partNumberValue )
{
partNumber = partNumberValue;
} // end function setPartNumber

// get part-number value
int HardwareData::getPartNumber() const
{
return partNumber;
} // end function getPartNumber

// set tool-name value
void HardwareData::setToolName( string toolNameString )
{
// copy at most 30 characters from string to toolName
const char *toolNameValue = toolNameString.data();
int length = toolNameString.size();
length = ( length < 30 ? length : 29 );
strncpy( toolName, toolNameValue, length );

// append null-terminating character to end of toolName
toolName[ length ] = '';
} // end function setHardwareDataName

// get tool-name value
string HardwareData::getToolName() const
{
return toolName;
} // end function getHardwareDataName

// set in-stock value
void HardwareData::setInStock( int inStockValue )
{
inStock = inStockValue;
} // end function setInStock

// get in-stock value
int HardwareData::getInStock() const
{
return inStock;
} // end function getInStock

// set unit-price value
void HardwareData::setUnitPrice( double unitPriceValue )
{
unitPrice = unitPriceValue;
} // end function setUnitPrice

// get unit-price value
double HardwareData::getUnitPrice() const
{
return unitPrice;
} // end function getUnitPrice



Explanation / Answer

#include "Inventory.h" #include #include #include using std::ofstream; using std::ifstream; using std::fstream; using std::ostream; using std::ios; using std::cerr; using std::endl; using std::cout; using std::cin; using std::left; using std::right; using std::setw; using std::setprecision; using std::fixed; using std::getline; void createFile(); void displayFile(); void displayLine(ostream &output, Inventory &tools); void addTool(); enum Menu {DISPLAY = 1, ADJUST, ADD, DELETE, CREATE, EXIT}; // Enumeration list for menu options. int main() { int selection = 0; do { cout