Author Topic: VC++ - Hardware Inventory System using Random Access File  (Read 1907 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
VC++ - Hardware Inventory System using Random Access File
« on: November 14, 2011, 11:39:13 am »
You are the owner of a hardware store and need to keep an inventory that can tell you
what different tools you have, how many of each you have on hand and the cost of
each one. Write a program that initializes the random-access file hardware.dat to
100 empty records, lets you input the data concerning each tool, enables you to list all
your tools, lets you delete a record for a tool that you no longer have and lets you
update any information in the file. The tool identification number should be the record
number. Use the following information to start your file:

Record# Tool name Quantity Cost
3 Electric sander 7 57.98
17 Hammer 76 11.99
24 Jig saw 21 11.00
39 Lawn mower 3 79.50
56 Power saw 18 99.99
68 Screwdriver 106 6.99
77 Sledge hammer 11 21.50
83 Wrench 34 7.50





#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>

using namespace std;

typedef struct _tooldata
{
   int toolNum;   
   char toolName[128];
   int quantity;
   double cost;

} toolData;


void textFile(fstream &readFromFile);
int enterChoice();
void updateRecord(fstream &updateFile);
void newRecord(fstream &insertInFile);
void deleteRecord(fstream &deleteFromFile);
void outputLine(ostream &output, toolData tool);

void gotoxy(int x, int y)
{
   COORD ord;
   ord.X = x;
   ord.Y = y;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);
}

void clrscr()
{
   system("CLS");
}


static char FILENAME[] = "tools.dat";

void CreateDataFile()
{
   // ::unlink(FILENAME);
   fstream testFileExists(FILENAME, ios::in | ios::out);
   if (!testFileExists) {
      ofstream outtools(FILENAME);
      toolData *data = new toolData();
      strcpy(data->toolName, "");
      data->quantity = 0;
      data->cost = 0.0;
      data->toolNum = 0;
      for(int i = 0; i < 100; i++)
         outtools.write((char*)data, sizeof(*data));
      outtools.close();
   }
   else
      testFileExists.close();
}

int main()
{
   CreateDataFile();

   fstream inOuttools(FILENAME, ios::in | ios::out);

   inOuttools.seekg(0,ios::end); // positions the file position pointer
   // to the end of the file
   long location = inOuttools.tellg(); //location = size of file
   cout << "Size of tools.dat = " << location;

   //if ( location == 0 )   
   // makeDirectAccessFile(inOuttools);

   int choice;

   while ( ( choice = enterChoice() ) != 5 ) {

      switch (choice) {
         case 1: textFile(inOuttools); break;
         case 2: updateRecord(inOuttools); break;
         case 3: newRecord(inOuttools); break;
         case 4: deleteRecord(inOuttools); break;
         default: cerr << "Incorrect choice" << endl; break;
      }

      inOuttools.clear(); // resets end-of-file indicator
   }
   clrscr();
   return 0;
}

// Prompt for and input menu choice
int enterChoice(void)
{
   cout << endl << "Enter your choice" << endl
      << "1 - store a formatted text file of accounts" << endl
      << " called \"print.txt\" for printing" << endl
      << "2 - update an account" << endl
      << "3 - add a new account" << endl
      << "4 - delete an account" << endl
      << "5 - end program" << endl << "? ";

   int menuChoice;
   cin >> menuChoice;
   return menuChoice;
}

// Create a random access data file called <tools.dat>
void makeDirectAccessFile(fstream &inOuttools)
{
   toolData blanktool = {0, "", 0, 0.0};

   for (int i = 1; i <= 100; i++)
      inOuttools.write((char *)&blanktool, sizeof(toolData));

   cout << endl << endl
      << "A direct access file called <tools.dat> has been " << endl
      << "created with four fields initialized as follows:"
      << endl << endl
      << " toolNum = 0" << endl
      << " toolName[15] = \"\"" << endl
      << " long quantity = \"\"" << endl
      << " cost = 0.0" << endl;

   cout << endl << "Press any key to return to the menu." << endl << endl;
   getch();
}

// Create formatted text file for printing
void textFile(fstream &readFromFile)
{
   ofstream outPrintFile("print.txt", ios::out);

   if (!outPrintFile) {
      cerr << "File could not be opened." << endl;
      exit(1);
   }

   clrscr();

   cout << setiosflags(ios::left) << setw(6) << "Tool"
      << setw(16) << "Tool Name" << setw(11) << "Quantity"
      << setiosflags(ios::right) << setw(10) << "Cost" << endl;

   outPrintFile << setiosflags(ios::left) << setw(6) << "Tool"
      << setw(16) << "Tool Name" << setw(11) << "Quantity"
      << setiosflags(ios::right) << setw(10) << "Cost" << endl;
   readFromFile.seekg(0);

   toolData tool;
   readFromFile.read((char *)&tool, sizeof(toolData));

   while (!readFromFile.eof()) {
      if (tool.toolNum != 0) {
         outputLine(outPrintFile, tool);
         outputLine(cout, tool); // to see on screen what goes to file.
      } // if

      readFromFile.read((char *)&tool, sizeof(toolData));
   } // while
}

// Update an account's cost
void updateRecord(fstream &updateFile)
{
   int account;

   clrscr();

   do {
      cout << "Enter account to update (1 - 100): ";
      cin >> account;
   } while (account < 1 || account > 100);

   updateFile.seekg((account - 1) * sizeof(toolData));

   toolData tool;
   updateFile.read((char *)&tool, sizeof(toolData));

   if (tool.toolNum != 0) {
      outputLine(cout, tool);
      cout << endl << "Enter charge (+) or payment (-): ";

      float transaction;
      cin >> transaction;
      tool.cost += transaction;
      outputLine(cout, tool);
      updateFile.seekp((account - 1) * sizeof(toolData));
      updateFile.write((char *)&tool, sizeof(toolData));
   }
   else
      cerr << "Acount #" << account << " has no information." << endl;
}

// Create and insert new record
void newRecord(fstream &insertInFile)
{
   char text[80];
   clrscr();

   cout << "Enter new account number (1 - 100): ";

   int account;
   cin >> account;

   insertInFile.seekg((account - 1) * sizeof(toolData));

   toolData tool;
   insertInFile.read((char *)&tool, sizeof(toolData));

   if (tool.toolNum == 0) { // if toolNum == 0
      cout << "Enter the tool name and press <ENTER>: ";
      cin.getline(text,80); // dummy input clears the input buffer up to '\n'
      cin.getline(text,80); // grabbing the new stuff being keyed in

      if ( strlen(text) > 14) // tests if input is more than 14 printable chars
      { // begin if
         strncpy (tool.toolName, text, 14); // copies 14 chars to toolName
         tool.toolName[14] = '\0'; // fifteenth char is a null character
         cout << "Name is too long. It has been shortened to "
            << tool.toolName << "." << endl;
      } // end if
      else // if toolName <= 14 characters
      { // begin else if toolName <= 14 characters
         strcpy(tool.toolName, text);
         tool.toolName[14] = '\0'; // fifteenth char is a null character
      } // end else if toolName <= 14 characters

      cout << "Enter the quantity and press <ENTER>: ";
      cin >> tool.quantity;

      cout << "Enter the cost as a decimal value and press <ENTER>: ";
      cin >> tool.cost;

      // The following two lines of code had to be substituted for the above
      // to accomodate spaces in the tool name field.
      // cout << "Enter toolName, quantity, cost" << endl << "? ";
      // cin >> tool.toolName >> tool.quantity >> tool.cost;

      tool.toolNum = account;
      insertInFile.seekp((account - 1) * sizeof(toolData));
      insertInFile.write((char *)&tool, sizeof(toolData));
   }
   else // if toolNum == 0
      cerr << "Account #" << account
      << " already contains information." << endl;
}

// Delete an existing record
void deleteRecord(fstream &deleteFromFile)
{
   clrscr();

   cout << "Enter account number to delete (1 - 100): ";

   int account;
   cin >> account;

   deleteFromFile.seekg((account - 1) * sizeof(toolData));

   toolData tool;
   deleteFromFile.read((char *)&tool, sizeof(toolData));

   if (tool.toolNum != 0) {
      toolData blanktool = {0, "", 0, 0};
      deleteFromFile.seekp((account - 1) * sizeof(toolData));
      deleteFromFile.write((char *)&blanktool, sizeof(toolData));
      cout << setiosflags(ios::left) << setw(6) << "tool"
         << setw(16) << "Last Name" << setw(11) << "First Name"
         << setiosflags(ios::right) << setw(10) << "cost" << endl;
      outputLine(cout, tool);
      cout << "Account #" << account << " deleted." << endl;
   }
   else
      cout << "Account #" << account << " is empty." << endl;
}

// Output a line of tool information to the file print.txt
void outputLine(ostream &output, toolData tool)
{
      output << setiosflags(ios::left) << setw(6) << tool.toolNum
      << setw(16) << tool.toolName << setw(11) << tool.quantity
      << setiosflags(ios::fixed | ios::showpoint | ios::right)
      << setw(10) << setprecision(2) << tool.cost << endl;
}



 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1