computer science lab

profileqwerty564

We're going to do an inventory management system.  In the real world, this would use a database, not a file.  Inventory contains

  • item names (or description), like "socks, - red" or "socks - blue".  Thius has to be a character string that WILL include spaces.
  • item numbers that are unique to the item, similar to UPC codes, like 121821 or 128122.  These are usually character strings.
  • price, like 4.99, a double.
  • quantity on hand, like 13, an integer.

Your code need to have the following features, all accessible from a menu.

  • Add a new inventory item to the data in memory.  You'll type in the inventory number, description, quantity and price.  This will also update the number of items on the menu screen. 
  • List the inventory on the screen in neat columns.  Display it in chucks of 15 items with a pause in between.
  • Search the inventory for an item by inventory number.   If found, print the stored info, otherwise print 'not found'.
  • Calculate the total value on hand. The sum is the accumulation of all the (quantities * prices).
  • Save what's in memory to the disk
  • Overwrite what's in memory from the disk

The way your work this is you'll load a file, add, search, etc, then write it back to disk. 

Ideas:

  • An array of objects works nicely if you read ahead, although this isn't required, see my code below (sorry about the indenting).

Here's a sample menu:


===========================
1) Add an item to memory
2) Search memory for an item
3) List what's in memory
4) Total value on hand
5) Save to a file
6) Read in from a file



0) Exit Program
56 items stored

 

#include <iostream>
#include <fstream>
#include <cstdlib>
 
using namespace std;
 
class invItem
{
public:
string name;
string number;
double price;
int quantity;
};
 
void printItem(invItem someitem);
 

int main()
{
/// read in one record
string itemName;
string itemNumber;
double itemPrice;
int itemQuantity;
 

/// read in a single record from a file
string inputBuffer;
ifstream invFile("invent1.txt"); /// declares and opens
 
getline(invFile, itemName);
getline(invFile, itemNumber);
getline(invFile, inputBuffer);
itemPrice = atof(inputBuffer.c_str());
getline(invFile, inputBuffer);
itemQuantity = atoi(inputBuffer.c_str());
 
cout << "Item = " << itemName << "[" << itemNumber <<"]" << endl;
cout << "Quantity = " << itemQuantity << " @ " << itemPrice
<< " = " << itemPrice * itemQuantity;
invFile.close();
 
/// write a single record to a file
ofstream invOutFile("invent1.txt", ios::app); /// declare and open for append
invOutFile << "White Sox" << endl;
invOutFile << "82452374293742" << endl;
invOutFile << 6.99 << endl;
invOutFile << 20 << endl;
invOutFile.close();
 
/// build a single invItem object
invItem someitem;
someitem.name="Green Socks";
someitem.number="5435234523452345";
someitem.price=5.99;
someitem.quantity=67;
printItem(someitem);
 
/// build an array of invItem objects and save one to disk
invItem items[1000];
items[0].name="Yellow Socks";
items[0].number="25435567456723452345";
items[0].price=7.99;
items[0].quantity=27;
printItem(items[0]);
ofstream invOutFile2("invent1.txt", ios::app); /// declare and open for append
invOutFile2 << items[0].name << endl;
invOutFile2 << items[0].number << endl;
invOutFile2 << items[0].price << endl;
invOutFile2 << items[0].quantity<< endl;
invOutFile2.close();
 
return 0;
}
 
void printItem(invItem someitem)
{
cout << "Item = " << someitem.name << "[" << someitem.number <<"]" << endl;
cout << "Quantity = " << someitem.quantity << " @ " << someitem.price
<< " = " << someitem.price * someitem.quantity << endl;

 

}

    • 11 years ago
    • 20
    Answer(0)
    Bids(0)