Respuesta :
Using the computational language C++ it is possible to write a code is possible to create a function that stores items like this:
Writting the code in C++:
//Header files
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Class inventoryItem
class inventoryItem
{
public:
//Variable declaration
string itemName;
string itemNumber;
double itemPrice;
int itemQuantity;
};
//implementation of menuItems() function.
void menuItems()
{
// Display the Menu
cout << "=============================\n";
cout << " 1) Add an item to memory\n"
<< " 2) Search memory for an item \n"
<< " 3) List what's in memory\n"
<< " 4) Total value on hand\n"
<< " 5) Save to a file\n"
<< " 6) Read from a file\n"
<< " 0) Exit Program" << endl;
}
//main method.
int main()
{
//variable declaration.
int mnuChoice, cn = 0, d;
string n;
double sum = 0, c;
int temp;
string a, b;
string inputBuffer;
inventoryItem items[100];
ifstream inventoryFile("inventory.txt");
if (inventoryFile)
{
//store the data from the file to memory.
while (!(inventoryFile.eof()))
{
inventoryFile >> items[cn].itemName;
inventoryFile >> items[cn].itemNumber;
inventoryFile >> inputBuffer;
items[cn].itemPrice = atof(inputBuffer.c_str());
inventoryFile >> inputBuffer;
items[cn].itemQuantity = atoi(inputBuffer.c_str());
cn++;
}
inventoryFile.close();
ifstream inventoryFile("inventory.txt");
ofstream fout;
fout.open("inventory.txt");
do
{
menuItems();
cout << cn << " items stored." << endl;
// Prompt and read choice form the user
cout << "Enter your choice: ";
cin >> mnuChoice;
switch (mnuChoice)
{
//Add a new inventory item to the data in memory.
case 1:
// Prompt and read name of the item from the user
cout << "Enter name of the item: ";
cin >> inputBuffer;
items[cn].itemName = inputBuffer;
// Prompt and read itemNumber from the user
cout << "Enter the itemNumber: ";
cin >> inputBuffer;
items[cn].itemNumber = inputBuffer;
// Prompt and read itemPrice from the user
cout << "Enter the itemPrice: ";
cin >> inputBuffer;
items[cn].itemPrice = atof(inputBuffer.c_str());
// Prompt and read itemQuantity from the user
cout << "Enter the itemQuantity: ";
cin >> inputBuffer;
items[cn].itemQuantity = atoi(inputBuffer.c_str());
cn++;
break;
//Search the inventory for an item by inventory number.
case 2:
temp = 0;
cout << "Enter inventory itemNumber: ";
cin >> n;
for (int i = 0; i < cn&&temp != 1; i++)
{
// If found, print the stored info
if (items[i].itemNumber.compare(n) == 0)
{
// Display the stored information
cout << "Item Name" << "\t" << "Item Number" << "\t" << " Item Price" << "\t" << " Item Quantity" << endl;
cout << items[i].itemName << "\t " << items[i].itemNumber << " \t" << items[i].itemPrice << "\t " << items[i].itemQuantity << endl;
temp = 1;
}
}
// otherwise print 'not found'.
if (temp == 0)
{
cout << "Not found!!!" << endl;
}
break;
//List the inventory on the screen
case 3:
cout << "The data in the memory is:" << endl;
cout << "Item Name" << "\t" << "Item Number" << "\t" << " Item Price" << "\t" << " Item Quantity" << endl;
for (int i = 0; i < cn; i++)
{
cout << items[i].itemName << "\t " << items[i].itemNumber << "\t " << items[i].itemPrice << " \t" << items[i].itemQuantity << endl;
}
break;
// Calculate the total value on hand.
case 4:
for (int i = 0; i < cn; i++)
{
// The sum is the accumulation of all the (quantities * prices).
sum += items[i].itemPrice*items[i].itemQuantity;
}
cout << "sum value on hand: " << sum << endl;
break;
// Save the inventory data in the given file
case 5:
for (int i = 0; i < cn; i++)
{
if (i == cn - 1)
{
fout << items[i].itemName << " \t" << items[i].itemNumber << " \t" << items[i].itemPrice << " \t" << items[i].itemQuantity;
}
else
{
fout << items[i].itemName << " \t" << items[i].itemNumber << " \t" << items[i].itemPrice << " \t" << items[i].itemQuantity << endl;
}
}
break;
// Read the data from the file
case 6:
while (!(inventoryFile.eof()))
{
inventoryFile >> a;
inventoryFile >> b;
inventoryFile >> inputBuffer;
c = atof(inputBuffer.c_str());
inventoryFile >> inputBuffer;
d = atoi(inputBuffer.c_str());
// Dsiplays the data in the file
cout << a << " " << b << " " << c << " " << d << endl;
}
break;
}
// Check the menu choice is not equal to 0,
//if it is equal exit from the loop
} while (mnuChoice != 0);
// File closed
fout.close();
inventoryFile.close();
}
// If the inventory file does not exist
// and then display the error message.
else
{
cout << "File does not exist!" << endl;
}
return 0;
}
See more about C++ code at brainly.com/question/17544466
#SPJ1

